Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using NPOI.HSSF.UserModel;
- using NPOI.SS.UserModel;
- using NPOI.XSSF.UserModel;
- using System.IO;
- using System.Web.Mvc;
- namespace ExcelExportMVCLab.Controllers
- {
- public class ExcelExportController : Controller
- {
- /// <summary>
- /// 副檔名為xls請使用此
- /// </summary>
- public void ExportXls()
- {
- string fileName = "ExportData.xls";
- IWorkbook workbook = new HSSFWorkbook();
- ISheet sheet = workbook.CreateSheet("Sheet1");
- using (MemoryStream ms = new MemoryStream())
- {
- workbook.Write(ms);
- Response.Clear();
- Response.ContentType = "application/vnd.ms-excel";
- Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
- Response.BinaryWrite(ms.ToArray());
- Response.Flush();
- Response.End();
- workbook = null;
- }
- }
- /// <summary>
- /// 副檔名為xlsx請使用此
- /// </summary>
- public void ExportXlsx()
- {
- string fileName = "ExportData.xlsx";
- IWorkbook workbook = new XSSFWorkbook();
- ISheet sheet = workbook.CreateSheet("Sheet1");
- using (MemoryStream ms = new MemoryStream())
- {
- workbook.Write(ms);
- Response.Clear();
- Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
- Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
- Response.BinaryWrite(ms.ToArray());
- Response.Flush();
- Response.End();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement