예제 #1
0
        public static string GetReportPath(string reportID)
        {
            string dir        = GetReportDirectory();
            string reportPath = Path.Combine(dir, $"{ReportTemplate.GetTemplate(reportID)}.repx");

            return(reportPath);
        }
예제 #2
0
        public static void ExportExcel(string reportID,
                                       object dataSource,
                                       List <ReportParam> reportParams,
                                       bool hasOpened         = true,
                                       bool isUseParramCommon = true,
                                       string sheetName       = null)
        {
            string templateName = ReportTemplate.GetTemplate(reportID);

            SaveFileDialog openFileDialog = new SaveFileDialog
            {
                Filter   = "Excel file(*.xlsx)|*.xlsx",
                FileName = templateName
            };

            string path;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                path = openFileDialog.FileName;
            }
            else
            {
                return;
            }

            try
            {
                XlsxExportOptions xlsxExportOptions = new XlsxExportOptions
                {
                    SheetName = string.IsNullOrWhiteSpace(sheetName) ? templateName : sheetName
                };

                reportParams.Add(new ReportParam(ExcelParam, true));

                XtraReport report = GetReport(reportID, dataSource, reportParams, isUseParramCommon);
                if (report == null)
                {
                    return;
                }

                report.ExportToXlsx(path, xlsxExportOptions);

                if (hasOpened)
                {
                    System.Diagnostics.Process.Start(path);
                }
            }
            catch (Exception ex)
            {
                BSLog.Logger.Warn(ex.Message);
                MessageBoxHelper.ShowErrorMessage($"Xuất excel thất bại!\r\n{ex.Message}");
            }
        }
예제 #3
0
        public static void ExportExcel(XtraReport report, string reportID, bool hasOpened = true)
        {
            string templateName = ReportTemplate.GetTemplate(reportID);

            SaveFileDialog openFileDialog = new SaveFileDialog
            {
                Filter   = "Excel file(*.xlsx)|*.xlsx",
                FileName = templateName
            };

            string path;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                path = openFileDialog.FileName;
            }
            else
            {
                return;
            }

            try
            {
                XlsxExportOptions xlsxExportOptions = new XlsxExportOptions
                {
                    SheetName = templateName
                };

                if (report == null)
                {
                    return;
                }

                if (report.Parameters[ExcelParam] != null)
                {
                    report.Parameters[ExcelParam].Value = true;
                }

                report.ExportToXlsx(path, xlsxExportOptions);

                if (hasOpened)
                {
                    System.Diagnostics.Process.Start(path);
                }
            }
            catch (Exception ex)
            {
                BSLog.Logger.Warn(ex.Message);
                MessageBoxHelper.ShowErrorMessage($"Xuất excel thất bại!\r\n{ex.Message}");
            }
        }
예제 #4
0
        /// <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);
        }
예제 #5
0
        public static void ExportWord(string reportID, object dataSource, List <ReportParam> reportParams, bool hasOpened = true, bool isUseParramCommon = true)
        {
            string templateName = ReportTemplate.GetTemplate(reportID);

            SaveFileDialog openFileDialog = new SaveFileDialog
            {
                Filter   = "Word Document(*.docx)|*.docx",
                FileName = templateName
            };

            string path;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                path = openFileDialog.FileName;
            }
            else
            {
                return;
            }

            try
            {
                DocxExportOptions exportOptions = new DocxExportOptions
                {
                    TableLayout   = true,
                    KeepRowHeight = true,
                };

                XtraReport report = GetReport(reportID, dataSource, reportParams, isUseParramCommon);
                if (report == null)
                {
                    return;
                }

                report.ExportToDocx(path, exportOptions);

                if (hasOpened)
                {
                    System.Diagnostics.Process.Start(path);
                }
            }
            catch (Exception ex)
            {
                BSLog.Logger.Warn(ex.Message);
                MessageBoxHelper.ShowErrorMessage($"Xuất word thất bại!\r\n{ex.Message}");
            }
        }
예제 #6
0
        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);
        }
예제 #7
0
 private void ExportExcel_Button_Click(object sender, EventArgs e)
 {
     Main_GridControl.ExportExcelByName(ReportTemplate.GetTemplate(ReportTemplate.RPT006));
 }