FastReport中国社区FastReport联系电话 联系电话:023-68661681

如何使用FastReport .NET 在 JetBrains Rider 中创建PDF报告?

来源:   发布时间:2022-12-20   浏览:次

Fastreport是目前世界上主流的图表控件,具有超高性价比,以更具成本优势的价格,便能提供功能齐全的报表解决方案,连续三年蝉联全球文档创建组件和库的“ Top 50 Publishers”奖。

慧都科技是Fast Reports在中国区十余年的友好合作伙伴,连续多年被Fast Reports授予中国区Best Partner称号。

FastReport.NET官方版下载

在本文中,我们将在不使用 Microsoft Visual Studio 的情况下了解 Windows 11 中的 .NET 平台,并创建可导出为 PDF 的报告。与Visual Studio 相似的是 JetBrains Ride,它是由 JetBrains 开发的跨平台 .NET IDE,支持 C#、VB.NET 和 F# 语言。接下来,我们将演示如何使用 FastReport .NET 从 JetBrains Rider 创建、构建和导出 PDF 报告/文档。第一步,您需要在电脑上安装 JetBrains Rider IDE。接下来,通过选择“New Solution ”创建一个新的解决方案。

下一步是设置项目。在.NET/.NET Core部分选择桌面应用程序项目类型。然后给项目命名,我们以 "ReportPDF_Core_WinFormsApp "为例。然后点击Windows Forms App类型,C#语言,NET 7.0框架。

我们首先需要在应用代码中为我们的报告添加一个简单的样本数据集,请在Form1.cs中添加:

 using System.Data; 

接下来,在Form1类中添加一个私有字段。

 private DataSet _fDataSet = new DataSet();

让我们添加一个私有的CreateDataSet方法,在这里我们将创建并填入一个数据集:

  private void CreateDataSet()
 {
 // create simple dataset with one table 
 // create simple dataset
 _fDataSet = new DataSet(); 
 // create a table
 DataTable table = new DataTable();
 table.TableName = "Employees";
 // adding a table to the dataset
 _fDataSet.Tables.Add(table);
 // adding data to a table 
 table.Columns.Add("ID", typeof(int));
 table.Columns.Add("Name", typeof(string));
 
 table.Rows.Add(1, "Andrew Fuller");
 table.Rows.Add(2, "Nancy Davolio");
 table.Rows.Add(3, "Margaret Peacock");
 }

添加对CreateDataSet方法的调用:

 public Form1()
 {
 InitializeComponent();
 CreateDataSet();
 }

在 JetBrains Rider 中让 FastReport .NET 运行得最快方法是什么?那就是使用快速报告私有 NuGet 服务器。接下来我们介绍在购买 FastReport .NET 后如何添加 NuGet 包。首选您需要单击 IDE 底部的 NuGet 选项卡,然后单击源选项卡。

现在我们通过单击“+”并输入必要的数据来添加一个新的存储库:- 名称 — 不带空格的源名称(例如 FR-Nuget);
- 网址——https://nuget.fast-report.com/api/v3/index.json;
- 用户——来自 Fast Reports 帐户的电子邮件;
- 密码 ― Fast Reports 帐户的密码。

您将看到相应的存储库:

现在我们将安装 FastReport.Pro 包。为此,请转到“包”选项卡并按 FR-Nuget 存储库过滤包。当然,安装找到的包。

如果成功,您将看到一条通知。

接下来在Form1.cs中添加:

 public Form1()
 {
 InitializeComponent();
 CreateDataSet();
 }

接下来,我们将在应用程序中插入 3 个新按钮:“报表设计”、“使用对话框导出为 PDF”、“无提示导出”。为此,对 Form1.Designer.cs 进行适当的更改:

  // <summary>
 // Required method for Designer support - do not modify
 // the contents of this method with the code editor.
 // </summary>
 private void InitializeComponent()
 {
 this.components = new System.ComponentModel.Container();
 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
 this.ClientSize = new System.Drawing.Size(800, 450);
 this.Text = "Form1";
 this.btnExportWithDialog = new System.Windows.Forms.Button();
 this.btnSilentExport = new System.Windows.Forms.Button();
 this.btnShowDesigner = new System.Windows.Forms.Button();
 this.SuspendLayout();
 // 
 // btnExportWithDialog
 // 
 this.btnExportWithDialog.Location = new System.Drawing.Point(44, 148);
 this.btnExportWithDialog.Name = "btnExportWithDialog";
 this.btnExportWithDialog.Size = new System.Drawing.Size(208, 23);
 this.btnExportWithDialog.TabIndex = 0;
 this.btnExportWithDialog.Text = "Export to PDF with dialog";
 this.btnExportWithDialog.UseVisualStyleBackColor = true;
 this.btnExportWithDialog.Click += new System.EventHandler(this.btnExportWithDialog_Click);
 // 
 // btnSilentExport
 // 
 this.btnSilentExport.Location = new System.Drawing.Point(44, 180);
 this.btnSilentExport.Name = "btnSilentExport";
 this.btnSilentExport.Size = new System.Drawing.Size(208, 23);
 this.btnSilentExport.TabIndex = 0;
 this.btnSilentExport.Text = "Silent export";
 this.btnSilentExport.UseVisualStyleBackColor = true;
 this.btnSilentExport.Click += new System.EventHandler(this.btnSilentExport_Click);
 // 
 // btnShowDesigner
 // 
 this.btnShowDesigner.Location = new System.Drawing.Point(44, 87);
 this.btnShowDesigner.Name = "btnShowDesigner";
 this.btnShowDesigner.Size = new System.Drawing.Size(208, 23);
 this.btnShowDesigner.TabIndex = 1;
 this.btnShowDesigner.Text = "Report design";
 this.btnShowDesigner.UseVisualStyleBackColor = true;
 this.btnShowDesigner.Click += new System.EventHandler(this.btnShowDesigner_Click);
 // 
 // Form1
 // 
 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
 this.ClientSize = new System.Drawing.Size(292, 266);
 this.Controls.Add(this.btnShowDesigner);
 this.Controls.Add(this.btnSilentExport);
 this.Controls.Add(this.btnExportWithDialog);
 this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
 this.Name = "Form1";
 this.Text = "ExportToPDF";
 this.ResumeLayout(false);
 }
 #endregion
 private System.Windows.Forms.Button btnExportWithDialog;
 private System.Windows.Forms.Button btnSilentExport;
 private System.Windows.Forms.Button btnShowDesigner; 

