Esempio n. 1
0
        private async Task ExportResult(string format, DataSourceLoadOptionsBase dataOptions, HttpContext context)
        {
            XtraReport report = new XtraReport();

            dataOptions.Skip = 0;
            dataOptions.Take = 0;
            var loadedData = DataSourceLoader.Load(await weatherForecastService.GetForecastAsync(), dataOptions);

            report.DataSource = loadedData.data.Cast <WeatherForecast>();
            ReportHelper.CreateReport(report, new string[] { "TemperatureC", "TemperatureF", "Summary", "Date" });
            await new TaskFactory().StartNew(() => {
                report.CreateDocument();
                using (MemoryStream fs = new MemoryStream()) {
                    if (format == pdf)
                    {
                        report.ExportToPdf(fs);
                    }
                    else if (format == xlsx)
                    {
                        report.ExportToXlsx(fs);
                    }
                    else if (format == docx)
                    {
                        report.ExportToDocx(fs);
                    }
                    context.Response.Clear();
                    context.Response.Headers.Append("Content-Type", "application/" + format);
                    context.Response.Headers.Append("Content-Transfer-Encoding", "binary");
                    context.Response.Headers.Append("Content-Disposition", "attachment; filename=ExportedDocument." + format);
                    context.Response.Body.WriteAsync(fs.ToArray(), 0, fs.ToArray().Length);
                    return(context.Response.CompleteAsync());
                }
            });
        }
        public async Task <byte[]> ExportResult(LoadResult lr, string format)
        {
            XtraReport report     = new XtraReport();
            var        loadedData = lr;

            report.DataSource = loadedData.data.Cast <Products>();
            CreateReport(report, new string[] { "ProductName", "CategoryId", "UnitPrice", "UnitsInStock", "UnitsOnOrder", "Discontinued" });
            return(await new TaskFactory().StartNew(() => {
                report.CreateDocument();
                using (MemoryStream fs = new MemoryStream()) {
                    if (format == pdf)
                    {
                        report.ExportToPdf(fs);
                    }
                    else if (format == xlsx)
                    {
                        report.ExportToXlsx(fs);
                    }
                    else if (format == docx)
                    {
                        report.ExportToDocx(fs);
                    }
                    return fs.ToArray();
                }
            }));
        }
        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}");
            }
        }
        private void exportFileDocx(XtraReport report)
        {
            string         reportPath = "";
            SaveFileDialog sf         = new SaveFileDialog();

            sf.FileName = "Save Here.docx";
            if (sf.ShowDialog() == DialogResult.OK)
            {
                reportPath = Path.GetDirectoryName(sf.FileName) + sf.FileName;
                try
                {
                    report.ExportToDocx(sf.FileName);
                    MessageBox.Show("Lưu thành công!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }