首页 > 代码库 > 导出EXCEL

导出EXCEL

            DataSource.ResultData.ReportData =http://www.mamicode.com/ 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="http://www.mamicode.com/@Url.Action("ExportToExcel2")">导出Excel</a></p>

  来源:http://www.lanhusoft.com/Article/323.html

导出EXCEL