• DataTable 导出到Excel


    代码如下:

     
    #region DataTable 导出到Excel
    /// Author: jy Wang
    /// Date: 2008-7-28
    /// <summary>
    /// DataTable 导出到Excel
    /// </summary>
    /// <param name="colName">列名数组</param>
    /// <param name="colWidth">列宽数组</param>
    /// <param name="reprotTitle">报表标题</param>
    /// <param name="exprortFile">导出文件名</param>
    /// <param name="logoPosition">图标的位置</param>
    /// <param name="table">要导出的datatable</param>
    /// <param name="response">HttpResponse</param>
    public void Export(string[] colName, int[] colWidth, string reprotTitle, string exprortFile, string logoPosition, System.Data.DataTable table, HttpResponse response, HttpRequest request)
    {
    string fileName = "" + exprortFile + ".xls";//客户端保存的文件名 
    string filePath = request.PhysicalApplicationPath +"Exported\\" + exprortFile + ".xls";//路径 

    if (System.IO.File.Exists(filePath))
    {
    System.IO.File.Delete(filePath);
    }

    GC.Collect();
    Excel.Application excel = new Excel.Application();
    Excel.Workbook book = excel.Workbooks.Add(Missing.Value);
    Excel.Worksheet sheet = (Excel.Worksheet)book.ActiveSheet;
    // 
    //取得标题 
    // 
    for (int col = 0; col < table.Columns.Count; col++)
    {

    sheet.Cells[5, col + 1] = colName[col];
    sheet.get_Range(excel.Cells[5, col + 1], excel.Cells[5, col + 1]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter; //设置标题格式为居中对齐 
    sheet.get_Range(excel.Cells[5, col + 1], excel.Cells[5, col + 1]).Interior.ColorIndex = 15; //设置为浅灰色,共计有56种 
    sheet.get_Range(excel.Cells[5, col + 1], excel.Cells[5, col + 1]).ColumnWidth = colWidth[col];
    sheet.get_Range(excel.Cells[5, col + 1], excel.Cells[5, col + 1]).RowHeight = 25;
    }

    // 
    //取得数据 
    // 

    for (int row = 0; row < table.Rows.Count; row++)
    {
    for (int col = 0; col < table.Columns.Count; col++)
    {
    sheet.Cells[row + 7, col + 1] = table.Rows[row][col].ToString();
    }
    }

    // 
    //取得整个报表的标题 
    // 
    excel.Cells[2, 5] = reprotTitle;
    // 
    //设置整个报表的标题格式 
    // 
    sheet.get_Range(excel.Cells[2, 5], excel.Cells[2, 5]).Font.Bold = true;
    sheet.get_Range(excel.Cells[2, 5], excel.Cells[2, 5]).Font.Size = 22;


    //设置内容字体
    sheet.get_Range(excel.Cells[5, 1], excel.Cells[table.Rows.Count + 6, table.Columns.Count + 1]).Font.Size = 9;
    sheet.get_Range(excel.Cells[5, 1], excel.Cells[5, table.Columns.Count + 1]).Font.Bold = true;

    //插入图标
    try
    {
    sheet.Shapes.AddPicture(logoPosition, Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoTriState.msoCTrue, 100, 5, 60, 60);
    }
    catch (Exception ee)
    {
    throw new Exception("Summary Report: 插入图标失败 logoPosition::" + logoPosition +"-"+ ee.Message);

    }
    // 
    //自动换行 
    // 
    sheet.get_Range(excel.Cells[6, 1], excel.Cells[table.Rows.Count + 6, table.Columns.Count]).Select();
    sheet.get_Range(excel.Cells[6, 1], excel.Cells[table.Rows.Count + 6, table.Columns.Count]).WrapText = true;

    // 
    //绘制边框 
    // 
    sheet.get_Range(excel.Cells[5, 1], excel.Cells[table.Rows.Count + 6, table.Columns.Count]).Borders.LineStyle = 1;
    sheet.get_Range(excel.Cells[5, 1], excel.Cells[table.Rows.Count + 6, table.Columns.Count]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;//设置左边线加粗 
    sheet.get_Range(excel.Cells[5, 1], excel.Cells[table.Rows.Count + 6, table.Columns.Count]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;//设置上边线加粗 
    sheet.get_Range(excel.Cells[5, 1], excel.Cells[table.Rows.Count + 6, table.Columns.Count]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;//设置右边线加粗 
    sheet.get_Range(excel.Cells[5, 1], excel.Cells[table.Rows.Count + 6, table.Columns.Count]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;//设置下边线加粗 


    //文件保存地点
    book.Close(true, filePath, Missing.Value);
    excel.Quit();
    GC.Collect();

    System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
    response.Clear();
    response.ClearContent();
    response.ClearHeaders();
    response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
    response.AddHeader("Content-Length", fileInfo.Length.ToString());
    response.AddHeader("Content-Transfer-Encoding", "binary");
    response.ContentType = "application/octet-stream";
    response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
    response.WriteFile(fileInfo.FullName);
    response.Flush();
    System.IO.File.Delete(filePath);
    response.End();
    }
    #endregion
     
     
    代码结束。
  • 相关阅读:
    HTTP Status 404
    The error occurred while setting parameters--索引 3 超出范围 sqlserver2008
    hessian不能注入dao的问题解决
    怎么 才能显示Eclipse中Console的全部内容
    java mvc web 项目web.xml头改错了,死活加载不上springMvc的jar
    如何理解andriod中的View和framelayout两个概念
    Type Project has no default.properties file! Edit the project properties to set one.
    shell获取目录下(包括子目录)所有文件名、路径、文件大小
    找出1小时内占用cpu最多的10个进程的shell脚本
    awk统计命令(求和、求平均、求最大值、求最小值)
  • 原文地址:https://www.cnblogs.com/puzi0315/p/2603992.html
Copyright © 2020-2023  润新知