返回列表 上一主題 發帖

請問這個網頁如何用WEB查詢輸入excel

回復 4# jewayy

  1. Option Explicit
  2. Dim IE As Object
  3. Sub 出口報單放行資料查詢()
  4.     Dim 出口報單號碼 As String, n As Object
  5.         出口報單號碼 = InputBox("出口報單號碼", "出口報單放行資料查詢", "BE  02XE580024")
  6.         If 出口報單號碼 = "" Then Exit Sub
  7.         With CreateObject("InternetExplorer.Application")
  8.            .Visible = True
  9.             .Navigate "http://portal.sw.nat.gov.tw/APGQ/GB315?request_locale=zh_TW&declNo=" & 出口報單號碼
  10.             Do While .Busy = True
  11.         DoEvents
  12.         Loop
  13.         For Each n In .document.getelementsbytagname("INPUT")
  14.            If n.Value = "查詢" Then
  15.                 n.Click                        '網頁按下 查詢
  16.                 Exit For
  17.            End If
  18.         Next
  19.         Application.Wait (Time + TimeValue("0:00:03"))  '依網頁下載速度調整等待秒數
  20.         Set IE = .document
  21.         查詢結果
  22.         .Quit
  23.     End With
  24. End Sub
  25. Private Sub 查詢結果()
  26.     Dim Ar(1 To 7), SH As Worksheet
  27.     '***** 網頁的原始檔案的本文
  28.     '<tbody><tr><td colspan="4" class="resultHeader">查詢結果</td></tr>
  29.     '<td class="resultHeader">海空運別</td><td class="result" id="transTypeCd">海</td>
  30.     '<td class="resultHeader" width="25%">出口報單號碼</td><td width="25%" class="result" id="declNo">BE  02XE580024</td>
  31.     '<td class="resultHeader" width="25%">報單類別</td><td width="25%" class="result" id="declType">G5</td>
  32.     '<td class="resultHeader" width="25%">總件數</td><td width="25%" class="result" id="totPackQty">32</td>
  33.     '<td class="resultHeader" width="25%">目的國家代碼</td><td width="25%" class="result" id="destCd">VNCLI</td>
  34.     '<td class="resultHeader" width="25%">總件數單位</td><td width="25%" class="result" id="totPackQtyUnit">PLT</td>
  35.     '<td class="resultHeader" width="25%">報單放行註記</td><td width="25%" class="result" id="examRelNote">Y</td>
  36.     '<td class="resultHeader" width="25%">總毛重</td><td width="25%" class="result" id="totGrossWeight">49376</td>
  37.     '<td class="resultHeader" width="25%">放行日期</td><td width="25%" class="result" id="relDate">102/09/17</td>
  38.     '<td class="resultHeader" width="25%">船舶名稱(海)/航機名稱(空)</td><td width="25%" class="result" id="vslName">UNI-PROSPER                        </td>
  39.     '<td class="resultHeader" width="25%">銷艙註記</td><td width="25%" class="result" id="marketMftNote">Y</td>
  40.     '<td class="resultHeader" width="25%">船舶航次(海)/航機班次(空)</td><td width="25%" class="result" id="voyageFlightNo">1084-186S</td>
  41.     '<td class="resultHeader" width="25%">船舶呼號(海)</td><td width="25%" class="result" id="vslSign">BKHC</td>
  42.     '<input id="statusMsg" type="text" class="msgText uppercase" readonly="readonly" style="width: 775px; color: red;">
  43.      Set SH = ActiveSheet                                 '指定顯示資料的工作表 'ActiveSheet->作用中的工作表
  44.     If InStr(IE.getElementById("statusMsg").Value, "[執行成功]") = 0 Then
  45.         SH.[b2].Resize(7, 1) = Application.WorksheetFunction.Transpose(Ar)
  46.         SH.[d2].Resize(7, 1) = Application.WorksheetFunction.Transpose(Ar)
  47.         Exit Sub
  48.     End If
  49.     Ar(1) = IE.getElementById("transTypeCd").innertext      '海空運別
  50.     Ar(2) = IE.getElementById("declNo").Value               '出口報單號碼
  51.     Ar(3) = IE.getElementById("totPackQty").innertext       '總件數
  52.     Ar(4) = IE.getElementById("totPackQtyUnit").innertext   '總件數單位
  53.     Ar(5) = IE.getElementById("totGrossWeight").innertext   '總毛重
  54.     Ar(6) = IE.getElementById("vslName").innertext          '船舶名稱 (海) / 航機名稱(空)
  55.     Ar(7) = IE.getElementById("voyageFlightNo").innertext   '船舶航次 (海) / 航機班次(空)
  56.     SH.[b2].Resize(7, 1) = Application.WorksheetFunction.Transpose(Ar)
  57.     Ar(1) = ""
  58.     Ar(2) = IE.getElementById("declType").innertext         '報單類別
  59.     Ar(3) = IE.getElementById("destCd").innertext           '目的國家代碼
  60.     Ar(4) = IE.getElementById("examRelNote").innertext      '報單放行註記
  61.     Ar(5) = IE.getElementById("totGrossWeight").innertext   '放行日期
  62.     Ar(6) = IE.getElementById("marketMftNote").innertext    '銷艙註記
  63.     Ar(7) = IE.getElementById("vslSign").innertext          '船舶呼號 (海)
  64.     SH.[d2].Resize(7, 1) = Application.WorksheetFunction.Transpose(Ar)
  65.     SH.Columns.AutoFit
  66. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2013-11-17 09:06 編輯

