对于管理类的程序在从数据库获取到数据集之后,可能就需要进行报告单的打印,C#中FastReport.Net类库封装了报告单的设计与打印,下面介绍FastReport.Net使用
1、FastReport.Net安装完毕,到C:\Program Files (x86)\FastReports\FastReport.Net Trial路径下找到RastReport.dll类库,加载到程序中
2、程序中加载FastReport.dll控件,在工具箱右击->添加FastReport选项卡->在FastReport选项卡上右击->单击“选择项”->在.Net FrameWork组件浏览上图中的FastReport.dll类库,点击确定完成组件添加。
3、使用FastReport进行打印,使用封装好的代码进行打印,打印的类型可分为3种
a、p_Print模式,直接打印模式,使用此方式,后台无需预览直接打印,此模式多用于无人值守的程序自动进行打印。
b、p_View模式,打印前进入预览模式,所见即所得模式,看到的电子报告单与实际的打印一致,这个模式需要人工再点击一次打印按钮,打印文件才能被打印出来。
c、p_Design模式,设计模式,在程序实际已用过程中,修改报告单的格式必不可少,通过此模式可以弹出FastReport的报表的设计窗体进行报告单的修改
报告单的打印步骤见PrintReport()方法,此方法步骤
a、绑定打印控件
b、装在设计好的打印文件
c、添加绑定从数据库获取到的数据集
d、打印
public static class PrintHelper
{
public enum PrintType
{
/// <summary>
/// 直接打印
/// </summary>
p_Print = 0,
/// <summary>
/// 预览
/// </summary>
p_View,
/// <summary>
/// 设计
/// </summary>
p_Design
};
private static Report FReport = new Report();
public static void PrintReport(DataSet ksxx, PrintType _PrintType, PreviewControl _preview = null)
{
try
{
FReport.Preview = _preview;
FReport.Load(System.Environment.CurrentDirectory + "\\exam.frx");
ksxx.Tables[0].TableName = "ksxx";
FReport.RegisterData(ksxx, "ksxx");
if (PrintType.p_View == _PrintType)
FReport.Show();
else if (PrintType.p_Design == _PrintType)
FReport.Design();
else
FReport.Print();
}
catch (Exception ex)
{
}
}
}
4、窗体控件使用,在窗体界面上需要从工具箱拉去PreviewControl控件,此空间默认下图样式,由于要开发的程序需要简单整洁,因此通过属性关闭了功能属性和状态栏,用到的2个属性分别是:
a、StatusbarVisible 设置为false
b、ToolbarVisible 设置为false
通过上面的两个属性设置后得到下面的控件样式,如果需要调整报告单预览的大小可以通过设置PreviewControl控件Zoom属性来控制大小,此属性float类型可以通过±0.1f来控制报告单大小