public async Task <IActionResult> ExportToExcel() { ExcelPackage.LicenseContext = LicenseContext.NonCommercial; byte[] fileContents; var header = await _stockService.ExportToExcel(); var currentTenant = await _sessionAppService.GetCurrentLoginInformations(); using (var excel = new ExcelPackage()) { var ws = excel.Workbook.Worksheets.Add("Stocks"); ws.Cells["A1"].Value = currentTenant.Tenant.Name; ws.Cells["A5"].Value = "Prod_Code"; ws.Cells["B5"].Value = "Prod_Description"; ws.Cells["C5"].Value = "Prod_Brand"; ws.Cells["D5"].Value = "TotalPieces"; ws.Cells["E5"].Value = "PricePerPiece"; ws.Cells["F5"].Value = "Amount"; int rowStart = 6; foreach (var item in header) { ws.Cells[string.Format("A{0}", rowStart)].Value = item.ProductCode; ws.Cells[string.Format("B{0}", rowStart)].Value = item.ProductItemName; ws.Cells[string.Format("C{0}", rowStart)].Value = item.ProductBrand; ws.Cells[string.Format("D{0}", rowStart)].Value = item.TotalPieces; ws.Cells[string.Format("E{0}", rowStart)].Value = item.ProductPricePerPiece; ws.Cells[string.Format("F{0}", rowStart)].Value = item.Amount; rowStart++; } ws.Cells["A:AZ"].AutoFitColumns(); fileContents = excel.GetAsByteArray(); } if (fileContents == null || fileContents.Length == 0) { return(NotFound()); } return(File( fileContents: fileContents, contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileDownloadName: "Stocks.xlsx" )); }