教程中国
PHOTOSHOP CS9.0中文版 MAYA 8.5 FOR WINDOWS Corel Painter v9.0 Flash MX2004 中文版 Illustrator cs2 中文版
VC++6.0含sp6 中英文版 VB6.0 +sp6 简体中文版 Borland Delphi 7汉化版 MSDN for vb6.0中文版 Visual Studio 2005简体
教程中国下属 文件存储共享专家BIBIDU.COM 提供大型软件,教材,源码,电影,音乐,图书等下载 更多精品请点此进入
  您目前所在位置: 教程中国 >> .NET类 >> ASP.NET >> 在.NET环境下将报表数据导出EXCEL和WORD RSS订阅
在.NET环境下将报表数据导出EXCEL和WORD
教程(视频,书籍)下载:  ASP.NET AutoCAD 数据库 C# ASP java photoshop 网页设计 delphi 3dmax Flash C++ VB 张孝祥 实例   更多请进入BIBIDU搜索
IT搜索引擎   
在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

总结:Microsoft word 10.0(版本号)对象库提供了word的大部分操作。类似的也有Microsoft Excel 对象库,我们可以用代码与word和Excel进行会话并控制它们。还有很重要的一点,就是我们必须学会使用office软件的“宏”。“宏”是一系列的word(或其它office软件)命令和指令的组合,都是生成VB代码。我们可用“工具/宏/录制新宏”来录制“宏”,录制完成后查看“宏”的代码就可以知道实现此功能的一系列的VB代码,我们把这些代码拷贝到VB.net编辑器中,稍微改动后就可以使用。要想做好office开发,必须用好VBA和“宏”。


来源:upschool.com.cn
作者:
关键字:WORD
发表日期:2005-12-23

网页显示有限 阅读全文请下载本文完整版WORD文档

上一篇:VB.NET实现五子棋的人工智能(2)   下一篇:在.NET中应用MATLAB算法


本文的相类似文章
  • Word文档保护隐私的七招秘技
  • word文档加密漏洞破解方法详解
  • 怎样在VB中控制Word
  • 在Word中嵌入应用程序
  • VB 中调用 Word 拼写检查
  • 使用Word的拼写检查和拼写建议功能
  • 用vb将word文档(或其他的二进制数据)生成xml文件并互相转换
  • Word Power!
  • 用VB轻松打开MS Word文档
  • 在VB 中控制 Word
  • 网友评论 查看本文全部评论
    笔 名: *
    评 论:
    最多500字。当前字数:0
    联系方式:
    验证码:
    在学习中进步 在进步中成长 教程中国相随您的成长之路
    华腾联合科技股份有限公司版权所有
    广告联系:Rosibo@163.com