回復 9# c_c_lai
EXCEL 2003, IE 8  或許是版本不同  declNo=>tagname("INPUT") 要用 VALUE
IE8   Ar(2) = IE.getElementById("declNo").innertext      '出口報單號碼  會傳回 空字串
要連同每個標題嗎 我@*#@*#@*#

參考7# joey0415  給的網址 http://portal.sw.nat.gov.tw/APGQ/GB315!query?declNo=BE++02XE580024
可捨去8#的程式碼 ,  如在 8# 圖片的工作表,這程式碼就簡便了.
  1. Option Base 1
  2. Sub Ex()
  3.     Dim Ar, AA(), 出口報單號碼 As String, Sh As Worksheet
  4.     出口報單號碼 = InputBox("出口報單號碼", "出口報單放行資料查詢", "BE  02XE580024")
  5.     If 出口報單號碼 = "" Then Exit Sub
  6.     Set Sh = ActiveSheet                                 '指定顯示資料的工作表 'ActiveSheet->作用中的工作表
  7.     With CreateObject("Microsoft.XMLHTTP")
  8.        .Open "GET", "http://portal.sw.nat.gov.tw/APGQ/GB315!query?declNo=" & 出口報單號碼, False
  9.         .send
  10.         Ar = Split(Replace(.responsetext, """", ""), ",")
  11.         AA = Array(1, 8, 4, 7, 2, 14, 11)           'A欗的標題內容 Ar中陣列對應之索引值
  12.         On Error GoTo Er                            '出口報單號碼 不正確會有錯誤:
  13.         For i = 1 To UBound(AA)
  14.             Sh.Cells(1 + i, "B") = Split(Ar(AA(i)), ":")(1)  'B欗
  15.        Next
  16.        AA = Array(5, 3, 10, 6, 12, 9)                 'C欗的標題內容 Ar中陣列對應之索引值
  17.        For i = 1 To UBound(AA)
  18.             Sh.Cells(2 + i, "D") = Split(Ar(AA(i)), ":")(1)  'D欗
  19.             If AA(i) = 6 Then Cells(2 + i, "D") = Replace(Cells(2 + i, "D"), "\/", "/")
  20.        Next
  21.     End With
  22.     Exit Sub
  23. Er:
  24.     Sh.[b2].Resize(7, 1) = ""
  25.     Sh.[d2].Resize(7, 1) = ""
  26. End Sub
  27. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

是為了方便指定AR陣列中指定的元素,在放進想要的CELLS中嗎?
如果要批次找100值放進去跑回圈,中間有錯的話
要放 on error resume next嗎?
回復 13# joey0415
沒錯是要放進想要的CELLS.

如果預期會有錯誤的程式碼之前 寫上 on error resume next ,程式就一直執行下去,如真有錯誤你是會不知道的
回復 11# jewayy

  1. Option Explicit
  2. Option Base 1
  3. Sub 口報單通關流程查詢()
  4.     Dim 出口報單號碼 As String, Rng As Range, AR, S As Variant, E As Variant, i As Integer, W As String, II As Integer
  5.     Dim Sh As Worksheet
  6.     出口報單號碼 = InputBox("出口報單號碼", "出口報單放行資料查詢", "BE  02XE580024")
  7.     If 出口報單號碼 = "" Then Exit Sub
  8.     Set Sh = ActiveSheet
  9.     '指定顯示資料的工作表 'ActiveSheet->作用中的工作表
  10.     Set Rng = Sh.Range("b2:B9, D3:D7, D9, B11, D11")
  11.     '**AR內容: 參照 **** 出口報單通關流程查詢(GB309) 網頁的原始檔*****
  12.     AR = Array("transTypeCd", "vslRegNo", "declNo", "brokerBoxNoName", "mawb", "hawb", "declType", "relCondSubCd" _
  13.     , "soNo", "custCd", "carrierAgencyCd", "arrangeNo", "examMethod", "debitMark", "firstSendDate", "lastSendDate")
  14.     With CreateObject("Microsoft.XMLHTTP")
  15.        .Open "GET", "http://portal.sw.nat.gov.tw/APGQ/GB309!query?&choice=D&declNo=" & 出口報單號碼, False
  16.         .send
  17.         S = Replace(.responsetext, """", "")
  18.     End With
  19.     i = 1
  20.     '*********** 查詢結果 *****
  21.     For Each E In Rng
  22.         E = ""
  23.         If InStr(S, AR(i)) Then
  24.             W = Mid(Split(Mid(S, InStr(S, AR(i)) + Len(AR(i))), ",")(0), 2)
  25.             E = IIf(InStr(LCase(W), "null"), "", W)
  26.         End If
  27.         i = i + 1
  28.     Next
  29.     '***************通關流程**********
  30.     AR = Split(S, "data:[")(1)                          '攔截 "data:[" 後的字串
  31.     AR = Split(AR, "]")(0)                              '攔截 "[" 前的字串
  32.     AR = Replace(Mid(AR, 2, Len(AR) - 2), "null", " ")  '替換 "null" 為 " "
  33.     AR = Replace(AR, "T", " ")                          '替換  "T"   為 " "
  34.     AR = Split(AR, "},{")                               '以 "},{" 分割為陣列
  35.     S = Array(4, 2, 0, 1, 3)
  36.     Sh.Range("A13").CurrentRegion.Offset(1) = ""
  37.     For i = 0 To UBound(AR)
  38.         For II = 0 To UBound(S) - 1
  39.             E = Split(AR(i), ",")(S(II + 1))            '以 S(II + 1)的值 取得 Split(AR(i), ",")陣列的索引值
  40.             Sh.Cells(i + 14, "A").Offset(, II) = Mid(E, InStr(E, ":") + 1)
  41.         Next
  42.     Next
  43. End Sub
  44. '出口報單通關流程查詢(GB309) 網頁的原始檔
  45. '<td class="resultHeader">海空運別</td><td id="transTypeCd" class="result">
  46. '<td class="resultHeader" width="25%">海關通關號碼</td><td width="25%" id="vslRegNo" class="result">
  47. '<td class="resultHeader" width="25%">裝貨單編號</td><td width="25%" id="soNo" class="result">
  48. '<td class="resultHeader">報單號碼</td><td id="declNo" class="result">
  49. '<td class="resultHeader">關區代碼</td><td id="custCd" class="result">
  50. '<td class="resultHeader">報關業者箱號</td><td id="brokerBoxNoName" class="result">
  51. '<td class="resultHeader">運輸業者/代理行代碼</td><td id="carrierAgencyCd" class="result">
  52. '<td class="resultHeader">託運單主號</td><td id="mawb" class="result">
  53. '<td class="resultHeader">理單號碼</td><td id="arrangeNo" class="result">
  54. '<td class="resultHeader">託運單分號</td><td id="hawb" class="result">
  55. '<td class="resultHeader">申請審驗方式</td><td id="examMethod" class="result">
  56. '<td class="resultHeader">報單類別</td><td id="declType" class="result">
  57. '<td class="resultHeader">放行附帶條件</td><td id="relCondSubCd" class="result">
  58. '<td class="resultHeader">是否為沖退稅e化報單</td><td id="debitMark" class="result">
  59. '<tr><td colspan="4" class="resultHeader">傳送總額交查至財稅中心的日期</td>
  60. '<td class="resultHeader">第一次傳送日期</td><td id="firstSendDate" class="result">
  61. '<td class="resultHeader">最後一次傳送日期</td><td id="lastSendDate" class="result">
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 18# stillfish00
  1. .document.body.innerHTML = .document.getElementById("queryResult").outerHTML
複製代碼
這招受教了
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2013-11-18 14:16 編輯

回復 20# joey0415
.document(文件).body(本體).innerHTML(代碼,文字) = .document.getElementById("queryResult").outerHTML(輸出的:代碼,文字)
可在這行程式碼設下中斷點,看一下網頁前後的變化
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2013-11-18 16:06 編輯

回復 22# c_c_lai
  1. For Each x In .document.getElementsByTagName("input")
  2.              If x.Name = "declNo" Then x.Value = sID
  3.             If x.Value = "查詢" Then x.Click
  4.         Next
複製代碼
這行也要修改 =0
  1. If InStr(sStatus, "[執行成功]") = 0 Then .Quit: MsgBox sStatus: Exit Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 25# stillfish00
感謝補足說明
回復 24# c_c_lai
如果這些 出口報單放行單查詢的網頁相類似的可如此 (改一下stillfish00的程式碼)
  1. Option Explicit
  2. Sub 出口查詢()
  3.     Dim sID As String, sStatus As String, URL As String
  4.     Dim x
  5.     URL = InputBox("1:出口報單放行資料查詢(產證專用)(GB315)" & vbLf & "2:出口報單通關流程查詢(GB309)", "出口資料查詢", 1)
  6.     If URL = "" Or (URL <> "1" And URL <> "2") Then Exit Sub
  7.     URL = IIf(URL = "1", "GB315", "GB309")
  8.     sID = InputBox("出口報單號碼", "出口報單放行資料" & URL & "查詢", "BE  02XE580024")
  9.     If sID = "" Then Exit Sub
  10.     URL = "http://portal.sw.nat.gov.tw/APGQ/" & URL & "?&declNo=" & sID   
  11.     With CreateObject("InternetExplorer.Application")
  12.         .Visible = True     '  是否顯示 IE
  13.         .Navigate URL
  14.         Do While .readyState <> 4
  15.             DoEvents
  16.         Loop
  17.         For Each x In .document.getElementsByTagName("input")
  18.             If x.Value = "查詢" Then x.Click: Exit For
  19.         Next
  20.         Do While .document.getElementById("statusMsg").Value = ""
  21.             DoEvents
  22.         Loop
  23.         sStatus = .document.getElementById("statusMsg").Value
  24.         If InStr(sStatus, "[執行成功]") <= 0 Then .Quit: MsgBox sStatus: Exit Sub
  25.                               
  26.         .document.body.innerHTML = .document.getElementById("queryResult").outerHTML
  27.         .execwb 17, 2       '  Select All
  28.         .execwb 12, 2       '  Copy selection
  29.         With ActiveSheet
  30.             .Cells.Clear
  31.             .[A1].Select
  32.             .PasteSpecial Format:="HTML"
  33.         End With
  34.         .Quit
  35.     End With
  36. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2013-11-19 08:05 編輯

回復 28# joey0415
有些有id與tagname 有些只有id ,但一定會有tagname
摘取網頁原使檔一段內容 <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="" />
getElementsBytagname("input")     '會是原使檔所有<input type=???  .....>  的集合
getElementsByName("__VIEWSTATE")  '唯一的 Name
getElementById("__VIEWSTATE")     '唯一的 Id
  1. Option Explicit
  2. Sub TEST11()
  3.     Dim x
  4.     With CreateObject("InternetExplorer.Application")
  5.         .Visible = True '是否顯示IE
  6.         .Navigate "http://www.cnyes.com/twstock/Institutional/1101.htm"
  7.         Do While .readyState <> 4: DoEvents: Loop
  8.         Set x = .document.getElementById("a_itrust")
  9.         x.Click ' 轉到 網頁: 'http://www.cnyes.com/twstock/itrust/1101.htm            '
  10.         Do While .readyState <> 4: DoEvents: Loop
  11.         ' .document.body.innerHTML = .document.getElementById("tabvl").outerHTML  '網頁文件元素沒這 Id="tabvl"
  12.         .document.body.innerHTML = .document.getElementsBytagname("table")(1).outerHTML  'tagname成員從 0 開始
  13.         .execwb 17, 2 'Select All
  14.         .execwb 12, 2 'Copy selection
  15.         ActiveSheet.[A1].Select
  16.         ActiveSheet.PasteSpecial Format:="HTML" ', NoHTMLFormatting:=True
  17.         .Quit
  18.     End With
  19. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2013-11-19 11:54 編輯

回復 32# joey0415
IQY 查詢檔的內容(副檔名為 IQY ,存於記事本,小作家.)
  1. WEB
  2. 1
  3. http://www.cnyes.com/twstock/itrust/1101.htm

  4. '投信進出網頁 'Selection => table的索引值 ,IQY文件由1開始算起, VBA程式 由0 開始算起
  5. Selection=2        

  6. Formatting=None
  7. PreFormattedTextToColumns=True
  8. ConsecutiveDelimitersAsOne=True
  9. SingleBlockTextImport=False
  10. DisableDateRecognition=False
  11. DisableRedirections=False
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2013-11-19 13:51 編輯

回復 34# joey0415
我把它放著沒有[重新整理成原來的畫面]
若要用上面的方法抓取時,最後不要quit,不quit要何作用.
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 稻穗結得越飽滿,越會往下垂,一個人越有成就,就要越有謙沖的胸襟。
返回列表 上一主題