FastReport 导出Excel、Word、Pdf

 private void ExportPDF(H_HistoryDataModel model)
        {
            try
            {
                // create report instance
                Report report = new Report();
                PrepareReport(report, model);
                // create export instance
                PDFExport export = new PDFExport();

                // export the report

                if (txtFilePath.Text.Trim() == "")//路径为空的时候默认放到桌面
                {
                    txtFilePath.Text = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                }
                if (txtFileName.Text.Trim() == "")
                {
                    txtFileName.Text = string.Format("检测报告{0}.pdf", DateTime.Now.ToString("yyyy-MM-dd HHmmss"));
                }
                string strPath = this.txtFilePath.Text + "\\" + this.txtFileName.Text;
                if (!strPath.EndsWith(".pdf"))
                {
                    MessageBox.Show("请输入pdf格式的文件名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                report.Export(export, strPath);

                // free resources used by report
                report.Dispose();
                MessageBox.Show("结果保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                MessageBox.Show("输入的路径无法访问,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ExportXLS(H_HistoryDataModel model)
        {
            try
            {
                // create report instance
                Report report = new Report();
                PrepareReport(report, model);

                FastReport.Export.OoXML.Excel2007Export xlsExport = new FastReport.Export.OoXML.Excel2007Export();
                xlsExport.ShowProgress = false;

                MemoryStream strm = new MemoryStream();
                report.Export(xlsExport, strm);

                strm.Position = 0;
                if (txtFilePath.Text.Trim() == "")//路径为空的时候默认放到桌面
                {
                    txtFilePath.Text = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                }
                if (txtFileName.Text.Trim() == "")
                {
                    txtFileName.Text = string.Format("检测报告{0}.xls", DateTime.Now.ToString("yyyy-MM-dd HHmmss"));
                }
                string strPath = this.txtFilePath.Text + "\\" + this.txtFileName.Text;
                if (!strPath.EndsWith(".xls"))
                {
                    MessageBox.Show("请输入xls格式的文件名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                FileStream fs = new FileStream(strPath, FileMode.Create);
                strm.WriteTo(fs);
                strm.Close();
                fs.Close();

                xlsExport.Dispose();
                strm.Dispose();
                fs.Dispose();
                report.Dispose();
                MessageBox.Show("结果保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                MessageBox.Show("输入的路径无法访问,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
   public void ExportPdfWordByFastReport(string reportFile = @"D:\reportFile.frx")
        {
            try
            {
                //数据源
                DataTable dt = new DataTable();
                dt.Columns.Add("col1", typeof(byte[]));
                dt.Columns.Add("col2", typeof(int));
                dt.Columns.Add("col3", typeof(string));
                dt.Rows.Add(dt.NewRow());
                dt.Rows[0]["col2"] = 2;
                dt.Rows[0]["col3"] = "表数据第三列字段值";

                FastReport.Report report = new FastReport.Report();
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                report.RegisterData(ds);
                report.Load(reportFile);
                report.Prepare();

                string fileName = @"D:\2.docx";
                //导出Word,使用RichText.RTFExport,但是效果没有OoXML.Word2007Export好
                FastReport.Export.RichText.RTFExport export = new FastReport.Export.RichText.RTFExport();
                report.Export(export, fileName);

                //导出Word,使用OoXML.Word2007Export
                FastReport.Export.OoXML.Word2007Export exportWord = new FastReport.Export.OoXML.Word2007Export();
                exportWord.ShowProgress = false; //可选,设置导出时是否显示进度窗口,这里设置为不显示。
                report.Export(exportWord, fileName);

                //导出pdf
                fileName = @"D:\2.pdf";
                FastReport.Export.Pdf.PDFExport exportPDF = new FastReport.Export.Pdf.PDFExport();
                exportPDF.PdfCompliance = FastReport.Export.Pdf.PDFExport.PdfStandard.PdfX_3;//pdf版本1.4
                exportPDF.TransparentImages = false;
                report.Export(exportPDF, fileName);
            }
            catch (Exception ex)
            {

            }
        }

 

posted @ 2023-10-30 16:08  lingmin210  阅读(1708)  评论(1编辑  收藏  举报