一个相当常见的情况是,当您需要非常快速的做出决策或使用可用信息时,不要忘记 表的结构和内容取决于外部因素。FastReport .NET 表生成器是一款非常灵活的产品,它为您提供了两种解决此问题的方法。
- 方法 1. 在自定义应用程序的代码中创建 表结构。
- 方法2. 使用脚本控制 表里面的操作。
我们可以用静态或动态数据填充 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 objectpage.Name = "Page1";//Set the name of the pagereport.Pages.Add(page);//Add a page to the collection of report pagesDataSet ds = new DataSet();//Create a data sourceds.ReadXml("~/../../../App_Data/nwind.xml");//Load the xml database into itreport.RegisterData(ds);//Register the data source in the reportreport.GetDataSource("Products").Enabled = true;//Enable the data sourceDataBand dataBand = new DataBand();//Create a data banddataBand.Name = "DataBand";//Specify the band namepage.Bands.Add(dataBand);// Add a band to the page's band collectionTableObject table = new TableObject();//Create a table objecttable.Name = "Table1";// Specify the name of the objecttable.RowCount = 10;// Specify the number of rowstable.ColumnCount = 2;//Specify the number of columns//Fill all the cells with some data in the loopfor (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 sourcedata.First();//We get the first recordfor (int i = 0; i < 10; i++){table[0, i].Text = data["ProductName"].ToString();table[0, i].Border.Lines = BorderLines.All;//Let’s set the borderlinestable[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 objectpicture.Bounds = new RectangleF(40, 0, Units.Centimeters * 0.5f, Units.Centimeters * 0.5f);// Set the size of the objectpicture.CreateUniqueName();//Set an arbitrary namepicture.Image = Image.FromFile("C:/Users/FR/Downloads/28.png");//Specify the path to the imagepicture.LoadImage();//Load the imagepicture.Parent = table[1, 1];//Specify the parent object for the image
影响图片在单元格中显示方式的是 Parent 属性,看看它的外观:

因此,我们已经了解了如何从应用程序代码在 表中创建表格,以及使用静态或动态数据填充表格的两种方法。 此外,现在您知道如何以编程方式将对象添加到表格单元格。
FastReport .Net | 下载试用
表生成器FastReport .NET是适用于.NET Core 3,ASP.NET,MVC和Windows窗体的全功能 告库。使用FastReport .NET,您可以创建独立于应用程序的.NET 告。
FastReport 技术交流群:702295239 欢迎一起进群讨论
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!