使用EPPlus将DataTable导出到Excel


68

我想使用EPPlus将数据表导出到Excel文件。该数据表的属性为int类型,因此我想要Excel文件中的格式相同。

有谁知道将这样的数据表导出到Excel的方法?

Answers:


141
using (ExcelPackage pck = new ExcelPackage(newFile))
{
  ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Accounts");
  ws.Cells["A1"].LoadFromDataTable(dataTable, true);
  pck.Save();
}

那应该为您解决问题。如果将字段定义为int,则EPPlus会将列正确转换为数字或浮点数。


2
导出时如何格式化数据表头?
Abhinav 2015年

1
是。这很好。但是当数据表包含十万行时。这是行不通的。
thevan 2016年

3
也许您应该针对该特定情况提出一个问题?
bastianwegge '16

标头将包含在@AbhinavRastogi
Helder Gurgel

使用OfficeOpenXml; (需要参考ExcelPackage / ExcelWorksheet)
B.Hawkins

18

以及是否要在浏览器响应中下载

Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode("Logs.xlsx", System.Text.Encoding.UTF8));

using (ExcelPackage pck = new ExcelPackage())
{
    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Logs");
    ws.Cells["A1"].LoadFromDataTable(dt, true);                 
    var ms = new System.IO.MemoryStream();
    pck.SaveAs(ms);
    ms.WriteTo(Response.OutputStream);                          
}

感谢您的摘要!无需进行urlencode“Logs.xlsx” :)
安德斯·林登

4

要在浏览器中下载excelsheet,请使用HttpContext.Current.ResponseResponse否则会出现Response is not available in this context.错误。这是我的代码

public void ExporttoExcel(DataTable table, string filename)
{
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.Buffer = true;
    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridData.xlsx");


    using (ExcelPackage pack = new ExcelPackage())
    {
        ExcelWorksheet ws = pack.Workbook.Worksheets.Add(filename);
        ws.Cells["A1"].LoadFromDataTable(table, true);
        var ms = new System.IO.MemoryStream();
        pack.SaveAs(ms);
        ms.WriteTo(HttpContext.Current.Response.OutputStream); 
    }

    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.End();

}

对于任何获取“ System.Web.HttpContextBase”的人都不包含“当前”的定义:要获取对HttpContext.Current的引用,您需要将HttpContext.Current替换为System.Web.HttpContext.Current stackoverflow.com/questions/19431820/ …
雷·科伦

1

以下是将DataSet导出到Excel的代码段:

    private static void DataSetToExcel(DataSet dataSet, string filePath)
    {
        using (ExcelPackage pck = new ExcelPackage())
        {
            foreach (DataTable dataTable in dataSet.Tables)
            {
                ExcelWorksheet workSheet = pck.Workbook.Worksheets.Add(dataTable.TableName);
                workSheet.Cells["A1"].LoadFromDataTable(dataTable, true);
            }

            pck.SaveAs(new FileInfo(filePath));
        }
    }

并使用语句:

using OfficeOpenXml;
using System.Data;
using System.IO;

0

这是epplus单元格的一些数字格式

//integer (not really needed unless you need to round numbers, Excel will use default cell properties)
ws.Cells["A1:A25"].Style.Numberformat.Format = "0";

//integer without displaying the number 0 in the cell
ws.Cells["A1:A25"].Style.Numberformat.Format = "#";

//number with 1 decimal place
ws.Cells["A1:A25"].Style.Numberformat.Format = "0.0";

//number with 2 decimal places
ws.Cells["A1:A25"].Style.Numberformat.Format = "0.00";

//number with 2 decimal places and thousand separator
ws.Cells["A1:A25"].Style.Numberformat.Format = "#,##0.00";

//number with 2 decimal places and thousand separator and money symbol
ws.Cells["A1:A25"].Style.Numberformat.Format = "€#,##0.00";

//percentage (1 = 100%, 0.01 = 1%)
ws.Cells["A1:A25"].Style.Numberformat.Format = "0%";

//accounting number format
ws.Cells["A1:A25"].Style.Numberformat.Format = "_-$* #,##0.00_-;-$* #,##0.00_-;_-$* \"-\"??_-;_-@_-";
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.