彩票走势图

报表控件FastReport使用指南——使用NuGet包创建PDF文档

翻译|行业资讯|编辑:周思宇|2023-05-26 10:36:17.947|阅读 95 次

概述:报表控件FastReport .NET在JetBrains Rider中工作的最简单方法是使用其私有 NuGet 服务器。本文主要描述如何在FastReport .NET中添加NuGet包,从而在Ubuntu 22.04.1 LTS系统中的.NET IDE上创建、构建和导出PDF报告/文档,希望能为您提供帮助。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

FastReport 是功能齐全的报表控件,可以帮助开发者可以快速并高效地为.NET,VCL,COM,ActiveX应用程序添加报表支持,由于其独特的编程原则,现在已经成为了Delphi平台最优秀的报表控件,支持将编程开发中的报表信息输出为TXT、PDF等多种文件格式,是编程开发人员必备的报表工具。

FastReport .Net是一款全功能的Windows Forms、ASP.NET和MVC报表分析解决方案,使用FastReport .Net可以创建独立于应用程序的.NET报表。

FastReport.NET官方版下载

上篇文章中,我们学习了如何使用FastReport .NET在Ubuntu 22.04.1 LTS系统中的.JetBrains Rider上创建PDF文档。本次将分享FastReport .NET 在 JetBrains Rider 中工作的最简单方法——使用 Fast Reports 中的私有 NuGet 服务器。

因此,本文主要描述在购买FastReport .NET后如何添加NuGet包,从而在Ubuntu 22.04.1 LTS系统中的.NET IDE上创建、构建和导出PDF报告/文档的过程。

单击IDE底部的NuGet选项卡,然后单击Sources选项卡。

NuGet settings form

现在通过点击“+”来添加一个新的存储库,并输入必要的数据:

- Name—不带空格的源名称(例如FastReport);
- URL—//nuget.fast-report.com/api/v3/index.json;
- User—来自Fast Reports帐户的电子邮箱;
- Password—来自Fast Reports帐户的密码。

Form for adding a NuGet repository

您将看到存储库:

FastReport-NuGet repository has been added

现在我们将安装FastReport Core 包。为此,请转到“Packages”选项卡并通过FastReport存储库过滤包。然后,安装找到的包。

Installing the FastReport.Core NuGet package

安装成功,您将收到通知。

Notification about the successful installation of the FastReport.Core NuGet package

接下来,从代码中创建一个模板,为此我们将执行以下操作:添加到Program.cs:

using System.Drawing;
using FastReport;
using FastReport.Export.Pdf;
using FastReport.Utils;

接下来,将 CreateDataSet 添加到下面的 Program.cs 中:

Report report = new Report();
CreateReportTemplate();
ExportToPDF();

然后,添加创建报表模板函数CreateReportTemplate:

void CreateReportTemplate()
{
// adding a report page
ReportPage page = new ReportPage();

// creating a date band
DataBand data = new DataBand();
PageHeaderBand dataText = new PageHeaderBand();

//creating a header
ReportTitleBand titleBand = new ReportTitleBand();
TextObject employeeIdText = new TextObject();
TextObject employeeNameText = new TextObject();
TextObject idText = new TextObject();
TextObject nameText = new TextObject();
TextObject titleText = new TextObject();
//registering the data source
report.RegisterData(dataSet);
//enabling on the data table
report.GetDataSource("Employees").Enabled = true;
//adding a page to the template
report.Pages.Add(page);
// add to the page: data,data Text, titleBand
// and set the unique name of the page
page.AddChild(data);
page.AddChild(dataText);
page.AddChild(titleBand);
page.CreateUniqueName();
// set the unique name titleBand
// and set the band settings
titleBand.CreateUniqueName();
titleBand.Height = Units.Centimeters * 1.5f;
titleText.Bounds = new RectangleF(300, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f);
titleText.Text = "Employees";
titleText.Font = new Font("Arial", 14, FontStyle.Bold);
titleText.VertAlign = VertAlign.Center;

// set the unique name data
// and set the data settings
data.CreateUniqueName();
data.DataSource = report.GetDataSource("Employees");
data.Height = Units.Centimeters * 0.5f;

// set a unique dataText name
// and set the dataText settings
dataText.CreateUniqueName();
dataText.Height = Units.Centimeters * 0.8f;

// setting the unique name employeeIdText
// and set the employeeIdText, idText settings
employeeIdText.Parent = data;
employeeIdText.CreateUniqueName();
employeeIdText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f);
idText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f);
idText.Text = "ID";
employeeIdText.Text = "[Employees.ID]";

// set the unique name employeeNameText
// and set the employeeNameText, nameText settings
employeeNameText.Parent = data;
employeeNameText.CreateUniqueName();
employeeNameText.Bounds = new RectangleF(50, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f);
nameText.Bounds = new RectangleF(50, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f);
nameText.Text = "Name";
employeeNameText.Text = "[Employees.Name]";

