导出EXCEL
DataSource.ResultData.ReportData = dv.ToTable(); HSSFWorkbook hssfworkbook = Manager2.ExportExcel(DataSource, ReportTitleString); using (MemoryStream ms = new MemoryStream()) { hssfworkbook.Write(ms); Response.Charset = "utf-8"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(excelName, System.Text.Encoding.UTF8) + ".xls"); Response.BinaryWrite(ms.ToArray()); Response.End(); }
public ActionResult ExportToExcel() { NPOI.SS.UserModel.IWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(); NPOI.SS.UserModel.ICell cell; NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("StressTest"); int i = 0; int rowLimit = 100; DateTime originalTime = DateTime.Now; for (i = 0; i < rowLimit; i++) { cell = sheet.CreateRow(i).CreateCell(0); cell.SetCellValue("值" + i.ToString()); } using (MemoryStream ms = new MemoryStream()) { workbook.Write(ms); var buffer = ms.GetBuffer(); ms.Close(); return File(buffer, "application/ms-excel", "test.xlsx"); } } <p><a href="@Url.Action("ExportToExcel2")">导出Excel</a></p>