我正试图像这样使用NPOI:
private Stream RenderDataTableToExcel(DataTable SourceTable)
{
XSSFWorkbook workbook = null;
MemoryStream ms = null;
ISheet sheet = null;
XSSFRow headerRow = null;
try
{
workbook = new XSSFWorkbook();
ms = new MemoryStream();
sheet = workbook.CreateSheet();
headerRow = (XSSFRow)sheet.CreateRow(0);
foreach(DataColumn column in SourceTable.Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
int rowIndex = 1;
foreach(DataRow row in SourceTable.Rows)
{
XSSFRow dataRow = (XSSFRow)sheet.CreateRow(rowIndex);
foreach(DataColumn column in SourceTable.Columns)
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
++rowIndex;
}
for (int i = 0; i <= SourceTable.Columns.Count; ++i)
sheet.AutoSizeColumn(i);
workbook.Write(ms);
ms.Flush();
}
catch (Exception ex)
{
return null;
}
finally
{
ms.Close();
sheet = null;
headerRow = null;
workbook = null;
}
return ms;
}
private void DownloadExcel(DataTable dt, string reportName)
{
Stream s = RenderDataTableToExcel(dt);
if (s != null)
{
MemoryStream ms = s as MemoryStream;
Response.AddHeader("Content-Disposition", string.Format("attachment;filename=" + HttpUtility.UrlEncode(reportName) + DateTime.Now.ToString("yyyyMMdd") + ".xlsx"));
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
Response.BinaryWrite(ms.ToArray());
Response.Flush();
ms.Close();
ms.Dispose();
}
else
Response.Write("Error!Connot Download");
}
我有二进制流而不是ms-excel文件。
PS:我真的想知道如何生成一个文件供下载,也就是说,为什么你的代码有效,浏览器会生成文件还是服务器?
答案 0 :(得分:1)
NPOI是一个帮助模块,用于创建四个excel文件。这是在服务器端和内存中创建的。 ( workbook.Write(ms)在内存中写入excel文件)excel文件以字节[]的形式通过网络传输,浏览器根据文件和内容类型决定如何处理它。
当你使用经典的asp.net时,在你的aspx页面中添加一个链接,如下所示
<a target="_blank" href="Handler.ashx" >download...</a>
创建一个Handler.ashx并将CodeExcel的代码放入Handler.ashx的ProcessRequest中。
public void ProcessRequest (HttpContext context)
{
//create dumy data, or in youre case the data form somewhere else
DataTable table = new DataTable();
table.Columns.AddRange(new[]
{
new DataColumn("Name")
});
table.Rows.Add("david");
table.Rows.Add("Ruud");
// your code
Stream s = RenderDataTableToExcel(dt);
if (s != null)
{
MemoryStream ms = s as MemoryStream;
Response.AddHeader("Content-Disposition", string.Format("attachment;filename=" + HttpUtility.UrlEncode(reportName) + DateTime.Now.ToString("yyyyMMdd") + ".xlsx"));
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
Response.BinaryWrite(ms.ToArray());
Response.Flush();
ms.Close();
ms.Dispose();
}
else
Response.Write("Error!Connot Download");
}
}
在MVC中如下所示。
[HttpGet]
public ActionResult ExportToExcel(string reportName)
{
byte [] reportDocument = RenderDataTableToExcel().ToArray();
Response.StatusCode = (int)HttpStatusCode.OK;
return File(reportDocument, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", reportName);
}
答案 1 :(得分:1)
我使用过ajax TVT。
window.location.href = "?action=DownloadAll";
//$.ajax({
// url: "?action=DownloadAll",
// type: "get",
// success: function () { }
//});
答案 2 :(得分:0)
您可以使用一些非常易读的代码将DataTable转换为Excel工作表:
XLWorkbook wb = new XLWorkbook();
DataTable dt = GetDataTableOrWhatever();
wb.Worksheets.Add(dt,"WorksheetName");
它也会跑得超快!