// add on data band: employeeIdText, employeeNameText
data.AddChild(employeeIdText);
data.AddChild(employeeNameText);

// add on dataText band: idText, nameText
dataText.AddChild(idText);
dataText.AddChild(nameText);

// add on titleBand band: itleText
titleBand.AddChild(titleText);
}

接下来,添加创建ExportToPDF报表模板的功能,并将报表导出命名为“Report.pdf”:

void ExportToPDF()
{
// running the report
report.Prepare();
// creating an export instance
PDFExport export = new PDFExport();
report.Export(export, "test.pdf");
// disposing the resources used by the report
report.Dispose();
}

最后,启动控制台应用程序。如果您收到响应 Process finished with exit code 0,那么您之前做的每一步,打开报告,在我们的例子中,它是路径:

 /home/alex/RiderProjects/ReportPDF_Core_ConsoleApp/ReportPDF_Core_ConsoleApp/bin/Debug/net6.0/test .pdf:

 The result of the PDF export of the report

由此,我们得到了一个从数据集构建的简单报表/PDF文档。

在本文中,我们讨论了绑定JetBrains Rider (C#) + .NET Core + Console Application + FastReport .NET Core + Linux (Ubuntu 22.04.1 LTS) ,并从PDF数据集构建了一个报告。当然,我们确保 .NET 平台可以在没有Microsoft Visual Studio的情况下轻松使用,因为Linux根本没有它。

完整的程序清单
using System.Data;
using System.Drawing;
using FastReport;
using FastReport.Export.Pdf;
using FastReport.Utils;

//creating a data set
DataSet dataSet = new DataSet();
CreateDataSet();
//creating a report
Report report = new Report();

CreateReportTemplate();
ExportToPDF();

void CreateReportTemplate()
{
// add a report page
ReportPage page = new ReportPage();

// create a data band
DataBand data = new DataBand();
PageHeaderBand dataText = new PageHeaderBand();

//create a title
ReportTitleBand titleBand = new ReportTitleBand();
TextObject employeeIdText = new TextObject();
TextObject employeeNameText = new TextObject();
TextObject idText = new TextObject();
TextObject nameText = new TextObject();
TextObject titleText = new TextObject();
//register a data source
report.RegisterData(dataSet);
//enable a data table
report.GetDataSource("Employees").Enabled = true;
//add a page to the template
report.Pages.Add(page);
//add on a page: data,dataText, titleBand
// and set the unique page name
page.AddChild(data);
page.AddChild(dataText);
page.AddChild(titleBand);
page.CreateUniqueName();
// set the unique name titleBand
// and set the band settings
titleBand.CreateUniqueName();
titleBand.Height = Units.Centimeters * 1.5f;
titleText.Bounds = new RectangleF(300, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f);
titleText.Text = "Employees";
titleText.Font = new Font("Arial", 14, FontStyle.Bold);
titleText.VertAlign = VertAlign.Center;

// create the unique data name
// and set the data settings
data.CreateUniqueName();
data.DataSource = report.GetDataSource("Employees");
data.Height = Units.Centimeters * 0.5f;

// create a unique dataText name
// and set dataText settings
dataText.CreateUniqueName();
dataText.Height = Units.Centimeters * 0.8f;

// create the unique employeeIdText name
// and set the employeeIdText, idText settings
employeeIdText.Parent = data;
employeeIdText.CreateUniqueName();
employeeIdText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f);
idText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f);
idText.Text = "ID";
employeeIdText.Text = "[Employees.ID]";

// create the unique name employeeNameText
// and set the employeeNameText, nameText settings
employeeNameText.Parent = data;
employeeNameText.CreateUniqueName();
employeeNameText.Bounds = new RectangleF(50, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f);
nameText.Bounds = new RectangleF(50, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f);
nameText.Text = "Name";
employeeNameText.Text = "[Employees.Name]";

// and add on data band: employeeIdText, employeeNameText
data.AddChild(employeeIdText);
data.AddChild(employeeNameText);

// add on dataText band: idText, nameText
dataText.AddChild(idText);
dataText.AddChild(nameText);

// add on titleBand band: itleText
titleBand.AddChild(titleText);
}

void ExportToPDF()
{
report.Prepare();
PDFExport export = new PDFExport();
report.Export(export, "test.pdf");
report.Dispose();
}

void CreateDataSet()
{
// create a simple dataset with a single table

// create a simple dataset
dataSet = new DataSet();

// create a table
DataTable table = new DataTable();
table.TableName = "Employees";
// add the table to dataset
dataSet.Tables.Add(table);

// add data to the 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");
}

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


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@cahobeh.cn


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP