熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

在.NET環境下將報表數據導出Excel和Word[1]

2013-11-13 12:16:12  來源: .NET編程 

  在VB開發環境下本人使用EXCEL作過報表NET環境下開發本人使用水晶報表但VBNET同樣可以將報表導出到EXCEL和WORD進行輸出制作出專業水平的報表

  具體操作如下(注首先需添加引用選擇COM選擇Microsoft Word Object Library和Microsoft Excel Object Library組件)

  先創建一個DataTable作為數據來源也可以另將其它的數據源

  Private Function CreaTable() As DataTable
  Dim dt As New DataTable()
  dtColumnsAdd( GetType(String))
  dtColumnsAdd( GetType(Integer))
  dtColumnsAdd( GetType(String))
  dtColumnsAdd( GetType(String))
  Dim row row As DataRow
  row = dtNewRow()
  row!列 =
  row!列 =
  row!列 = d
  row!列 = a
  dtRowsAdd(row)
  row = dtNewRow()
  row!列 =
  row!列 =
  row!列 = b
  row!列 = c
  dtRowsAdd(row)
  Return dt
  End Function

  將表中的內容導出到Excel

  Dim xlApp As New ExcelApplication()
  Dim xlBook As ExcelWorkbook
  Dim xlSheet As ExcelWorksheet
  Dim rowIndex colIndex As Integer
  rowIndex =
  colIndex =
  xlBook = xlAppWorkbooks()Add
  xlSheet = xlBookWorksheets(sheet)
  Dim Table As New DataTable()
  Table = CreaTable()
  將所得到的表的列名賦值給單元格
  Dim Col As DataColumn
  Dim Row As DataRow
  For Each Col In TableColumns
  colIndex = colIndex +
  xlAppCells( colIndex) = ColColumnName
  Next
  得到的表所有行賦值給單元格
  For Each Row In TableRows
  rowIndex = rowIndex +
  colIndex =
  For Each Col In TableColumns
  colIndex = colIndex +
  xlAppCells(rowIndex colIndex) = Row(ColColumnName)
  Next
  Next
  With xlSheet
  Range(Cells( ) Cells( colIndex))FontName = 黑體
  設標題為黑體字
  Range(Cells( ) Cells( colIndex))FontBold = True
  標題字體加粗
  Range(Cells( ) Cells(rowIndex colIndex))BordersLineStyle =
  設表格邊框樣式
  End With
  With xlSheetPageSetup
  LeftHeader = & Chr() & &楷體_GB常規&公司名稱 & Gsmc
  CenterHeader = &楷體_GB常規公司人員情況表&宋體常規 & Chr() & &楷體_GB常規&日 期
  RightHeader = & Chr() & &楷體_GB常規&單位
  LeftFooter = &楷體_GB常規&制表人
  CenterFooter = &楷體_GB常規&制表日期
  RightFooter = &楷體_GB常規&第&P頁 共&N頁
  End With
  xlAppVisible = True

  將表中的內容導出到WORD

[]  []  


From:http://tw.wingwit.com/Article/program/net/201311/15543.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.