这听起来很容易,但我是 vb 编程的新手。我有一个包含 11 行、2 列的文本文件,它们之间用 39 个空格分隔。现在我正在尝试读取此文件,将其复制并粘贴到 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.