'Imports System.Collections.Generic
'Imports System.Text
'Imports System.IO
'Imports office = Microsoft.Office.Core
'Imports word = Microsoft.Office.Interop.Word
Module Module1 Sub Main(ByVal args As String()) '这里的参数args是字符串数组,传递的是\bin\Debug\中的文本文件,可以传递多个文件
Dim theApplication As New Microsoft.Office.Interop.Word.Application '添加引用COM的“Microsoft Word 12.0 Object Library”
theApplication.Visible = True
Dim theDocument As Microsoft.Office.Interop.Word.Document
theDocument = theApplication.Documents.Add()
Dim reader As System.IO.TextReader '添加引用COM的“Microsoft Visual Basic for Applications Extensibility 5.3”
reader = New System.IO.StreamReader("woshi.txt") '原语句是reader = New System.IO.StreamReader(args(0))
'C:\Users\user\Documents\Visual Studio 2008\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\woshi.txt Dim separators() As String
separators() = "||"
Dim rowCount As Integer =
Dim columnCount As Integer = Dim rowList As New System.Collections.Generic.List(Of String)
Dim row As String = reader.ReadLine() While row IsNot Nothing
rowCount +=
rowList.Add(row) If rowCount = Then
Dim splitHeaderRow As String() = row.Split(separators, StringSplitOptions.None) columnCount = splitHeaderRow.Length -
End If row = reader.ReadLine()
End While Dim range As Microsoft.Office.Interop.Word.Range = theDocument.Range()
Dim table As Microsoft.Office.Interop.Word.Table = range.Tables.Add(range, rowCount, columnCount) Dim columnindex As Integer =
Dim rowindex As Integer = For Each r As String In rowList
Dim splitrow As String() = r.Split(separators, StringSplitOptions.None) For columnindex = To columnCount
Dim cell As Microsoft.Office.Interop.Word.Cell = table.Cell(rowindex, columnindex)
cell.Range.Text = splitrow(columnindex)
Next
rowindex +=
Next table.Rows().Range.Bold =
table.AutoFitBehavior(Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitContent) System.Console.WriteLine("Table complete.")
System.Console.ReadLine() theApplication.Quit(False) End Sub End Module