public async Task <ActionResult> ExportExcelListAspose([FromQuery] PaginationParams param, string text, int checkExport, string productCateID, string productName, int checkSearch) { PageListUtility <Product_Dto> data; if (checkSearch == 1) { data = await _productService.GetProductWithPaginations(param, text, false); } else { data = await _productService.SearchProductWithPaginations(param, productCateID, productName, false); } var path = Path.Combine(_webHostEnvironment.ContentRootPath, "Resources\\Template\\Product\\ProductListTemplate.xlsx"); WorkbookDesigner designer = new WorkbookDesigner(); designer.Workbook = new Workbook(path); Cell cell = designer.Workbook.Worksheets[0].Cells["A1"]; Worksheet ws = designer.Workbook.Worksheets[0]; designer.SetDataSource("result", data.Result); designer.Process(); Style styleDecimal = ws.Cells["G2"].GetStyle(); styleDecimal.Custom = "0.00"; Style styleDateTime = ws.Cells["J2"].GetStyle(); styleDateTime.Custom = "dd/MM/yyyy hh:mm:ss"; for (int i = 1; i <= data.Result.Count; i++) { ws.AutoFitRow(i); ws.Cells["G" + (i + 1)].SetStyle(styleDecimal); ws.Cells["J" + (i + 1)].SetStyle(styleDateTime); } int index = 2; foreach (var item in data.Result) { string fileNew = _dropzoneService.CheckTrueFalse(item.New); string fileIsSale = _dropzoneService.CheckTrueFalse(item.IsSale); string fileHotSale = _dropzoneService.CheckTrueFalse(item.Hot_Sale); string fileStatus = _dropzoneService.CheckTrueFalse(item.Status); Aspose.Cells.Drawing.Picture iconNew = ws.Pictures[ws.Pictures.Add(1, 2, fileNew)]; Aspose.Cells.Drawing.Picture iconIsSale = ws.Pictures[ws.Pictures.Add(1, 3, fileIsSale)]; Aspose.Cells.Drawing.Picture iconHotSale = ws.Pictures[ws.Pictures.Add(1, 4, fileHotSale)]; Aspose.Cells.Drawing.Picture iconStatus = ws.Pictures[ws.Pictures.Add(1, 5, fileStatus)]; iconNew.Height = iconIsSale.Height = iconHotSale.Height = iconStatus.Height = 20; iconNew.Width = iconIsSale.Width = iconHotSale.Width = iconStatus.Width = 20; iconNew.Top = iconIsSale.Top = iconHotSale.Top = iconStatus.Top = 5; iconNew.Left = iconIsSale.Left = iconHotSale.Left = iconStatus.Left = 40; ws.Cells.SetRowHeight(index - 1, 22.5); index++; } MemoryStream stream = new MemoryStream(); string fileKind = ""; string fileExtension = ""; if (checkExport == 1) { designer.Workbook.Save(stream, SaveFormat.Xlsx); fileKind = "application/xlsx"; fileExtension = ".xlsx"; } if (checkExport == 2) { // custom size ( width: in, height: in ) ws.PageSetup.FitToPagesTall = 0; ws.PageSetup.SetHeader(0, "&D &T"); ws.PageSetup.SetHeader(1, "&B Article"); ws.PageSetup.SetFooter(0, "&B SYSTEM BY MINH HIEU"); ws.PageSetup.SetFooter(2, "&P/&N"); ws.PageSetup.PrintQuality = 1200; designer.Workbook.Save(stream, SaveFormat.Pdf); fileKind = "application/pdf"; fileExtension = ".pdf"; } byte[] result = stream.ToArray(); return(File(result, fileKind, "Product_List_" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + fileExtension)); }
public async Task <ActionResult> ExportExcelListAspose([FromQuery] PaginationParams param, string text, int checkExport, string articleCateID, string articleName, int checkSearch) { PageListUtility <Article_Dto> data; if (checkSearch == 1) { data = await _articleService.GetArticleWithPaginations(param, text, false); } else { data = await _articleService.SearchArticleWithPaginations(param, articleCateID, articleName, false); } var path = Path.Combine(_webHostEnvironment.ContentRootPath, "Resources\\Template\\Article\\ArticleListTemplate.xlsx"); WorkbookDesigner designer = new WorkbookDesigner(); designer.Workbook = new Workbook(path); Cell cell = designer.Workbook.Worksheets[0].Cells["A1"]; Worksheet ws = designer.Workbook.Worksheets[0]; designer.SetDataSource("result", data.Result); designer.Process(); Style style = ws.Cells["F2"].GetStyle(); style.Custom = "dd/MM/yyyy hh:mm:ss"; for (int i = 1; i <= data.Result.Count; i++) { ws.Cells["F" + (i + 1)].SetStyle(style); } int index = 1; foreach (var item in data.Result) { if (item.Content.Length > 70) { ws.AutoFitRow(index); } else { ws.Cells.SetRowHeight(index, 22.5); } string file = _dropzoneService.CheckTrueFalse(item.Status); Aspose.Cells.Drawing.Picture pic = ws.Pictures[ws.Pictures.Add(index, 3, file)]; pic = await StyleImageCheck(pic); index++; } MemoryStream stream = new MemoryStream(); string fileKind = ""; string fileExtension = ""; if (checkExport == 1) { designer.Workbook.Save(stream, SaveFormat.Xlsx); fileKind = "application/xlsx"; fileExtension = ".xlsx"; } if (checkExport == 2) { // custom size ( width: in, height: in ) ws.PageSetup.FitToPagesTall = 0; ws.PageSetup.SetHeader(0, "&D &T"); ws.PageSetup.SetHeader(1, "&B Article"); ws.PageSetup.SetFooter(0, "&B SYSTEM BY MINH HIEU"); ws.PageSetup.SetFooter(2, "&P/&N"); ws.PageSetup.PrintQuality = 1200; designer.Workbook.Save(stream, SaveFormat.Pdf); fileKind = "application/pdf"; fileExtension = ".pdf"; } byte[] result = stream.ToArray(); return(File(result, fileKind, "Article_List_" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + fileExtension)); }