ClosedXML、DocumentFormat.OpenXml導出DataTable到Excel

在不少系統中都用到導出,使用過多種導出方式,以爲ClosedXML插件的導出簡單又方便。c++

而且ClosedXML、DocumentFormat.OpenXml都是MIT開源。瀏覽器

首先經過 Nuget 安裝 ClosedXML 插件,同時會自動安裝 DocumentFormat.OpenXml 插件。緩存

如下就是導出相關的代碼:app

一、MVC控制器中的導出this

// MVC 控制器中
/// <summary>
/// 導出 (不管是否須要返回值,都有 Response)
/// </summary>
public void Export1() // 傳入搜索條件
{
    // 一、根據條件查詢到想要的數據
    DataTable data = new DataTable();
    // 二、設置表名(對應sheet名)、列名(對應列名)
    data.TableName = "XX統計";
    // 三、列寬設置(須要慢慢設置)
    int[] colsWidth = new int[] { 160, 200, 300, 160 };
    // 四、生成導出文件
    byte[] filedata = ExportHelper.ExportExcel(data);
    // 五、輸出文件流
    HttpContext.Output(filedata, "XX統計_導出" + DateTime.Today.ShowDate() + ".xlsx");
}

二、生成導出文件spa

using ClosedXML.Excel;
using System.Data;
using System.IO;

/// <summary>
/// 導出Excel文件
/// </summary>
/// <param name="data">導出的數據</param>
/// <param name="colsWidth">列寬</param>
/// <returns></returns>
public static byte[] ExportExcel(DataTable data, int[] colsWidth = null)
{
    using (XLWorkbook workbook = new XLWorkbook())
    {
        IXLWorksheet worksheet = workbook.AddWorksheet(data.TableName);

        // 處理列
        for (int i = 0; i < data.Columns.Count; i++)
        {
            worksheet.Cell(1, i + 1).Value = data.Columns[i].ColumnName;
            worksheet.Cell(1, i + 1).Style.Font.Bold = true;
        }

        // 處理列寬
        if (colsWidth != null)
        {
            for (int j = 1; j <= colsWidth.Length; j++)
            {
                worksheet.Columns(j, j).Width = colsWidth[j - 1];
            }
        }

        // 處理數據
        int r = 2;// 第二行開始
        foreach (DataRow dr in data.Rows)
        {
            // 第一列開始
            for (int c = 1; c <= data.Columns.Count; c++)
            {
                worksheet.Cell(r, c).Value = data.Rows[r][c].ToString();
            }
            r++;
        }

        // 緩存到內存流,而後返回
        using (MemoryStream stream = new MemoryStream())
        {
            workbook.SaveAs(stream);
            return stream.ToArray();
        }
    }
}

三、輸出文件流插件

using System.Text;
using System.Text.RegularExpressions;
using System.Web;

/// <summary>
/// 輸出文件流
/// </summary>
/// <param name="httpContext">Http上下文</param>
/// <param name="filedata">文件數據</param>
/// <param name="fileName">文件名(要後綴名)</param>
public static void Output(this HttpContextBase httpContext, byte[] filedata, string fileName)
{
    //文件名稱效驗 文件名不能包含\/:*?<>| 其中\須要兩次轉義
    if (Regex.IsMatch(fileName, @"[\\/:*?<>|]"))
    {
        fileName = Regex.Replace(fileName, @"[\\/:*?<>|]", "_");
    }

    //判斷是否爲火狐瀏覽器(下載時,有些瀏覽器中文名稱亂碼)
    if (httpContext.Request.Browser.Browser != "Firefox")
    {
        fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8);
    }

    // 沒有定義類型,用二進制流類型的MIME
    string MIME = "application/octet-stream";
    httpContext.Response.Clear();
    httpContext.Response.Buffer = true; //該值指示是否緩衝輸出,並在完成處理整個響應以後將其發送
    httpContext.Response.ContentType = MIME;
    httpContext.Response.ContentEncoding = Encoding.UTF8;
    httpContext.Response.Charset = Encoding.UTF8.HeaderName;
    httpContext.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
    httpContext.Response.AddHeader("Accept-Language", "zh-cn");
    httpContext.Response.AddHeader("Content-Length", filedata.LongLength.ToString());
    httpContext.Response.BinaryWrite(filedata);
    httpContext.Response.Flush();
    httpContext.Response.End();
}

經過ClosedXML導出操做方便。code

相關文章
相關標籤/搜索