Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use FastReport to realize print function in. Net

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

本文小编为大家详细介绍".NET中如何使用FastReport实现打印功能",内容详细,步骤清晰,细节处理妥当,希望这篇".NET中如何使用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. Select the data source to be used in the Choose Report Data interface:

3. After clicking the "OK" button, the selected data source will be displayed on the right side of the design interface:

IV. Overall structure of the report:

V. Design report title 1. Design report title to use "A" control:

2. Drag the "A" control on the left to the report title area:

3. Set the title:

Double-click the A control in the report title area to open the interface for inputting titles:

4. Enter the report title and click the "OK" button:

The report title area displays the designed title and allows you to set the alignment of the title.

VI: Design report data area 1. Design report data, to use table controls, table controls are shown in the following figure:

2. Drag the table to the data area and design the number of rows and columns to be displayed in the table:

3. Contents displayed in the table:

4. Form interface:

VII. Setting Table Events

Add data binding events to tables:

After setting the event, double-click the event to enter the code editing interface. The code for binding the event is as follows:

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) { //Set data source 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(); } {}} VIII. Print time displayed in footer:

IX. Save Report Format

After designing the report format, remember to save:

X. Effect

Because the interface is too large, it is divided into two screenshots:

Read here, this article "how to use FastReport to achieve printing function in. NET" article has been introduced, want to master the knowledge points of this article also need to be used by ourselves to understand, if you want to know more related content of the article, welcome to pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report