FastReport是功能非常强大的报表工具,在本篇文章中讲解如何使用FastReport实现打印功能。 一、新建一个窗体程序,窗体上面有设计界面和预览界面两个按钮,分别对应Fast
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);
}
}
}
在设计之前要先选择数据源,只有选择了数据源,报表文件才会有数据。
双击报表标题区域的A控件,即可打开输入标题的界面:
报表标题区域就会显示设计的标题,并可以设置标题的对齐方式。
给表格添加数据绑定事件:
设置了事件以后,双击事件即可进入代码编辑界面,绑定事件的代码如下:
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(0);
Table1.PrintColumns();
bool tfvar = false;
while (rowData.HasMoreRows)
{
tfvar = true;
Table1.PrintRow(1);
Table1.PrintColumns();
rowData.Next();
}
if (!tfvar)
{
Table1.PrintRow(2);
Table1.PrintColumns();
}
}
}
}
设计完报表格式以后,一定要记得保存:
因为界面太大,所以分了两个截图显示:
到此这篇关于.net使用报表工具FastReport实现打印功能的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持编程网。
--结束END--
本文标题: .NET使用报表工具FastReport实现打印功能
本文链接: https://www.lsjlt.com/news/141922.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2023-05-21
2023-05-21
2023-05-21
2023-05-21
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0