在.NET环境下将报表导出EXCEL和WORD(原作lihonggen0)

webcool 2003-03-20 09:33:30


在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

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

...全文
55 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
风影2000 2003-08-02
  • 打赏
  • 举报
回复
怎么释放资源呢。
hgc4 2003-04-22
  • 打赏
  • 举报
回复
写Word 时,出错:无法创建 ActiveX 组件。

怎么解决
gengwei80 2003-04-22
  • 打赏
  • 举报
回复
学习~
youngby 2003-04-22
  • 打赏
  • 举报
回复
学习~
wei131420 2003-04-22
  • 打赏
  • 举报
回复
是阿,.net操作Excel Word就是不会自动释放资源
kals 2003-04-07
  • 打赏
  • 举报
回复
你能否保证在每一台机器上都能正确释放资源?
xiamiwagua 2003-04-03
  • 打赏
  • 举报
回复
能给个c#的看看吗
anna_aiya 2003-03-21
  • 打赏
  • 举报
回复
xuexi
voyager 2003-03-21
  • 打赏
  • 举报
回复
不错,建议给你加分
garth 2003-03-20
  • 打赏
  • 举报
回复
学习

62,254

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