public async Task <MemoryStream> BuildCSV(AllContactViewModel data) { string sWebRootFolder = _hostingEnvironment.WebRootPath; string sFileName = @"ContactList.xlsx"; 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("ContactList"); IRow row = excelSheet.CreateRow(0); row.CreateCell(0).SetCellValue("Full Name"); row.CreateCell(1).SetCellValue("Nick Name"); row.CreateCell(2).SetCellValue("Phone"); row.CreateCell(3).SetCellValue("Address"); row.CreateCell(4).SetCellValue("Website"); row.CreateCell(5).SetCellValue("Birth Date"); var i = 1; foreach (People person in data.MyContacts) { string phones = ""; foreach (var phone in person.Phones) { phones += phone.Number.ToString() + " "; } row = excelSheet.CreateRow(i); row.CreateCell(0).SetCellValue(person.FullName); row.CreateCell(1).SetCellValue(person.NickName); row.CreateCell(2).SetCellValue(phones); row.CreateCell(3).SetCellValue(person.Address); row.CreateCell(4).SetCellValue(person.WebSite); row.CreateCell(5).SetCellValue(person.BirthDate.Date.ToString("yyyy/MM/dd")); i++; } workbook.Write(fs); using (var stream = new FileStream(Path.Combine(sWebRootFolder, sFileName), FileMode.Open)) { await stream.CopyToAsync(memory); } memory.Position = 0; } return(memory); throw new NotImplementedException(); }
public async Task <IActionResult> Index() { var currentUser = await _userManager.GetUserAsync(User); if (currentUser == null) { return(Challenge()); } var data = await _contactServices.GetAllContactsAsync(currentUser.Id); var model = new AllContactViewModel() { MyContacts = data }; return(View(model)); }
public async Task <IActionResult> DownloadCSV() { var currentUser = await _userManager.GetUserAsync(User); if (currentUser == null) { return(Challenge()); } var data = await _contactServices.GetAllContactsAsync(currentUser.Id); var model = new AllContactViewModel() { MyContacts = data }; var memory = new MemoryStream(); memory = await _contactServices.BuildCSV(model); return(File(memory, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", @"ContactList.xlsx")); }