這聽起來很簡單,但我是vb編程的新手。我有一個11行的文本文件,兩列之間用39個空格隔開。現在我試圖讀取這個文件,將它複製到excel表格中&粘貼。下面是代碼,我迄今爲止:使用VB 2010 Express將文本文件粘貼到excel中
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oExcel As Object
Dim oBook As Object
Dim oRow As Int16 = 0
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
'Read input .txt file line-by-line, Copy to Clipboard & Paste to Excel
Using rdr As New System.IO.StreamReader("C:\Temp\ONI.txt")
Do While rdr.Peek() >= 0
Dim InputLine As String = rdr.ReadLine
oRow = oRow + 1
System.Windows.Forms.Clipboard.SetDataObject (InputLine)
oBook.Worksheets(1).Range("A" + oRow.ToString).Select()
oBook.Worksheets(1).Paste()
Loop
rdr.Close()
End Using
oExcel.Visible = True
'oExcel.SaveAs("C\Temp\test.xls")
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
End Sub
End Class
這是工作於Excel工作簿被打開&數據在A1得到粘貼到A11即各行是程度11(這是確定),但是該列僅爲1(它應該在列A & B)。我知道這很容易,請指導我。
此外代碼停在「另存爲」行(我已評論)。運行時顯示錯誤Public member 'SaveAs' on type 'ApplicationClass' not found.
對於另存爲,看到這個鏈接: http://www.siddharthrout.com/2012/09/12/saving-and-closing-the-excel-file-savesave-as-method/ 關於粘貼,文本將被粘貼在欄目A中。您將不得不使用「文本到列」來分隔數據對於此鏈接 http://www.siddharthrout.com/2012/06/ 29/excel-text-to-columns-from-vb-net/ –
謝謝悉達思!兩個問題都解決了你是真正的幫助引導我到你的博客。 – slyclam