前往顾页
以后地位: 主页 > 收集编程 > .Net实例教程 >

在.NET环境下将报表导出Excel和Word法度代码

时候:2012-05-20 00:22来源:知行网www.zhixing123.cn 编辑:麦田守望者

在VB.net一样可以将报表导出到ExcelWord进行输入,制作出专业程度的报表。详细操纵以下:(注:起首需增加援引,挑选COM-->挑选Microsoft Word Object Library和Microsoft Excel Object Library组件)

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 As Integer = 1
Dim colIndex As Integer = 0
xlBook = xlApp.Workbooks().Add
xlSheet = xlBook.Worksheets("sheet1")

Dim Table As New DataTable()
Table = CreaTable()

'将所获得的表的列名,赋值给单位格

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

With xlSheet.PageSetup
.LeftHeader = "" & Chr(10) & "&""楷体_GB2312,通例""&10公司称呼:" ' & Gsmc
.CenterHeader = "&""楷体_GB2312,通例""公司职员环境表&""宋体,通例""" & Chr(10) &_
"&""楷体_GB2312,通例""&10日 期:"
.RightHeader = "" & Chr(10) & "&""楷体_GB2312,通例""&10单位:"
.LeftFooter = "&""楷体_GB2312,通例""&10制表人:"
.CenterFooter = "&""楷体_GB2312,通例""&10制表日期:"
.RightFooter = "&""楷体_GB2312,通例""&10第&P页 共&N页"
End With
xlApp.Visible = True

'3.将表中的内容导出到WORD

Dim wordApp As New Word.Application()
Dim myDoc As Word.Document
Dim oTable As Word.Table
Dim rowIndex, colIndex As Integer
rowIndex = 1
colIndex = 0
wordApp.Documents.Add()
myDoc = wordApp.ActiveDocument

Dim Table As New DataTable()
Table = CreaTable()
oTable = myDoc.Tables.Add(Range:=myDoc.Range(Start:=0, End:=0), _
NumRows:=Table.Rows.Count + 1, NumColumns:=Table.Columns.Count)
'将所获得的表的列名,赋值给单位格
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
oTable.Borders.InsideLineStyle = 1
oTable.Borders.OutsideLineStyle = 1
wordApp.Visible = True

 

------分开线----------------------------
标签(Tag):C# C#实例教程 c#根本教程 C#源代码 c#技能
------分开线----------------------------
保举内容
猜你感兴趣