zoukankan      html  css  js  c++  java
  • 在.NET环境下将报表导出Excel和Word

    在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

    1,此类用于将DataTable中的数据导出到Excel文件中,并由指定的Excel摩板规定固定的输出格式,用来得到规定的样式文件。
    2,导出文件可以是Excel文件或者XML文件。
    3,作为摩板的Excel文件要求有宏“MainMacro”,来处理DataTable中的数据。

     1using System;
     2using System.Data;
     3
     4namespace ExportFile
     5{
     6
     7    public class DataExport
     8    {
     9    
    10        public static bool ToExcel(DataTable datatable,string[] header,string templatefilename,string filename,string filepath) {
    11            return GenerateFile(datatable,header,templatefilename,filename,filepath,"Excel",true);
    12        }

    13
    14        public static bool ToExcel(DataTable datatable,string templatefilename,string filename,string filepath) {
    15            return GenerateFile(datatable,null,templatefilename,filename,filepath,"Excel",false);
    16        }

    17
    18        public static bool ToXML(DataTable datatable,string[] header,string templatefilename,string filename,string filepath) {
    19            return GenerateFile(datatable,header,templatefilename,filename,filepath,"XML",true);
    20        }

    21
    22        private static bool GenerateFile(DataTable datatable,string[] Header,string templatefilename,string filename,
    23                                        string filepath,string filetype,bool hasheader){
    24            Excel.ApplicationClass oExcel = new Excel.ApplicationClass();
    25            oExcel.Visible = false;
    26            oExcel.DisplayAlerts = false;
    27            Excel.Workbooks oBooks = oExcel.Workbooks;
    28            Excel._Workbook oBook = null;
    29            object oMissing = System.Reflection.Missing.Value;
    30            
    31            try{
    32                oBook = oBooks.Open(templatefilename,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,
    33                    oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing);
    34
    35                string[,] Content = ConvertDataToArray(datatable);
    36
    37                oExcel.Run("MainMacro",Content, Header,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,
    38                    oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,
    39                    oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing); 
    40
    41                string strSaveFile = filepath + filename;
    42
    43                if(filetype == "Excel"){
    44                    oBook.SaveAs(strSaveFile,oMissing,oMissing,oMissing,oMissing,oMissing,Excel.XlSaveAsAccessMode.xlShared,oMissing,oMissing,oMissing,
    45                        oMissing,oMissing);
    46                }

    47                else if(filetype == "XML"){
    48                    oBook.SaveAs(strSaveFile,Excel.XlFileFormat.xlXMLSpreadsheet,oMissing,oMissing,oMissing,oMissing,Excel.XlSaveAsAccessMode.xlNoChange,
    49                        Excel.XlSaveConflictResolution.xlOtherSessionChanges,oMissing,oMissing,oMissing,oMissing);
    50                }

    51            }

    52            catch(Exception){
    53                return false;
    54            }

    55            finally{
    56                if(oBook != null){
    57                    oBook.Close(false,oMissing,oMissing);
    58                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook);
    59                    oBook = null;
    60                }

    61                if(oBooks != null){
    62                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks);
    63                    oBooks = null;
    64                }

    65                if(oExcel != null){
    66                    oExcel.Quit();
    67                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel);
    68                    oExcel = null;
    69                }

    70                GC.Collect();
    71            }

    72            return true;
    73        }

    74
    75        private static string[,] ConvertDataToArray(DataTable datatable){
    76
    77            int rowCount = datatable.Rows.Count;
    78            int columnCount = datatable.Columns.Count;
    79            string[,] arrData = new string[rowCount,columnCount];
    80
    81            for(int i = 0 ;i< rowCount ;i++{
    82                for(int j = 0;j < columnCount ; j++ ) {
    83                    arrData[i,j] = Convert.ToString(datatable.Rows[i][j]);
    84                }

    85            }
     
    86            return arrData;
    87        }

    88
    89    }

    90}
    转自&#160; http://dotnet.aspx.cc/ShowDetail.aspx?id=6AFBF00B-459D-4642-AD14-8A4765FFAFCC
  • 相关阅读:
    更改Tomcat startup.bat启动窗口名称
    java 启用新线程异步调用
    [转]jquery中使用event.target的几点
    Linux开启相关端口及查看已开启端口
    【转】eclipse插件:OpenExplorer快速打开文件目录
    bootbox.js [v4.2.0]设置确认框 按钮语言为中文
    【转】eclipse使用git提交到osc
    使用RMAN恢复数据库
    来一篇最全的自动化运维部署文档
    (转)linux 内存管理——内核的shmall 和shmmax 参数
  • 原文地址:https://www.cnblogs.com/sutengcn/p/301650.html
Copyright © 2011-2022 走看看