Created
December 7, 2017 06:54
-
-
Save talkingdotnet/fbf05d889b6d96445cbda7d812bd5fa5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public async Task<IActionResult> OnPostExport() | |
| { | |
| string sWebRootFolder = _hostingEnvironment.WebRootPath; | |
| string sFileName = @"demo.xlsx"; | |
| string URL = string.Format("{0}://{1}/{2}", Request.Scheme, Request.Host, sFileName); | |
| FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName)); | |
| var memory = new MemoryStream(); | |
| using (var fs = new FileStream(Path.Combine(sWebRootFolder, sFileName), FileMode.Create, FileAccess.Write)) | |
| { | |
| IWorkbook workbook; | |
| workbook = new XSSFWorkbook(); | |
| ISheet excelSheet = workbook.CreateSheet("Demo"); | |
| IRow row = excelSheet.CreateRow(0); | |
| row.CreateCell(0).SetCellValue("ID"); | |
| row.CreateCell(1).SetCellValue("Name"); | |
| row.CreateCell(2).SetCellValue("Age"); | |
| row = excelSheet.CreateRow(1); | |
| row.CreateCell(0).SetCellValue(1); | |
| row.CreateCell(1).SetCellValue("Kane Williamson"); | |
| row.CreateCell(2).SetCellValue(29); | |
| row = excelSheet.CreateRow(2); | |
| row.CreateCell(0).SetCellValue(2); | |
| row.CreateCell(1).SetCellValue("Martin Guptil"); | |
| row.CreateCell(2).SetCellValue(33); | |
| row = excelSheet.CreateRow(3); | |
| row.CreateCell(0).SetCellValue(3); | |
| row.CreateCell(1).SetCellValue("Colin Munro"); | |
| row.CreateCell(2).SetCellValue(23); | |
| workbook.Write(fs); | |
| } | |
| using (var stream = new FileStream(Path.Combine(sWebRootFolder, sFileName), FileMode.Open)) | |
| { | |
| await stream.CopyToAsync(memory); | |
| } | |
| memory.Position = 0; | |
| return File(memory, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", sFileName); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment