翻譯|使用教程|編輯:龔雪|2022-02-22 10:21:14.093|閱讀 612 次
概述:本文主要為大家介紹如何以編程方式從本機(jī)代碼創(chuàng)建表格報(bào)表,歡迎下載最新版體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
FastReport .Net v2022.1官方正式版下載
一個相當(dāng)常見的情況是,當(dāng)您需要非常快速的做出決策或使用可用信息時,不要忘記報(bào)表的結(jié)構(gòu)和內(nèi)容取決于外部因素。FastReport .NET 報(bào)表生成器是一款非常靈活的產(chǎn)品,它為您提供了兩種解決此問題的方法。
關(guān)于如何在報(bào)表腳本中實(shí)現(xiàn)某些操作的特征信息非常多,但是有不少示例展示了如何從代碼創(chuàng)建報(bào)表。在本文中,我們將使用應(yīng)用程序代碼創(chuàng)建一個包含表格的報(bào)表,還將了解如何用數(shù)據(jù)填充表格并將對象放置在表格單元格中。
我們可以用靜態(tài)或動態(tài)數(shù)據(jù)填充 Table 對象。 在第一種情況下,我們知道表格的具體維度,并立即將數(shù)據(jù)從固定集輸入到單元格中。
在第二種情況下,表是動態(tài)創(chuàng)建的,根據(jù)源中的數(shù)據(jù)添加行(列),但是您也可以限制表格的大小并對其進(jìn)行修復(fù)。
讓我們創(chuàng)建一個 WinForms 應(yīng)用程序,在鏈接中包含 FastReport.dll 庫,您可以在安裝了報(bào)表生成器的文件夾中找到該庫。在表單中添加一個按鈕來開始構(gòu)建報(bào)表,不要忘記為它創(chuàng)建一個點(diǎn)擊處理程序。
首先,我們包含 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ù)的表:
沒有標(biāo)題的表格看起來不完整,讓我們添加它:
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(); }
在表格的第一行中指定了標(biāo)題,這意味著循環(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)了解了如何從應(yīng)用程序代碼在報(bào)表中創(chuàng)建表格,以及使用靜態(tài)或動態(tài)數(shù)據(jù)填充表格的兩種方法。 此外,現(xiàn)在您知道如何以編程方式將對象添加到表格單元格。
報(bào)表生成器FastReport .NET是適用于.NET Core 3,ASP.NET,MVC和Windows窗體的全功能報(bào)告庫。使用FastReport .NET,您可以創(chuàng)建獨(dú)立于應(yīng)用程序的.NET報(bào)告。
FastReport 技術(shù)交流群:702295239 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)