中国站长下载-为中国站长提供最好最全的建站资源! 首 页发布资源有事留言繁體中文
设为首页
加入收藏
联系我们
 
您当前的位置:中国站长下载 -> 文章中心 -> 网页编程 -> .NET编程 -> 文章内容  虚拟主机 域名注册 退出登录 用户管理
栏目导航
· ASP编程 · .NET编程
· PHP编程 · JSP编程
· CGI 专区
热门文章
· sndvol32 - sndvol3...
· [组图] FLASH:《大话李白》...
· 个人网站到底能赚多...
· [图文] Rundll.exe是病毒吗...
· [组图] Flash:制作MV
· 价值12万元的网站SE...
· 网站创业者,你需要...
· 一个成功的网站设计...
· [图文] FLASH:韩国导航条解...
· 中国网站的赚钱模式...
相关文章
· 遭遇“Word文档杀手...
· [组图] 遭遇Word文档病毒的...
· [图文] 妙用Dreamweaver MX...
· [图文] Dreamweaver处理wor...
· ASP.Net实现将Word转...
· 在.NET 应用程序中用...
· 将Word文档转化为HT...
· 用ado.net对word,ex...
· 在.NET中使用脚本引...
· 在.NET中,将竖表变横...
在.NET环境下将报表导出Excel和Word
作者:不详  来源:不详  发布时间:2006-8-14 9:15:22  发布人:chinazhan

 减小字体 增大字体

     在VB.net同样可以将报表导出到Excel和Word进行输出,制作出专业水平的报表。具体操作如下:(注:首先需添加引用,选择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
  
  
    做人要厚道,请注明转自chinazhan中国站长(www.ChinaZhan.com)。

 
[] [返回上一页] [打 印] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
中国站长下载
中国站长下载

本页只接受PR>=4 IT类站点连接,申请连接,谢谢您们的支持!希望我们的下载站能够真正帮到中国的站长们!
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图
Copyright © 2005-2006 ChinaZhan.Net. All Rights Reserved .