当前位置: 移动技术网 > IT编程>开发语言>.net > Winforn中使用FastReport实现点击导出按钮PDF预览并弹出另存为对话框

Winforn中使用FastReport实现点击导出按钮PDF预览并弹出另存为对话框

2019年10月23日  | 移动技术网IT编程  | 我要评论

型月的七曜魔法使,axx 毕夏照片,吴镇宇第一任老婆

场景

fastreport安装包下载、安装、去除使用限制以及工具箱中添加控件:

https://blog.csdn.net/badao_liumang_qizhi/article/details/100893794

winform中使用fastreport实现简单的自定义pdf导出:

https://blog.csdn.net/badao_liumang_qizhi/article/details/100920681

参照上面实现使用fastreport导出pdf的实现后。

如果要在点击导出按钮后同时进行pdf预览并弹出另存为对话框。

效果如下:

 

 

注:

博客主页:

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

新建窗体并拖拽一个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);

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网