/// <summary> /// Tạo excel nhiều sheet /// </summary> /// <param name="excelInfos">Danh sách sheet excel</param> /// <param name="newFileName">Tên file mới</param> /// <param name="message">Nội dung lỗi</param> /// <param name="hasOpened">Đánh dấu có/không mở file</param> /// <returns></returns> public static bool MakeExcelMultiSheet(List <ExcelInfo> excelInfos, string newFileName, List <ReportParam> param, out string message, bool hasOpened = true) { message = string.Empty; // Tạo thư mục lưu tạm string excelTempDir = ReportHelper.GetExcelTemporaryDirectory(); if (!Directory.Exists(excelTempDir)) { Directory.CreateDirectory(excelTempDir); } List <string> pathList = new List <string>(); foreach (var excelInfo in excelInfos) { if (string.IsNullOrEmpty(excelInfo.SheetName)) { excelInfo.SheetName = ReportTemplate.GetTemplate(excelInfo.ReportID); } excelInfo.FilePath = Path.Combine(excelTempDir, $"{excelInfo.SheetName}.xlsx"); excelInfo.Report = ReportHelper.GetReport(excelInfo.ReportID, excelInfo.ReportSource, param, true); if (ReportHelper.ExportExcelByPath(excelInfo.Report, excelInfo.FilePath, excelInfo.SheetName, false)) { pathList.Add(excelInfo.FilePath); } } if (pathList.Count > 0) { string newFilePath = ReportUtility.ShowSaveExcelDialog(newFileName); if (string.IsNullOrEmpty(newFilePath)) { return(false); } // Merge các file excel lại thành 1 file if (!MergeExcellToNew(pathList, newFilePath, out message, hasOpened)) { return(false); } // Xóa các file tạm DeleteTemporaryFiles(); } return(true); }
private bool ExportExcel(ref string message) { List <string> pathList = new List <string>(); if (ExcelReportInfos == null || ExcelReportInfos.Count == 0) { return(true); } // Tạo thư mục lưu tạm string excelTempDir = ReportHelper.GetExcelTemporaryDirectory(); if (!Directory.Exists(excelTempDir)) { Directory.CreateDirectory(excelTempDir); } // Lưu các file excel tạm foreach (ReportInfo item in ExcelReportInfos) { if (string.IsNullOrEmpty(item.SheetName)) { item.SheetName = ReportTemplate.GetTemplate(item.ReportID); } item.FilePath = Path.Combine(excelTempDir, $"{item.SheetName}.xlsx"); if (ReportHelper.ExportExcelByPath(item.Report, item.FilePath, item.SheetName, false)) { pathList.Add(item.FilePath); } } if (pathList.Count > 0) { // Hỏi chỗ lưu file string dateStr; if (ByYear_RadioButton.Checked) { dateStr = Year_DateEdit.DateTime.Year.ToString(); } else { dateStr = $"{Date_FromToDateEdit.FromDate:yyyyMMdd}_{Date_FromToDateEdit.ToDate:yyyyMMdd}"; } string dataDir = ReportHelper.GetDataDirectory(); if (!Directory.Exists(dataDir)) { Directory.CreateDirectory(dataDir); } string newFileName = $"So_Sach_{dateStr}.xlsx"; string newFilePath = ReportUtility.ShowSaveExcelDialog(newFileName); if (string.IsNullOrEmpty(newFilePath)) { newFilePath = Path.Combine(dataDir, newFileName); } // Merge các file excel lại thành 1 file if (!ReportUtility.MergeExcellToNew(pathList, newFilePath, out string msgErr)) { message = msgErr; return(false); } // Xóa các file tạm ReportUtility.DeleteTemporaryFiles(); } return(true); }