您可以动态构建一个报表,该报表将基于指定的 DataSet 输出一个简单的表,实际上是一个 System.Data.DataTable。基本上对于 DataTable 中的每一列,在标题中添加一个文本框来保存列的名称,并在 Detail 部分添加另一个文本框来保存值。
对于详细信息部分中的文本框,将其 DataField 属性设置为列的名称。绑定到位后,您可以将报表的 DataSource 属性设置为 DataTable,然后运行报表并将其导出为 PDF。
以下代码是一个基本示例:
var left = 0f;
var width = 1f;
var height = .25f;
var space = .25f;
var rpt = new SectionReport();
rpt.Sections.Add(SectionType.ReportHeader, "rh").Height = height;
rpt.Sections.Add(SectionType.Detail, "detail").Height = height;
rpt.Sections.Add(SectionType.ReportFooter, "rf").Height = height;
foreach (System.Data.DataColumn col in dataTable.Columns)
{
var txt = new TextBox { Location = new PointF(left, 0), Size = new SizeF(width, height) };
txt.Text = col.ColumnName;
rpt.Sections["rh"].Controls.Add(txt);
txt = new TextBox { Location = new PointF(left, 0), Size = new SizeF(width, height) };
txt.DataField = col.ColumnName;
rpt.Sections["detail"].Controls.Add(txt);
left += width + space;
}
rpt.DataSource = dataTable;
rpt.Run();
var pdf = new PdfExport();
pdf.Export(rpt.Document, @"c:\Users\scott\downloads\test.pdf");