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