【发布时间】:2015-02-08 08:10:57
【问题描述】:
我正在使用 EPPlus 库来创建 excel 文件。这是我正在使用的代码
public static byte[] CreateExcelFile(DataTable dataTable)
{
using (ExcelPackage pck = new ExcelPackage())
{
//Create the worksheet
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Sheet1");
//Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
ws.Cells["A1"].LoadFromDataTable(dataTable, true);
//Format the header for column A1:AZ1
using (ExcelRange rng = ws.Cells["A1:AZ1"])
{
rng.Style.Font.Bold = true;
}
////Example how to Format Column 1 as numeric
//using (ExcelRange col = ws.Cells[2, 1, 2 + dataTable.Rows.Count, 1])
//{
// col.Style.Numberformat.Format = "#,##0.00";
// col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
//}
return pck.GetAsByteArray();
}
}
然后我使用此内容类型生成一个响应:“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”
文件正在下载,但类型未知。如果将文件重命名为 a.xlsx,它将打开 excel 文件。
我使用的是旧版本,然后我用最新版本更新了 dll,但问题仍然存在。有任何想法吗 ?
【问题讨论】: