2015-09-04 84 views
-1

我可以將元素內部文本刮到文本框中,然後我可以把它放到我的Excel表格中,但所有元素都在一個單元格「A1」中結束。我如何獲得Excel中列出的A1到A100的元素。示例將是一個很大的幫助,謝謝。VB.net刮和粘貼

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.All 

For Each curElement As HtmlElement In theElementCollection 

If curElement.GetAttribute("classname") = "dtstart time" Then 
TextBox1.Text = TextBox1.Text & curElement.InnerText & Environment.NewLine 
End If 

............................................ .................................................. ....... ........................................... ........

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 

Dim oExcel As Object 
Dim oBook As Object 
Dim oSheet As Object 


oExcel = CreateObject("Excel.Application") 
oBook = oExcel.workbooks.add 
oSheet = oBook.worksheets(1) 

oSheet.range("A1").value = TextBox1.Text 


oBook.saveas("C:\Users\garymcmanus\Desktop\test.xls") 
oBook.close() 

回答

1

假設你想保留它作爲兩個單獨的按鈕點擊,您需要通過他們分裂由換行符和循環文本框的內容,將每一行成不同的細胞。因此,而不是:

oSheet.range("A1").value = TextBox1.Text 

你會做:

Dim counter as Int32 = 1 
For Each str as String In TextBox1.Text.Split(Environment.NewLine) 
    oSheet.range("A" & counter).value = str 
    counter += 1 
Next 

另一種方法是這兩種方法結合起來,和而不將所有文本TextBox1.Text用換行,你就不是將它們插入到您的For Each curElement As HtmlElement In theElementCollection循環內的單元格。