|
本帖最后由 X.Z. 于 2012-9-24 22:48 编辑
請問如何將 透過WebBrowser1抓到的網頁表格(TABLE) 寫入 xlsx檔案.
網路上找的絕大部分是 在EXCLE透過巨集把資料弄進去
但是在Visual basic 2010 要怎麼做呢? 謝謝!
只能寫到這樣..匯入資料卡住了
- Private Sub Button_AutoGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_AutoGet.Click
- Dim testUR As String = "file://d:/test.html"
- WebBrowser1.Navigate(testUR) ' 瀏覽網址
- While Not WebBrowser1.ReadyState = WebBrowserReadyState.Complete
- System.Windows.Forms.Application.DoEvents()
- System.Threading.Thread.Sleep(1000)
- End While
- Dim out_temp As String
- out_temp = WebBrowser1.Document.GetElementsByTagName("TABLE")(0).InnerHtml
- out_temp = Replace(out_temp, "<TBODY>", "<TABLE>")
- out_temp = Replace(out_temp, "</TBODY>", "</TABLE>")
- Dim xlApp As Application
- Dim xlBook As Workbook
- Dim xlSheet As Worksheet
- Dim xlRange As Range
- On Error Resume Next
- '#一部電腦僅執行一個Excel Application, 就算中突開啟Excel也不會影響程式執行
- '#在工作管理員中只會看見一個EXCEL.exe在執行,不會浪費電腦資源
- '#引用正在執行的Excel Application
- xlApp = GetObject(, "Excel.Application")
- '#若發生錯誤表示電腦沒有Excel正在執行,需重新建立一個新的應用程式
- If Err.Number() <> 0 Then
- Err.Clear()
- '#執行一個新的Excel Application
- xlApp = CreateObject("Excel.Application")
- If Err.Number() <> 0 Then
- MsgBox("電腦沒有安裝Excel")
- End
- End If
- End If
- xlBook = xlApp.Workbooks.Open("d:\testiso.xlsx")
- xlApp.DisplayAlerts = False '停用警告訊息
- xlApp.Visible = True '設置EXCEL對象可見
- xlBook.Activate() '設定活頁簿為焦點
- xlBook.Parent.Windows(1).Visible = True '顯示第一個子視窗
- xlSheet = xlBook.Worksheets(1) '引用第一個工作表
- xlSheet.Activate() '設定工作表為焦點
- System.Threading.Thread.Sleep(5000)
- '======================抄來的還但是不能用=============================
- '#用Array寫資料至EXCEL
- 'Dim myRows As Long, myCols As Long
- 'myRows = UBound(DataArray, 1) + 1
- 'myCols = UBound(DataArray, 2)
- 'xlSheet.Range(xlApp.Cells(2, 1), xlApp.Cells(myCols, myRows)).Select() '選擇儲存格範圍
- 'xlSheet.Range(xlApp.Cells(2, 1), xlApp.Cells(myCols, myRows)).Value = xlApp.Transpose(DataArray) '陣列一次寫入Excel
- 'xlApp.Run("Sample.xlt" & "!Caculater") '呼叫Excel的巨集
- '======================抄來的還但是不能用=============================
-
- xlBook.SaveAs("d:\testiso-1.xlsx") '另存新檔
- xlBook.Close() '關閉當前活頁簿EXCEL
- System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) 'xlApp.Quit()
- GC.Collect()
- End Sub
复制代码
html檔
- <TABLE>
- <TR class=TRH align=middle>
- <TD>需求編號
- <TD>申請<BR>部門
- <TD noWrap>申請人
- <TD>申請日期
- <TD>預計完成日
- <TD>需求型態
- <TD>需求類別
- <TD>需求主旨(摘要)
- <TD>狀態(負責人)</TD>
- <TD>實際<BR>工時 </TD>
- <TR class=TRB align=middle>
- <TD><A href="IRS001Detail.asp?irid=6MX1234561">6MX1234561</A></TD>
- <TD>MJ</TD>
- <TD>陳先生</TD>
- <TD>2012/9/21</TD>
- <TD>2012/9/21</TD>
- <TD align=left>網路,軟硬體</TD>
- <TD align=left>軟體維護</TD>
- <TD align=left>軟體維護</TD>
- <TD><SPAN style="COLOR: blue; CURSOR: hand" language=javascript onclick="window.open('../inc/BK_VIEW.asp?sType=Q&BRE_NO=IRS001&AUDIT_NO=IRSC04&KEY_NO_A=6MX1234561&KEY_NO_B=6MX1234561&TITLE_NAME=%B8%EA%B0T%BB%DD%A8D%B3%E6%BCf%AE%D6%B0O%BF%FD&KEY_NO_A_NAME=%B8%EA%B0T%BB%DD%A8D%B3%E6%B8%B9','BK_VIEW','scrollbars=yes,resizable=yes,top=0,width=790,height=550,left=0')"><U>驗收中(陳先生)</SPAN></U></TD>
- <TD>0.5 </TD>
- <TR class=TRA align=middle>
- <TD><A href="IRS001Detail.asp?irid=6M1234562">6MX1234562</A></TD>
- <TD>MJ</TD>
- <TD>陳小姐</TD>
- <TD>2012/9/21</TD>
- <TD>2012/9/25</TD>
- <TD align=left>網路,軟硬體</TD>
- <TD align=left>軟體維護</TD>
- <TD align=left>5F 會議室NB無法投影, 請...</TD>
- <TD><SPAN style="COLOR: blue; CURSOR: hand" language=javascript onclick="window.open('../inc/BK_VIEW.asp?sType=Q&BRE_NO=IRS001&AUDIT_NO=IRSC04&KEY_NO_A=6MX1234562&KEY_NO_B=6MX1234562&TITLE_NAME=%B8%EA%B0T%BB%DD%A8D%B3%E6%BCf%AE%D6%B0O%BF%FD&KEY_NO_A_NAME=%B8%EA%B0T%BB%DD%A8D%B3%E6%B8%B9','BK_VIEW','scrollbars=yes,resizable=yes,top=0,width=790,height=550,left=0')"><U>MS作業審查(王小姐)</SPAN></U></TD>
- <TD>0.3 </TD>
- </TABLE>
复制代码
查了不少資料了 但是塞不進去
[求助] 網頁導入excel問題,請幫忙看下這段代碼
http://club.excelhome.net/forum.php?mod=viewthread&tid=746743
[求助] 需要翻頁提取多頁網頁數據問題,謝謝浪板!
http://club.excelhome.net/thread-699704-1-1.html
【導出EXCEL彙編】asp.net 將數據導出到excel中,並設置其格式
http://blog.csdn.net/wonsoft/article/details/5966432
Microsoft.Office.Interop.Excel Namespace
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel%28v=office.11%29.aspx
[RESOLVED] Reference to non-shared member error
http://www.vbforums.com/showthread.php?656737-RESOLVED-Reference-to-non-shared-member-error
[Office][C#] NPOI、OpenXML SDK、OpenOffice.org SDK 寫入資料到 EXCEL 檔案
http://www.dotblogs.com.tw/chou/archive/2010/04/29/14912.aspx
[VB .NET] Microsoft.Office.Interop.Excel 讀 / 寫 Excel 語法概全
http://android2me.blogspot.tw/2012/05/vb-net-microsoftofficeinteropexcel.html
前輩請問,將html table輸出成Excel時,文數字表示法問題?
http://social.msdn.microsoft.com/Forums/zh-TW/238/thread/b8f2467e-ba05-4c9f-a718-db2490aa8631
如何快速地把一維陣列的內容填入Excel 的指定Range?
http://social.msdn.microsoft.com/Forums/zh-TW/805/thread/aba09ef7-0c98-4239-8f04-1437a52353ea
演練:創建您的第一個 Excel 應用程序級外接程序
http://msdn.microsoft.com/zh-cn/library/cc668205.aspx
|
|