[ASP.NET][C#]使用HtmlAgilityPack(5) 擷取每旬報關匯率,幾乎外銷公司都需要用到

每旬報關匯率查詢



財政部-每旬報關匯率查詢

http://web.customs.gov.tw/currency/currency/currency.asp?qry=1&language=c

搞了很久的關稅截取,給大家做個參考,謝謝

 

1.我想許多公司行號都可能需要一個查看外銷關稅匯看板率,這時C# 就可以幫你做到了


2.新增一個 customsCurrency.aspx


3. customsCurrency.aspx  HTML語法


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="customsCurrency.aspx.cs" Inherits="customsCurrency" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 335px;
        }

        .auto-style4 {
            text-align: center;
            font-size: 50px;
            background-color: #FFF594;
        }

        .auto-style5 {
            color: #9C520B;
        }

        .auto-style7 {
            width: 335px;
            background-color: #CAFFCA;
        }

        .auto-style9 {
            font-size: 50px;
        }

        .auto-style10 {
            width: 335px;
            font-size: 40px;
            text-align: center;
            font-weight: bold;
        }

        .auto-style11 {
            width: 200px;
            font-size: 40px;
            text-align: center;
            font-weight: bold;
        }

        .auto-style12 {
            width: 200px;
            font-size: 46px;
            background-color: #CAFFCA;
        }

        .auto-style13 {
            width: 200px;
            font-size: 46px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table align="center">
                <tr>
                    <td colspan="3">
                        <asp:Label ID="time2" runat="server" Style="font-weight: 700" />
                    </td>
                </tr>
                <tr>
                    <td class="auto-style4" colspan="3"><strong><span class="auto-style5">財政部-每旬報關匯率查詢</span></strong></td>
                </tr>
                <tr>
                    <td class="auto-style10">幣別</td>
                    <td class="auto-style11">買入</td>
                    <td class="auto-style11">賣出</td>
                </tr>
                <tr>
                    <td class="auto-style7">
                        <asp:Label ID="currencytxt201" runat="server" CssClass="auto-style9" /></td>
                    <td class="auto-style12">
                        <asp:Label ID="buytxt201" runat="server" /></td>
                    <td class="auto-style12">
                        <asp:Label ID="selltxt201" runat="server" /></td>
                </tr>
                <tr>
                    <td class="auto-style1">
                        <asp:Label ID="currencytxt202" runat="server" CssClass="auto-style9" /></td>
                    <td class="auto-style13">
                        <asp:Label ID="buytxt202" runat="server" /></td>
                    <td class="auto-style13">
                        <asp:Label ID="selltxt202" runat="server" /></td>
                </tr>
                <tr>
                    <td class="auto-style7">
                        <asp:Label ID="currencytxt203" runat="server" CssClass="auto-style9" /></td>
                    <td class="auto-style12">
                        <asp:Label ID="buytxt203" runat="server" /></td>
                    <td class="auto-style12">
                        <asp:Label ID="selltxt203" runat="server" /></td>
                </tr>
                <tr>
                    <td class="auto-style1">
                        <asp:Label ID="currencytxt204" runat="server" CssClass="auto-style9" /></td>
                    <td class="auto-style13">
                        <asp:Label ID="buytxt204" runat="server" /></td>
                    <td class="auto-style13">
                        <asp:Label ID="selltxt204" runat="server" /></td>
                </tr>
                <tr>
                    <td class="auto-style7">
                        <asp:Label ID="currencytxt205" runat="server" CssClass="auto-style9" /></td>
                    <td class="auto-style12">
                        <asp:Label ID="buytxt205" runat="server" /></td>
                    <td class="auto-style12">
                        <asp:Label ID="selltxt205" runat="server" /></td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

4.customsCurrency.aspx.cs 語法


using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;


    public partial class customsCurrency : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //時間字體大小
            time2.Font.Size = 20;

            //顏色設置
            buytxt201.ForeColor = buytxt202.ForeColor = buytxt203.ForeColor = buytxt204.ForeColor = buytxt205.ForeColor = Color.Red;
            selltxt201.ForeColor = selltxt202.ForeColor = selltxt203.ForeColor = selltxt204.ForeColor = selltxt205.ForeColor = Color.FromName("#008000");

            //指定來源網頁
            WebClient url2 = new WebClient();
            //將網頁來源資料暫存到記憶體內
            MemoryStream ms2 = new MemoryStream(url2.DownloadData("http://web.customs.gov.tw/currency/currency/currency.asp?qry=0&language=c"));
            //每旬報關匯率查詢

            // 使用預設編碼讀入 HTML
            HtmlDocument doc2 = new HtmlDocument();

            //doc2.Load(ms2, Encoding.UTF8);
            doc2.Load(ms2, Encoding.Default);

            //以每月日期為判斷,符合條件則印出
            switch (DateTime.Now.Day)
            {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 10:
                    //取得現在的日期
                    time2.Text = "牌告時間:" + DateTime.Now + " - 上半月 1-10 日";
                    
                    //美元
                    currencytxt201.Text = doc2.DocumentNode.SelectSingleNode(@"//tr[21]/td[2]").InnerText + " (" + doc2.DocumentNode.SelectSingleNode(@"//tr[21]/td[3]").InnerText + ") ";
                    buytxt201.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[21]/td[7]").InnerText.ToString()));
                    selltxt201.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[21]/td[8]").InnerText.ToString()));
                    //英鎊
                    currencytxt202.Text = doc2.DocumentNode.SelectSingleNode(@"//tr[7]/td[2]").InnerText + " (" + doc2.DocumentNode.SelectSingleNode(@"//tr[7]/td[3]").InnerText + ") ";
                    buytxt202.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[7]/td[7]").InnerText.ToString()));
                    selltxt202.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[7]/td[8]").InnerText.ToString()));
                    //日圓
                    currencytxt203.Text = doc2.DocumentNode.SelectSingleNode(@"//tr[10]/td[2]").InnerText + " (" + doc2.DocumentNode.SelectSingleNode(@"//tr[10]/td[3]").InnerText + ") ";
                    buytxt203.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[10]/td[7]").InnerText.ToString()));
                    selltxt203.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[10]/td[8]").InnerText.ToString()));
                    //歐元
                    currencytxt204.Text = doc2.DocumentNode.SelectSingleNode(@"//tr[6]/td[2]").InnerText + " (" + doc2.DocumentNode.SelectSingleNode(@"//tr[6]/td[3]").InnerText + ") ";
                    buytxt204.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[6]/td[7]").InnerText.ToString()));
                    selltxt204.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[6]/td[8]").InnerText.ToString()));
                    //人民幣
                    currencytxt205.Text = doc2.DocumentNode.SelectSingleNode(@"//tr[4]/td[2]").InnerText + " (" + doc2.DocumentNode.SelectSingleNode(@"//tr[4]/td[3]").InnerText + ") ";
                    buytxt205.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[4]/td[7]").InnerText.ToString()));
                    selltxt205.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[4]/td[8]").InnerText.ToString()));
                    break;
                case 11:
                case 12:
                case 13:
                case 14:
                case 15:
                case 16:
                case 17:
                case 18:
                case 19:
                case 20:
                    //取得現在的日期
                    time2.Text = "牌告時間:" + DateTime.Now + " - 月中 11-20 日";
                    //美元
                    currencytxt201.Text = doc2.DocumentNode.SelectSingleNode(@"//tr[43]/td[2]").InnerText + " (" + doc2.DocumentNode.SelectSingleNode(@"//tr[43]/td[3]").InnerText + ") ";
                    buytxt201.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[43]/td[7]").InnerText.ToString()));
                    selltxt201.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[43]/td[8]").InnerText.ToString()));
                    //英鎊
                    currencytxt202.Text = doc2.DocumentNode.SelectSingleNode(@"//tr[14]/td[2]").InnerText + " (" + doc2.DocumentNode.SelectSingleNode(@"//tr[14]/td[3]").InnerText + ") ";
                    buytxt202.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[14]/td[7]").InnerText.ToString()));
                    selltxt202.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[14]/td[8]").InnerText.ToString()));
                    //日圓
                    currencytxt203.Text = doc2.DocumentNode.SelectSingleNode(@"//tr[20]/td[2]").InnerText + " (" + doc2.DocumentNode.SelectSingleNode(@"//tr[20]/td[3]").InnerText + ") ";
                    buytxt203.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[20]/td[7]").InnerText.ToString()));
                    selltxt203.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[20]/td[8]").InnerText.ToString()));
                    //歐元
                    currencytxt204.Text = doc2.DocumentNode.SelectSingleNode(@"//tr[12]/td[2]").InnerText + " (" + doc2.DocumentNode.SelectSingleNode(@"//tr[12]/td[3]").InnerText + ") ";
                    buytxt204.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[12]/td[7]").InnerText.ToString()));
                    selltxt204.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[12]/td[8]").InnerText.ToString()));
                    //人民幣
                    currencytxt205.Text = doc2.DocumentNode.SelectSingleNode(@"//tr[8]/td[2]").InnerText + " (" + doc2.DocumentNode.SelectSingleNode(@"//tr[8]/td[3]").InnerText + ") ";
                    buytxt205.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[8]/td[7]").InnerText.ToString()));
                    selltxt205.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[8]/td[8]").InnerText.ToString()));
                    break;
                case 21:
                case 22:
                case 23:
                case 24:
                case 25:
                case 26:
                case 27:
                case 28:
                case 29:
                case 30:
                case 31:
                    //取得現在的日期
                    time2.Text = "牌告時間:" + DateTime.Now + " - 下半月 21-31 日";
                    //美元
                    currencytxt201.Text = doc2.DocumentNode.SelectSingleNode(@"//tr[63]/td[2]").InnerText + " (" + doc2.DocumentNode.SelectSingleNode(@"//tr[63]/td[3]").InnerText + ") ";
                    buytxt201.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[63]/td[7]").InnerText.ToString()));
                    selltxt201.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[63]/td[8]").InnerText.ToString()));
                    //英鎊
                    currencytxt202.Text = doc2.DocumentNode.SelectSingleNode(@"//tr[21]/td[2]").InnerText + " (" + doc2.DocumentNode.SelectSingleNode(@"//tr[21]/td[3]").InnerText + ") ";
                    buytxt202.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[21]/td[7]").InnerText.ToString()));
                    selltxt202.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[21]/td[8]").InnerText.ToString()));
                    //日圓
                    currencytxt203.Text = doc2.DocumentNode.SelectSingleNode(@"//tr[30]/td[2]").InnerText + " (" + doc2.DocumentNode.SelectSingleNode(@"//tr[30]/td[3]").InnerText + ") ";
                    buytxt203.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[30]/td[7]").InnerText.ToString()));
                    selltxt203.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[30]/td[8]").InnerText.ToString()));
                    //歐元
                    currencytxt204.Text = doc2.DocumentNode.SelectSingleNode(@"//tr[18]/td[2]").InnerText + " (" + doc2.DocumentNode.SelectSingleNode(@"//tr[18]/td[3]").InnerText + ") ";
                    buytxt204.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[18]/td[7]").InnerText.ToString()));
                    selltxt204.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[18]/td[8]").InnerText.ToString()));
                    //人民幣
                    currencytxt205.Text = doc2.DocumentNode.SelectSingleNode(@"//tr[12]/td[2]").InnerText + " (" + doc2.DocumentNode.SelectSingleNode(@"//tr[12]/td[3]").InnerText + ") ";
                    buytxt205.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[12]/td[7]").InnerText.ToString()));
                    selltxt205.Text = String.Format("{0:0.00000}", Convert.ToDouble(doc2.DocumentNode.SelectSingleNode(@"//tr[12]/td[8]").InnerText.ToString()));
                    break;
                default:
                    Response.Write("失敗");
                    break;
            }

            //清除資料
            doc2 = null;
            url2 = null;
            ms2.Close();

            //網頁更新
            HtmlMeta meta = new HtmlMeta();
            meta.Attributes.Add("http-equiv", "refresh");
            //設定秒數,5秒後執行頁面更新
            meta.Content = "5";
            this.Header.Controls.Add(meta);
        }
    }

5.這個網頁不用透過電腦,就可以在直接電視上瀏覽
測試機種使用 HERAN禾聯 50吋HERTV HD-50AC2
直接用電視當作電子看板

還是不會的人可以參考http://www.dotblogs.com.tw/jackbgova/archive/2014/06/11/145488.aspx

我之前做的程式