Stimulsoft Reports.Net示例演示:WinForms 类别中在运行时使用业务对象创建报告

发布时间 : 2021-09-17 11:00:00.977|阅读 106 次

概述:此示例说明如何使用代码中的 Business Objects 集合创建报告。

相关链接:

Stimulsoft Ultimate是用于创建报表和仪表板的通用工具集。该产品包括用于WinForms、ASP.NET、.NET Core、JavaScript、WPF、PHP、Java和其他环境的完整工具集。

Stimulsoft Reports不仅拥有强大的报表导出系统,而且还支持多种报表导出格式,拥有简单且强大的报表引擎。Stimulsoft Reports基本原则是,用简单常规的方法创建报表,将不同的技术应用于应用程序。Stimulsoft Reports ..NET一个基于.NET框架的报表生成器,能够帮助你创建结构、功能丰富的报表。不仅界面友好,而且使用便捷,能够让你轻松创建所有报表。

点击下载Stimulsoft Reports .NET v2021.3.1最新版

此示例说明如何使用代码中的 Business Objects 集合创建报告。Business Objects 是一个对象类数据,数据可以用不同的结构呈现:表格、列表、数组等。该FillBusinessObject()方法创建并填充数据集合:

private ArrayList list = null;

private void FillBusinessObject()
{
	list = new ArrayList();
	list.Add(new BusinessEntity("name1", "alias1"));
	list.Add(new BusinessEntity("name2", "alias2"));
	list.Add(new BusinessEntity("name3", "alias3"));
}

数据已创建,现在您需要显示它。首先,您应该在字典中创建一个新的报表和数据结构,将数据从 Business Object 传输到字典:

private void PrintDataGrid(DataGrid sender)
{
	StiReport report = new StiReport();
	report.ScriptLanguage = StiReportLanguageType.CSharp;
	
	// Add data to datastore
	report.RegData("MyList", list);
	
	// Fill dictionary
	report.Dictionary.Synchronize();
	StiPage page = report.Pages.Items[0];
	
...

然后您需要将组件添加到报告模板中。您应该创建带 Header、Data 和 Footer。这些带用于放置带有文本或对业务对象字段的引用的文本框:

...

	// Create HeaderBand
	StiHeaderBand headerBand = new StiHeaderBand();
	headerBand.Name = "HeaderBand";
	page.Components.Add(headerBand);
	
	// Create Databand
	StiDataBand dataBand = new StiDataBand();
	dataBand.DataSourceName = "MyList";
	dataBand.Height = 0.5f;
	dataBand.Name = "DataBand";
	page.Components.Add(dataBand);
	
	StiDataSource dataSource = report.Dictionary.DataSources[0];
	
	// Create texts
	Double pos = 0;
	Double columnWidth = StiAlignValue.AlignToMinGrid(page.Width / dataSource.Columns.Count, 0.1, true);
	int nameIndex = 1;
	foreach (StiDataColumn column in dataSource.Columns)
	{
		if (column.Name == "_ID" || column.Name == "_Current") continue;
		
		// Create text on header
		StiText headerText = new StiText(new RectangleD(pos, 0, columnWidth, 0.5f));
		headerText.Text.Value = column.Name;
		headerText.HorAlignment = StiTextHorAlignment.Center;
		headerText.Name = "HeaderText" + nameIndex.ToString();
		headerText.Brush = new StiSolidBrush(Color.MediumSeaGreen);
		headerText.Border.Side = StiBorderSides.All;
		headerBand.Components.Add(headerText);
		
		// Create text on Data Band
		StiText dataText = new StiText(new RectangleD(pos, 0, columnWidth, 0.5f));
		dataText.Text.Value = "{MyList." + column.Name + "}";
		dataText.Name = "DataText" + nameIndex.ToString();
		dataText.Border.Side = StiBorderSides.All;
		
		dataBand.Components.Add(dataText);
		
		pos += columnWidth;
		
		nameIndex ++;
	}
	
	// Create FooterBand
	StiFooterBand footerBand = new StiFooterBand();
	footerBand.Height = 0.5f;
	footerBand.Name = "FooterBand";
	page.Components.Add(footerBand);
	
	// Create text on footer
	StiText footerText = new StiText(new RectangleD(0, 0, page.Width, 0.5f));
	footerText.Text.Value = "Count - {Count()}";
	footerText.HorAlignment = StiTextHorAlignment.Right;
	footerText.Name = "FooterText";
	footerText.Brush = new StiSolidBrush(Color.LightGreen);
	footerBand.Components.Add(footerText);
	
...

在此之后,您可以呈现报告并将其显示在查看器中(或调用报告设计器):

...

	// Render without progress bar
	report.Render(false);
	
	report.Show();
	
	// For checking created report you can uncomment this line
	//report.Design();
}

在下面的屏幕截图中,您可以看到示例代码的结果:

Stimulsoft Reports.Net示例演示:WinForms 类别中在运行时使用业务对象创建报告

Aspose、E-iceblue、FastReport、Stimulsoft等文档/报表图表类开发工具享超低折扣,如有需要可直接联系在线客服


在线
客服
微信
QQ 电话
023-68661681
返回
顶部