翻譯|使用教程|編輯:龔雪|2022-02-22 10:21:14.093|閱讀 621 次
概述:本文主要為大家介紹如何以編程方式從本機代碼創(chuàng)建表格報表,歡迎下載最新版體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
FastReport .Net v2022.1官方正式版下載
一個相當常見的情況是,當您需要非??焖俚淖龀鰶Q策或使用可用信息時,不要忘記報表的結構和內(nèi)容取決于外部因素。FastReport .NET 報表生成器是一款非常靈活的產(chǎn)品,它為您提供了兩種解決此問題的方法。
關于如何在報表腳本中實現(xiàn)某些操作的特征信息非常多,但是有不少示例展示了如何從代碼創(chuàng)建報表。在本文中,我們將使用應用程序代碼創(chuàng)建一個包含表格的報表,還將了解如何用數(shù)據(jù)填充表格并將對象放置在表格單元格中。
我們可以用靜態(tài)或動態(tài)數(shù)據(jù)填充 Table 對象。 在第一種情況下,我們知道表格的具體維度,并立即將數(shù)據(jù)從固定集輸入到單元格中。
在第二種情況下,表是動態(tài)創(chuàng)建的,根據(jù)源中的數(shù)據(jù)添加行(列),但是您也可以限制表格的大小并對其進行修復。
讓我們創(chuàng)建一個 WinForms 應用程序,在鏈接中包含 FastReport.dll 庫,您可以在安裝了報表生成器的文件夾中找到該庫。在表單中添加一個按鈕來開始構建報表,不要忘記為它創(chuàng)建一個點擊處理程序。
首先,我們包含 FastReport 庫。
using FastReport;
using FastReport.Data;
using FastReport.Table;
using FastReport.Utils;
private void button1_Click(object sender, EventArgs e)
{
using (Report report = new Report())
// Create a report object
{
ReportPage page = new ReportPage();
//Create a report page object
page.Name = "Page1";
//Set the name of the page
report.Pages.Add(page);
//Add a page to the collection of report pages
DataSet ds = new DataSet();
//Create a data source
ds.ReadXml("~/../../../App_Data/nwind.xml");
//Load the xml database into it
report.RegisterData(ds);
//Register the data source in the report
report.GetDataSource("Products").Enabled = true;
//Enable the data source
DataBand dataBand = new DataBand();
//Create a data band
dataBand.Name = "DataBand";
//Specify the band name
page.Bands.Add(dataBand);
// Add a band to the page's band collection
TableObject table = new TableObject();
//Create a table object
table.Name = "Table1";
// Specify the name of the object
table.RowCount = 10;
// Specify the number of rows
table.ColumnCount = 2;
//Specify the number of columns
//Fill all the cells with some data in the loop
for (int i = 0; i < 10; i++)
for (int j = 0; j < 2; j++)
{
table[j, i].Text = (10 * i + j + 1).ToString();
table[j, i].Border.Lines = BorderLines.All;
}
dataBand.Objects.Add(table);
//dataBand.Objects.Add(picture);
if (report.Prepare())
report.ShowPrepared();
}
現(xiàn)在讓我們看一下需要用源數(shù)據(jù)填充表的示例,用另一個代碼替換上面的循環(huán):
table.Columns[0].AutoSize = true;
//table.Columns[1].AutoSize = true;
DataSourceBase data = report.GetDataSource("Products");
data.Init();
//Let’s initialize the data source
data.First();
//We get the first record
for (int i = 0; i < 10; i++)
{
table[0, i].Text = data["ProductName"].ToString();
table[0, i].Border.Lines = BorderLines.All;
//Let’s set the borderlines
table[1, i].Text = data["UnitPrice"].ToString();
table[1, i].Border.Lines = BorderLines.All;
data.Next();
}
最后,我們會從數(shù)據(jù)庫中得到一個包含數(shù)據(jù)的表:
沒有標題的表格看起來不完整,讓我們添加它:
table.RowCount = 11;
…
table[0, 0].Text ="Product Name";
table[0, 0].Border.Lines = BorderLines.All;
table[1, 0].Text = "Unit Price";
table[1, 0].Border.Lines = BorderLines.All;
for (int i = 1; i < 10; i++)
{
table[0, i].Text = data["ProductName"].ToString();
table[0, i].Border.Lines = BorderLines.All;
table[1, i].Text = data["UnitPrice"].ToString();
table[1, i].Border.Lines = BorderLines.All;
data.Next();
}
在表格的第一行中指定了標題,這意味著循環(huán)不是從第一個元素開始,而是從第二個元素開始。
接下來看看最后一個案例——如何在表格單元格中放置一個對象,例如一張圖片:
PictureObject picture = new PictureObject();
//Create a picture object
picture.Bounds = new RectangleF(40, 0, Units.Centimeters * 0.5f, Units.Centimeters * 0.5f);
// Set the size of the object
picture.CreateUniqueName();
//Set an arbitrary name
picture.Image = Image.FromFile("C:/Users/FR/Downloads/28.png");
//Specify the path to the image
picture.LoadImage();
//Load the image
picture.Parent = table[1, 1];
//Specify the parent object for the image
影響圖片在單元格中顯示方式的是 Parent 屬性,看看它的外觀:
因此,我們已經(jīng)了解了如何從應用程序代碼在報表中創(chuàng)建表格,以及使用靜態(tài)或動態(tài)數(shù)據(jù)填充表格的兩種方法。 此外,現(xiàn)在您知道如何以編程方式將對象添加到表格單元格。
報表生成器FastReport .NET是適用于.NET Core 3,ASP.NET,MVC和Windows窗體的全功能報告庫。使用FastReport .NET,您可以創(chuàng)建獨立于應用程序的.NET報告。
FastReport 技術交流群:702295239 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自:慧都網(wǎng)