'將所得到的表的列名,賦值給儲存格
Dim Col As DataColumn
Dim Row As DataRow
For Each Col In Table.Columns
colIndex = colIndex + 1
xlApp.Cells(1, colIndex) = Col.ColumnName
Next
'得到的表所有行,賦值給儲存格
For Each Row In Table.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each Col In Table.Columns
colIndex = colIndex + 1
xlApp.Cells(rowIndex, colIndex) = Row(Col.ColumnName)
Next
Next
With xlSheet
.Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Name = "黑體"
'設標題為黑體字
.Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Bold = True
'標題字型加粗
.Range(.Cells(1, 1), .Cells(rowIndex, colIndex)).Borders.LineStyle = 1
'設表格邊框樣式
End With
'將所得到的表的列名,賦值給儲存格
Dim Col As DataColumn
Dim Row As DataRow
For Each Col In Table.Columns
colIndex = colIndex + 1
oTable.Cell(1, colIndex).Range.InsertAfter(Col.ColumnName)
Next
'得到的表所有行,賦值給儲存格
For Each Row In Table.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each Col In Table.Columns
colIndex = colIndex + 1
oTable.Cell(rowIndex, colIndex).Range.InsertAfter(Row(Col.ColumnName))
Next
Next
總結:Microsoft Word 10.0(版本號碼)物件程式庫提供了Word的大部分操作。類似的也有Microsoft Excel 物件程式庫,我們可以用代碼與Word和Excel進行會話並控制它們。還有很重要的一點,就是我們必須學會使用OFFICE軟體的“宏”。“宏”是一系列的Word(或其它OFFICE軟體)命令和指令的組合,都是產生VB代碼。我們可用“工具/宏/錄製新宏”來錄製“宏”,錄製完成後查看“宏”的代碼就可以知道實現此功能的一系列的VB代碼,我們把這些代碼拷貝到VB.net編輯器中,稍微改動後就可以使用。要想做好OFFICE開發,必須用好VBA和“宏”。