让我们使用这段代码为“报表设计”按钮编写一个点击处理程序。

 private void btnShowDesigner_Click(object sender, EventArgs e)
 {
 // create report instance
 Report report = new Report();
 // load the existing report
 //report.Load(@"..\..\..\Report.frx");
 // register the dataset
 report.RegisterData(_fDataSet);
 report.GetDataSource("Employees").Enabled = true;
 // run the designer
 report.Design(); 
 // free resources used by report
 report.Dispose();
 }

运行应用程序并查看带有 3 个按钮的表单。

单击“报表设计”按钮并转到 FastReport .NET 设计器。

让我们使用拖放操作将数据集中的字段添加到报告模板,然后将“员工”标题添加到报告中。之后,为文本对象设置 AutoWidth = true 属性。

让我们将我们的报告模板保存在 ReportPDF_Core_WinFormsApp 项目所在的文件夹中,名称为“Report”。保存后,关闭设计器和应用程序。让我们取消注释 btnExportWithDialog_Click 方法中的行,以便在打开设计器时加载我们保存的报表:

  report.Load(@"..\..\..\Report.frx");

为带有对话框的“导出为 PDF”按钮添加点击处理程序:

  private void btnExportWithDialog_Click(object sender, EventArgs e)
 {
 // create report instance
 Report report = new Report();
 // load the existing report
 report.Load(@"..\..\..\Report.frx");
 // register the dataset
 report.RegisterData(_fDataSet);
 // run the report
 report.Prepare();
 // create export instance
 PDFExport export = new PDFExport();
 export.Export(report);
 // free resources used by report
 report.Dispose();
 }

运行项目并单击“使用对话框导出为 PDF”按钮:

将打开一个包含 PDF 导出设置的对话框。选择“导出后打开”并单击“确定”。保存到名为“Report”的 PDF 项目文件夹。导出完成后,PDF文件会自动打开:

因此,我们得到了一个从数据集构建的简单报告/PDF 文档。我们还要检查所谓的没有对话框的“静默”PDF 导出选项。为“静默导出”按钮添加点击处理程序:

private void btnSilentExport_Click(object sender, EventArgs e)
 {
 // create report instance
 Report report = new Report();
 // load the existing report
 report.Load(@"..\..\..\Report.frx");
 // register the dataset
 report.RegisterData(_fDataSet);
 // run the report
 report.Prepare();
 // run the report
 PDFExport export = new PDFExport();
 // opening after export
 export.OpenAfterExport = true;
 // export the report
 report.Export(export, "Result.pdf");
 // free resources used by report
 report.Dispose();
 }

运行项目并单击“静默导出”按钮。它将立即导出并打开一个名为“结果”的 PDF 文件,它位于正在运行的项目的 exe 旁边:

在本文中,我们回顾了JetBrains Rider (C#) + .NET Core + WinForms + FastReport .NET + Windows 11的拉力,并收到了从数据集构建的 PDF 报告。当然,我们确保无需 Microsoft Visual Studio 即可轻松使用 .NET 平台。

关于“使用FastReport .NET 从 JetBrains Rider 中创建PDF报告”的教程就到这里了,点击查看更多FastReport .Net使用教程

FastReport .Net | 下载试用

FastReport技术QQ群:536197826      欢迎进群一起讨论

本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:FastReport控件中文网 [https://www.fastreportcn.com/]
本文地址:https://www.fastreportcn.com/post/3423.html

联系我们
  • 重庆总部 023-68661681
购买
  • sales@evget.com
合作
  • business@evget.com


在线
客服
微信
QQ 电话
023-68661681
返回
顶部