在.NET环境下将报表导出Excel和word lihonggen0(原作)
在VB6开发环境下,本人使用Excel作过报表,在.NET环境下开发,本人使用水晶报表。但VB.NET同样可以将报表导出到Excel和word进行输出,制作出专业水平的报表。
具体操作如下:(注:首先需添加引用,选择COM-->选择Microsoft word 10.0 Object Library和Microsoft Excel 10.0 Object Library组件)
1.先创建一个DataTable,作为数据来源,也可以另将其它的数据源。
Private Function CreaTable() As DataTable
Dim dt As New DataTable()
dt.Columns.Add("列1", GetType(String))
dt.Columns.Add("列2", GetType(Integer))
dt.Columns.Add("列3", GetType(String))
dt.Columns.Add("列4", GetType(String))
Dim row, row1 As DataRow
row = dt.NewRow()
row!列1 = "行1"
row!列2 = 1
row!列3 = "d"
row!列4 = "a"
dt.Rows.Add(row)
row1 = dt.NewRow()
row1!列1 = "行2"
row1!列2 = 12
row1!列3 = "b"
row1!列4 = "c"
dt.Rows.Add(row1)
Return dt
End Function
2.将表中的内容导出到Excel
Dim xlApp As New Excel.Application()
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim rowIndex, colIndex As Integer
rowIndex = 1
colIndex = 0
xlBook = xlApp.Workbooks().Add
xlSheet = xlBook.Worksheets("sheet1")
Dim Table As New DataTable()
Table = CreaTable()
'将所得到的表的列名,赋值给单元格
Dim Col As DataColumn
Dim Row As DataRow
<