功能:
1.抓取外匯資料
2.預設顯示 美金 英鎊 日圓 RMB
3.可供選顯示/隱藏 其他外幣資料
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ActionResult Index() | |
{ | |
string strUrl = "http://rate.bot.com.tw/Pages/Static/UIP003.zh-TW.htm"; | |
//Html AgilityPack | |
WebClient client = new WebClient(); | |
currency currency = new currency(); | |
using (MemoryStream ms = new MemoryStream(client.DownloadData(strUrl))) | |
{ | |
HtmlDocument doc = new HtmlDocument(); | |
doc.Load(ms, Encoding.UTF8); | |
// 取得所有符合條件的nodes | |
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//table[2]/tr[@class='color0' or @class='color1' ]");///tr[contains(@class='color0')] td[@class='titleLeft'] | |
currency.dtCurrency = new DataTable(); | |
currency.dtCurrency.Columns.Add("幣別"); | |
currency.dtCurrency.Columns.Add("買入"); | |
currency.dtCurrency.Columns.Add("賣出"); | |
currency.NewTime = doc.DocumentNode.SelectSingleNode("//div[@class='entry-content']/table/tr/td[2]").InnerText.Trim().Replace("\r\n", "").Replace(" ",""); | |
List<CheckBoxes> CurrencyType = new List<CheckBoxes>(); | |
foreach (HtmlNode node in nodes) | |
{ | |
DataRow r = currency.dtCurrency.NewRow(); | |
r[0] = node.SelectSingleNode("td[@class='titleLeft']").InnerText.Replace(" ",""); | |
if (r[0].ToString() == "美金 (USD)" || r[0].ToString() == "英鎊 (GBP)" || r[0].ToString() == "日圓 (JPY)" || r[0].ToString() == "人民幣(CNY)") | |
{ | |
CurrencyType.Add(new CheckBoxes() | |
{ | |
Text = r[0].ToString(), | |
Checked = true | |
}); | |
}else | |
{ | |
CurrencyType.Add(new CheckBoxes() | |
{ | |
Text = r[0].ToString(), | |
Checked = false | |
}); | |
} | |
r[1] = node.SelectSingleNode("td[3]").InnerText; | |
r[2] = node.SelectSingleNode("td[4]").InnerText; | |
currency.dtCurrency.Rows.Add(r); | |
currency.CurrencyType = CurrencyType; | |
} | |
} | |
return View(currency); | |
} | |
public class CheckBoxes | |
{ | |
public string Text { get; set; } | |
public bool Checked { get; set; } | |
} | |
public class currency | |
{ | |
public string Name { get; set; } | |
public double buy { get; set; } | |
public double sale { get; set; } | |
public List<CheckBoxes> CurrencyType { get; set; } | |
public string NewTime{ get; set; } | |
public DataTable dtCurrency { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model AfternoonTea.Models.currency | |
@{ | |
ViewBag.Title = "Home Page"; | |
int count = 0; | |
} | |
<div class="jumbotron"> | |
<h1>台灣銀行-外幣匯率 </h1> | |
</div> | |
<section class="content"> | |
<div class="row"> | |
<div class="col-xs-12"> | |
<div class="box box-solid"> | |
<div class="row pad"> | |
<div class="label label-primary"> | |
<input type="checkbox" id="All" name="All" class="checkbox-inline" style="width:20px;height:20px;position:relative;top:-2px;" /><label style="font-size:20px;margin-right:10px;padding:3px;">All </label> | |
</div> | |
<br /> | |
@for (int i = 0; i < Model.CurrencyType.Count; i++) | |
{ | |
<div class="label label-success" style="margin-right:10px;margin-bottom:3px;padding:5px;"> | |
<input type="checkbox" id="ck_@i" name="currency" value="@Model.CurrencyType[i].Text" class="ck_list checkbox-inline" style="width:20px;height:20px;position:relative;top:-2px;" checked="@Model.CurrencyType[i].Checked" /> | |
<label style="font-size:20px;padding:5px;">@Model.CurrencyType[i].Text.PadRight(9, ' ')</label> | |
</div> | |
//@Html.CheckBoxFor(m => m.CurrencyType[i].Text, new { Class = "ck_list", id = string.Format("ck_{0}", i) }) | |
if (i!=0 && i%5 == 0) { <br />} | |
} | |
<br /> | |
</div> | |
</div> | |
</div> | |
</div> | |
</section> | |
<div class="row"> | |
<h3 class="label-default" style="color:white">@Model.NewTime</h3> | |
<table class="table table-bordered table-hover"> | |
<thead> | |
<tr> | |
<th>幣別</th> | |
<th>買入</th> | |
<th>賣出</th> | |
</tr> | |
</thead> | |
@foreach (System.Data.DataRow item in Model.dtCurrency.Rows) | |
{ | |
string hide = "display:none;"; | |
if (count == 0 || count == 2 || count == 7 || count == 18 ) | |
{ | |
hide = ""; | |
} | |
<tr id="row_@count" class="currencyRow" style="@hide"> | |
<td>@item[0]</td> | |
<td>@item[1]</td> | |
<td>@item[2]</td> | |
</tr> | |
count++; | |
} | |
</table> | |
</div> | |
<script type="text/javascript"> | |
$(function (){ | |
$("#All").click(function(){ | |
if($("#All").prop("checked")) | |
{ | |
$(".ck_list").prop("checked",false); | |
$(".currencyRow").removeAttr("style"); | |
}else | |
{ | |
$(this).prop("checked",false); | |
$(".currencyRow").attr("style","display:none;"); | |
$("#ckStatus").val("All"); | |
} | |
}); | |
$(".ck_list").on("change", function () { | |
var row = $(this).attr("id").substring(3); | |
var count = @Model.CurrencyType.Count; | |
console.log(row); | |
if ($(this).prop("checked")) | |
{ | |
$("#row_" + row).removeAttr("style"); | |
}else | |
{ | |
$("#row_" + row).attr("style", "display:none;"); | |
} | |
if ($(".ck_list").is(":checked").length == count) | |
{ | |
$("#All").prop("checked", true); | |
}else | |
{ | |
$("#All").removeAttr("checked"); | |
} | |
}); | |
}); | |
</script> |
沒有留言:
張貼留言