您可以動態構建,將輸出一個簡單的表基於指定的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");
BTW:ActiveReports中7確實有支持RTF及HTML的輸出RichTextBox控件,但我覺得上面的方法是根據您的需求更好的方法。 – 2013-03-09 18:35:25
嗯 - 感謝發佈 - 我可以使用這種方法!我喜歡它的程序化。 – user1778606 2013-03-10 02:32:39