在VB开发环境下本人使用EXCEL作过报表在NET环境下开发本人使用水晶报表但VBNET同样可以将报表导出到EXCEL和WORD进行输出制作出专业水平的报表
具体操作如下(注首先需添加引用选择COM选择Microsoft Word Object Library和Microsoft Excel Object Library组件)
先创建一个DataTable作为数据来源也可以另将其它的数据源
Private Function CreaTable() As DataTable
Dim dt As New DataTable()
dtColumnsAdd(列 GetType(String))
dtColumnsAdd(列 GetType(Integer))
dtColumnsAdd(列 GetType(String))
dtColumnsAdd(列 GetType(String))
Dim row row As DataRow
row = dtNewRow()
row!列 = 行
row!列 =
row!列 = d
row!列 = a
dtRowsAdd(row)
row = dtNewRow()
row!列 = 行
row!列 =
row!列 = b
row!列 = c
dtRowsAdd(row)
Return dt
End Function
将表中的内容导出到Excel
Dim xlApp As New ExcelApplication()
Dim xlBook As ExcelWorkbook
Dim xlSheet As ExcelWorksheet
Dim rowIndex colIndex As Integer
rowIndex =
colIndex =
xlBook = xlAppWorkbooks()Add
xlSheet = xlBookWorksheets(sheet)
Dim Table As New DataTable()
Table = CreaTable()
将所得到的表的列名赋值给单元格
Dim Col As DataColumn
Dim Row As DataRow
For Each Col In TableColumns
colIndex = colIndex +
xlAppCells( colIndex) = ColColumnName
Next
得到的表所有行赋值给单元格
For Each Row In TableRows
rowIndex = rowIndex +
colIndex =
For Each Col In TableColumns
colIndex = colIndex +
xlAppCells(rowIndex colIndex) = Row(ColColumnName)
Next
Next
With xlSheet
Range(Cells( ) Cells( colIndex))FontName = 黑体
设标题为黑体字
Range(Cells( ) Cells( colIndex))FontBold = True
标题字体加粗
Range(Cells( ) Cells(rowIndex colIndex))BordersLineStyle =
设表格边框样式
End With
With xlSheetPageSetup
LeftHeader = & Chr() & &楷体_GB常规&公司名称 & Gsmc
CenterHeader = &楷体_GB常规公司人员情况表&宋体常规 & Chr() & &楷体_GB常规&日 期
RightHeader = & Chr() & &楷体_GB常规&单位
LeftFooter = &楷体_GB常规&制表人
CenterFooter = &楷体_GB常规&制表日期
RightFooter = &楷体_GB常规&第&P页 共&N页
End With
xlAppVisible = True
将表中的内容导出到WORD
[] []