Winforn中使用FastReport實現點擊導出按鈕PDF預覽並彈出另存爲對話框

場景

FastReport安裝包下載、安裝、去除使用限制以及工具箱中添加控件:編程

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100893794工具

Winform中使用FastReport實現簡單的自定義PDF導出:this

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100920681spa

參照上面實現使用FastReport導出PDF的實現後。.net

若是要在點擊導出按鈕後同時進行PDF預覽並彈出另存爲對話框。3d

效果以下:code

 

 

注:orm

博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公衆號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。blog

實現

新建窗體並拖拽一個button和FastReport的PreviewControl教程

 

 

在button的點擊事件中

首先加載frm模板文件

Report report = new Report();
//獲取項目目錄
string baseDir = System.Windows.Forms.Application.StartupPath;
//拼接模板文件目錄
var reportFile = Path.Combine(baseDir + @"\data\Report", "ExportPDF.frx");
//先清理一下
report.Clear();
//而後加載模板文件
report.Load(reportFile);

 

對模板中的TextObject進行賦值

foreach (Control ctl in this.panelControl1.Controls)
            {
                string[] strs = ctl.Name.Split('_');
                if (strs.Length > 1)
                {
                    string changeText = null;
                    if (strs[1].Equals("Date"))
                    {
                        //日期處理
                        DateEdit dateEdit = ctl as DateEdit;
                        DateTime date = (DateTime)dateEdit.EditValue;
                        changeText = date.ToLongDateString().ToString();
                    }else if (strs[1].Equals("Time"))
                    {
                        //時間處理
                        TimeEdit dateEdit = ctl as TimeEdit;
                        DateTime time = (DateTime)dateEdit.EditValue;
                        changeText = time.ToLongTimeString().ToString();
                    }
                    else
                    {
                        changeText = ctl.Text;
                    }
                    //找到 Name屬性爲T的控件
                    var t = report.FindObject("Text_" + strs[1]) as TextObject;
                    if (t != null)
                    {
                        //修改控件值
                        t.Text = changeText;
                    }
                }
            }

 

對模板中的圖片控件PictureObject進行設置照片源

var graph = report.FindObject("Picture2") as PictureObject;
//獲取圖像
System.Drawing.Image image = MainViewContent.mainViewContent.zedGraphControl1.GetImage();
//照片旋轉90度
image.RotateFlip(RotateFlipType.Rotate90FlipNone);
graph.Image = image;

綁定並顯示預覽窗口

//綁定預覽控件 否則會彈出新的窗口
report.Preview = this.previewControl1; 
//顯示預覽窗口
report.Prepare();
report.ShowPrepared();

 

顯示另存爲窗口

//顯示另存爲窗口
SaveFileDialog saveDialog = new SaveFileDialog();
//設置默認文件擴展名。
saveDialog.DefaultExt = "pdf";
//設置當前文件名篩選器字符串,該字符串決定對話框的「另存爲文件類型」或「文件類型」框中出現的選擇內容。
saveDialog.Filter = "pdf文件|*.pdf";
//設置文件名
saveDialog.FileName = fileName;
//用默認的全部者運行通用對話框。
saveDialog.ShowDialog();
//若是修改了文件名,用對話框中的文件名名從新賦值
fileName = saveDialog.FileName;
//被點了取消
if (fileName.IndexOf(":") < 0) return;
FastReport.Export.Pdf.PDFExport export = new FastReport.Export.Pdf.PDFExport();
report.Export(export, fileName);
//即保存後打開Excel
System.Diagnostics.Process.Start(fileName);
相關文章
相關標籤/搜索