Grid++ Report 这个组件在我们开发过程中可以帮助我们实现一些报表票据的模板,你可以自由的的设计出你需要的格式。不管做票据,还是报表都是很简便明了的。当然,还有许多的报表组件,比如FineReport、FastReport等诸多报表组件。下面分享怎么使用Grid++ Report 实现票据打印。
使用Grid++ Report 实现票据打印
1.首先去 锐浪报表工具 官网下载 Grid++ Report 开发者安装包下载 - 锐浪报表工具
2.这是下载的文件,然后在你的程序中 添加引用 引入 gregn6.dll。

3.打开Grid++ Report 报表设计器,设计你要的格式 如:

4.设置字段,报表中的字段最好是和你程序中的要打印的数据实体字段相同,这里我是直接连接数据源生成字段。如:
、
5.以上都弄好之后,把设计好的模板放到你程序中
6.编写一个打印的帮助类,这里需要用到 GridppReport 这个类,上面我们已经引用了gregn6.dll,然后这里using一下就好,
public static class GridPrint
{
//定义Grid++Report报表主对象
private static GridppReport Report;
private static List<IGRField?> _listFields; // 定义Grid字段集合
private static QueueNumber _number;//打印的数据
public static void Print(QueueNumber queue)
{
try
{
_number = queue;//把数据全局化
Report = new GridppReport();
Report.LoadFromFile("./Record/Number.grf");//读取模板在程序中的地址
//Initialize事件,在报表生成开始时触发,这里我主要做的是字段初始化(当然具体用法根据个人所需可以在报表生成开始时设置)
Report.Initialize += ReportInitialize;
//FetchRecord 报表主对象请求数据时触发,这里差不多就是给模板提供对应数据(具体使用
方法同上)
Report.FetchRecord += Report_FetchRecord;
//这里是打印机的一些设置,这里是获取当前使用的打印机然后使用当前的打印机进行打印,如果是使用默认打印机打印的话可以不用设置(我是为了后面做别的设置就先把代码写在这),调用Print方法时会使用默认打印机打印,
PrinterSettings settings = new PrinterSettings();
string defaultPrinter = settings.PrinterName;
Report.Printer.PrinterName = defaultPrinter;//打印机名字
Report.Print(false);
}
catch (Exception ex)
{
MessageBox.Show("" + ex.Message, "错误");
}
}
/// <summary>
/// 字段初始化
/// </summary>
private static void ReportInitialize()
{
//实例化Grid 字段的集合
_listFields = new();
//这里直接使用我们的数据实体的字段来当做Grid 字段
Type myClassType = typeof(QueueNumber);
PropertyInfo[] properties = myClassType.GetProperties();
foreach (PropertyInfo property in properties)
{
//这个判断是为了屏蔽某个字段,这里是IsRemove,IsCallState这两个字段在Grid 中不需要的,这里就屏蔽掉
if (property.Name== "IsRemove")
{
continue;
}
if (property.Name == "IsCallState")
{
continue;
}
_listFields.Add(Report.FieldByName(property.Name));
}
//如果你有某个字段需要单独定制,例如票据中的总数,但是你实体又没有的,是后续才算出来的,可以这样单独添加
//IGRField? report = Report.FieldByName("Sum");
//if (report != null)
//{
//_listFields.Add(report);
//}
}
/// <summary>
/// 载入字段
/// </summary>
private static void Report_FetchRecord()
{
try
{
Report.DetailGrid.Recordset.Append();
Type myClassType = _number.GetType();
PropertyInfo[] properties = myClassType.GetProperties();
//这里也是遍历数据实体的字段然后筛选Grid 的字段进行赋值,当然,这里是可以拓展的,我这里写的比叫粗糙,在使用时可以自己根据需要进行封装
foreach (var field in _listFields)
{
if (field?.Name== properties.Where(o=>o.Name== field?.Name).FirstOrDefault()?.Name)
{
field.AsString = properties.Where(o => o.Name == field.Name).FirstOrDefault()?.GetValue(_number)?.ToString();
continue;
}
}
Report.DetailGrid.Recordset.Post();
}
catch (Exception e)
{
MessageBox.Show("字段时发生错误" + e.Message);
}
}
}
7.接下来就是调用帮助类了
//因为GridPrint是静态所以不用new了,可以根据需要做改变。Model 就是传入要打印的数据
GridPrint.Print(Model);//打印
这样就可以实现自定义模板打印了,报表也是同理。Grid++Report 详细用法可以去参考相关文档。
在开发过程中遇到这个功能,有什么更好的方式,欢迎指教。
本文介绍了如何利用Grid++Report组件设计报表模板并实现票据打印,包括下载开发包、添加引用、设计格式、设置字段和数据绑定,以及创建帮助类进行打印操作。
6837

被折叠的 条评论
为什么被折叠?



