原創|使用教程|編輯:何家巧|2022-12-20 10:18:24.490|閱讀 254 次
概述:在本文中,我們將演示如何使用 FastReport .NET 從 JetBrains Rider 創建、構建和導出 PDF 報告/文檔,一起來看看吧~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Fastreport是目前世界上主流的圖表控件,具有超高性價比,以更具成本優勢的價格,便能提供功能齊全的報表解決方案,連續三年蟬聯全球文檔創建組件和庫的“ Top 50 Publishers”獎。
慧都科技是Fast Reports在中國區十余年的友好合作伙伴,連續多年被Fast Reports授予中國區Best Partner稱號。
在本文中,我們將在不使用 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 ”創建一個新的解決方案。
我們首先需要在應用代碼中為我們的報告添加一個簡單的樣本數據集,請在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);
您將看到相應的存儲庫:
現在我們將安裝 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技術QQ群:536197826 歡迎進群一起討論
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn