.NET中使用FastReport实现打印功能
FastReport是功能非常强大的报表工具,在本篇文章中讲解如何使用FastReport实现打印功能。
一、新建一个窗体程序,窗体上面有设计界面和预览界面两个按钮,分别对应FastReport的设计和预览功能,其实现代码如下:
using FastReport;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Dapper; namespace FastReportDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btn_Design_Click(object sender, EventArgs e)
{
// 显示设计界面
CreateReport(true);
} private void CreateReport(bool tfDesigin)
{
// 获得当前程序的运行路径
string path = Application.StartupPath;
// 定义报表
Report report = new Report();
string strDirectory = path + "\\ReportFiles"; // 判断文件路径是否存在,不存在则创建文件夹
if (!Directory.Exists(strDirectory))
{
// 不存在就创建目录
Directory.CreateDirectory(strDirectory);
} // 判断文件是否存在
if (!File.Exists(strDirectory + "\\产品明细.frx"))
{
report.FileName = strDirectory + "\\产品明细.frx";
}
else
{
report.Load(strDirectory + "\\产品明细.frx");
} // 创建报表文件的数据源
DataSet ds = new DataSet();
DataTable dt = GetDataSource();
DataTable dtSource = dt.Copy();
dtSource.TableName = "ProductDetail";
ds.Tables.Add(dtSource);
report.RegisterData(ds); if (tfDesigin)
{
// 打开设计界面
report.Design();
}
else
{
// 打开预览界面
report.Show();
}
} private DataTable GetDataSource()
{
DataTable dt = new DataTable();
// 数据库连接
string strCon = @"Initial Catalog=StudentSystem; Integrated Security=False;User Id=sa;Password=1qaz@WSX;Data Source=127.0.0.1;Failover Partner=127.0.0.1;Application Name=TransForCCT";
SqlConnection conn = new SqlConnection(strCon);
string strSql = @"SELECT p.ProductId,p.ProductName,p.Price,c.CategoryName FROM ProductDetail p INNER JOIN Category c
ON p.CategoryId=c.CategoryId";
// 使用Dapper获取数据
IDataReader reader = conn.ExecuteReader(strSql);
dt.Load(reader);
return dt;
} private void btn_Show_Click(object sender, EventArgs e)
{
// 显示预览界面
CreateReport(false);
}
}
}
二、运行程序,点击设计界面,打开FastReport的设计界面:

三、选择数据源
在设计之前要先选择数据源,只有选择了数据源,报表文件才会有数据。
1、在Data文件夹下面选择“Choose Report Data”选项:

2、在Choose Report Data界面选择程序中要用到的数据源:

3、点击“OK”按钮以后,在设计界面的右侧会显示选择的数据源:

四、报表的整体结构:

五、设计报表标题
1、设计报表标题要使用到“A”控件:

2、将左侧的"A"控件拖拽到报表标题区域:

3、设置标题:
双击报表标题区域的A控件,即可打开输入标题的界面:

4、输入报表标题,点击“OK”按钮:
报表标题区域就会显示设计的标题,并可以设置标题的对齐方式。

六:设计报表数据区域
1、设计报表数据,要使用到表格控件,表格控件如下图所示:

2、将表格拖拽到数据区域,设计表格要显示的行数和列数:

3、表格显示的内容:

4、表格界面:

七、设置表格事件
给表格添加数据绑定事件:

设置了事件以后,双击事件即可进入代码编辑界面,绑定事件的代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using FastReport;
using FastReport.Data;
using FastReport.Dialog;
using FastReport.Barcode;
using FastReport.Table;
using FastReport.Utils; namespace FastReport
{
public class ReportScript
{ private void Table1_ManualBuild(object sender, EventArgs e)
{
// 设置数据源
DataSourceBase rowData = Report.GetDataSource("ProductDetail"); rowData.Init(); Table1.PrintRow();
Table1.PrintColumns(); bool tfvar = false;
while (rowData.HasMoreRows)
{
tfvar = true;
Table1.PrintRow();
Table1.PrintColumns();
rowData.Next();
} if (!tfvar)
{
Table1.PrintRow();
Table1.PrintColumns();
}
}
}
}
八、页脚显示打印时间:

九、保存报表格式
设计完报表格式以后,一定要记得保存:

十、效果
因为界面太大,所以分了两个截图显示:


.NET中使用FastReport实现打印功能的更多相关文章
- 浅谈QT打印功能实现
QT作为一款轻量级的集成开发环境,其设计的目标是使开发人员利用QT这个应用程序框架更加快速及轻易的开发应用程序.要达到此目的,要求QT必须能够跨平台,QT能够在32位及64位的Linux,MAC OS ...
- Qt Graphics-View的打印功能实现
本文来研究一下Qt Graphics-View的打印功能实现. 在Qt的官方文档中介绍了Graphics-View的打印相关内容. Qt中对打印的支持是有一个独立的printsupport模块来完成的 ...
- FastReport 中添加二维码功能.(Delphi)
http://www.cnblogs.com/fancycloud/archive/2011/07/24/2115240.html FastReport 中添加二维码功能.(Delphi) 在实际 ...
- 使用FastReport.net 报表在网页上实现打印功能
这些年的工作当中,最早是在8年前接触到FastReport这个报表工具,从名字上来看,直译过来就是快速报表,正所谓天下武功,唯快不破,FastReport报表早些年确实是制作报表的不二之选,8年前的工 ...
- 在react中实现打印功能
最近项目中,前端采用react+antd+dva的组合,已经完成了后头管理类系统产品的更新迭代工作. 今天有一个新需求,需要在后台管理系统中实现点击打印完成指定页面的打印功能. 之前也没接触过,只知道 ...
- Winform中使用FastReport实现自定义PDF打印预览
场景 Winform中使用FastReport实现简单的自定义PDF导出: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1009 ...
- FastReport.Net 常用功能总汇
一.常用控件 文本框:输入文字或表达式 表格:设置表格的行列数,输入数字或表达式 子报表:放置子报表后,系统会自动增加一个页面,你可以在此页面上设计需要的报表.系统在打印处理时,先按主报表打印,当碰到 ...
- 转: 尽己力,无愧于心 FastReport.Net 常用功能总汇
FastReport.Net 常用功能总汇 一.常用控件 文本框:输入文字或表达式 表格:设置表格的行列数,输入数字或表达式 子报表:放置子报表后,系统会自动增加一个页面,你可以在此页面上设计需要 ...
- 在Winform开发中使用FastReport创建报表
FastReport.Net是一款适用于Windows Forms, ASP.NET和MVC框架的功能齐全的报表分析解决方案.可用在Microsoft Visual Studio 2005到2015, ...
随机推荐
- CSS margin属性与用法教程
margin 属性是css用于在一个声明中设置所有 margin 属性的简写属性,margin是css控制块级元素之间的距离, 它们之间是透明不可见的. margin属性包含了margin left ...
- MySQL 两个死锁样例
[引子] 从事MySQL-DBA这一行也有些年头了,想对新人说,在分析死锁问题时应该还要考虑到有一个叫请求队列的“概念”.之所以 在这里提这个不是因为新手不知道,而是有时候会自然而然的想不到. 不信的 ...
- Linux 查看文件 cat与 more 用法
1.cat 显示文件连接文件内容的工具: cat 是一个文本文件查看和连接工具.查看一个文件的内容,用cat比较简单,就是cat 后面直接接文件名. 比如: [root@localhost ~]# c ...
- 程序包org.junit不存在和编码GBK的不可映射字符问题解决
maven项目在打包编译时提示: 解决办法: 将pom中junit依赖中的scope给注释掉 <dependency> <groupId>junit</groupId&g ...
- Error:Cause: org/gradle/api/publication/maven/internal/DefaultMavenFactory Android
首先,要看一下自己的项目使用 “Gradle版本” 接着要看一下项目根目录的build.gradle文件中的“dependencies”的 classpath 'com.github.dcendent ...
- git android.google 源码:Unknown SSL protocol error in connection to code.google.com:443
想要提取android的源码.就必须要使用git.下面是本人安装的过程发生的问题: 1.1安装git.win的命令行的客户端(相当与svn的乌龟那样使用).http://git-scm.com/dow ...
- Rational Rose 2003 下载及破解方法
FROM:http://blog.csdn.net/fenglibing/archive/2007/08/17/1747693.aspx 这么好的东西,不拿来出分享,我对不起原作者呀.但是我这里不知道 ...
- 不错位的java .class 反编译工具推荐
我们经常会反编译看一些class文件,但是反编译出来的文件里面会有很多杂乱的东西 一直以来都是用的idea来反编译的,只要把class文件往里面一拖就行了 这么用没问题,用来看看源码什么的都OK 但是 ...
- Atitit nodejs5 nodejs6 nodejs 7.2.1 新特性attialx总结
Atitit nodejs5 nodejs6 nodejs 7.2.1 新特性attialx总结 1.1. Node.js 4.0.0 已经发布了 .这是和 io.js 合并之后的首个稳定版本,它 ...
- 【Unity】2.7 检视器(Inspector)
分类:Unity.C#.VS2015 创建日期:2016-03-31 一.简介 Unity中的游戏是以包含网格.脚本.声音或灯光 (Lights) 等其他图形元素的多个游戏对象 (GameObject ...