ActiveReports 支持多种格式的报表导出,包括PDF、Excel、Word、RTF、HTML、Text、TIFF以及其它图片格式,用户可以将它们应用到Windows Forms、Web、WPF、Silverlight等应用系统中。
在专业版的 ActiveReports 里,对PDF格式的数据输出又有了增强功能。现在用户可以将不可见的数字签名或者可见的文字图案加入到报表里。通过多种属性对数字签名进行个性化设置, 用数字签名验证报表作者,还可通过Certification Level 来设定用户访问权限。用时间印章功能建立第三方授权版本。这些新功能完全和Adobe的新安全机制兼容。
本文以客户订单为例演示如何将 ActiveReports 报表导出为各种格式。
1、创建报表文件
在应用程序中创建一个名为 rptInvoice.rdlx 的 ActiveReports 报表文件,使用的项目模板为 ActiveReports 页面报表。
2、打开报表资源管理器,并按照以下信息创建报表数据源
名称:
NWind_CHS
类型:
Micorsoft OleDb Provider
OLE DB 提供程序:
Microsoft.Jet.OLEDB.4.0
服务器或文件名称:
Data\NWind_CHS.mdb
3、 添加数据集
在新建的 NWind_CHS 数据源上鼠标右键并选择添加数据集菜单项,添加以下两个数据集:
常规-名称:OrderDetails
查询-查询:
1SELECTTOP 10 订单.订单ID, 订单.客户ID, 订单.订购日期, 产品.产品名称, 订单明细.数量, 订单明细.单价, 订单明细.折扣, 订单.货主城市, 订单.货主地址, 订单.货主名称, 订单.货主邮政编码, 客户.电话 2 3FROM ((订单 INNERJOIN 订单明细 ON 订单.订单ID = 订单明细.订单ID) INNERJOIN 产品 ON 订单明细.产品ID = 产品.产品ID) INNERJOIN 客户 ON 订单.客户ID = 客户.客户ID 4 5ORDERBY 订单.订购日期 DESC;
4、设计报表界面
4.1、选中报表文件,并设置以下属性:
常规-数据集名称:
OrderDetails
分组:
名称:FixedPage1_Group
表达式:=[订单ID]
4.2、从 VS 中将 Table 控件添加到报表设计界面,并按照以下列表设置相应属性:
表格
属性
DataSetname
OrderDetails
FixedSize
19cm*15.75cm
RepeatHeaderOnNewPage
True
RepeatToFill
True
单元格
属性
Cells[2,1]
Value:=RowNumber("Table1")
Cells[2,2]
Value:=Fields!产品名称.Value
Cells[2,3]
Value:=Fields!数量.Value
Cells[2,4]
Value:=Fields!单价.Value
Cells[2,5]
Value:=Fields!数量.Value * Fields!单价.Value
合计单元格
属性
TextBox38
Value:=Sum(Fields!数量.Value * Fields!单价.Value,"FixedPage1_Group")
TextBox42
Value:=ReportItems!TextBox38.Value * 0.17
TextBox39
Value:=ReportItems!TextBox38.Value + ReportItems!TextBox42.Value
最终设计界面如下:
5、添加报表导出功能
5.1、Excel导出代码:
1protectedvoid btnExcel_Click(object sender, EventArgs e) 2 3{ 4 5GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("../") + "Reports/" + report + ".rdlx")); 6 7_reportDef.Report.DataSources[0].DataSourceReference = Server.MapPath("../Data/NWind_CHS_Access.rdsx"); 8 9GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef); 10 11GrapeCity.ActiveReports.Export.Excel.Section.XlsExport XlsExport1 = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport(); 12 13System.IO.MemoryStream ms = new System.IO.MemoryStream(); 14 15XlsExport1.FileFormat = GrapeCity.ActiveReports.Export.Excel.Section.FileFormat.Xlsx; 16 17XlsExport1.Export(_reportRuntime, ms); 18 19Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 20 21Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.xlsx")); 22 23Response.BinaryWrite(ms.ToArray()); 24 25Response.End(); 26 27}
5.2、Word导出代码:
1protectedvoid btnWord_Click(object sender, EventArgs e) 2 3{ 4 5GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("../") + "Reports/" + report + ".rdlx")); 6 7_reportDef.Report.DataSources[0].DataSourceReference = Server.MapPath("../Data/NWind_CHS_Access.rdsx"); 8 9GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef); 10 11GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension _renderingExtension = new GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension(); 12 13GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider(); 14 15GrapeCity.ActiveReports.Export.Word.Page.Settings s = new GrapeCity.ActiveReports.Export.Word.Page.Settings(); 16 17s.UseMhtOutput = true; 18 19_reportRuntime.Render(_renderingExtension, _provider, s); 20 21Response.ContentType = "application/msword"; 22 23Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.doc")); 24 25System.IO.MemoryStream ms = new System.IO.MemoryStream(); 26 27_provider.GetPrimaryStream().OpenStream().CopyTo(ms); 28 29Response.BinaryWrite(ms.ToArray()); 30 31Response.End(); 32 33}
5.3、常规PDF导出代码:
1protectedvoid btnPdf_Click(object sender, EventArgs e) 2 3{ 4 5GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("../") + "Reports/" + report + ".rdlx")); 6 7_reportDef.Report.DataSources[0].DataSourceReference = Server.MapPath("../Data/NWind_CHS_Access.rdsx"); 8 9GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef); 10 11GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension _renderingExtension = new GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension(); 12 13GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider(); 14 15_reportRuntime.Render(_renderingExtension, _provider); 16 17Response.ContentType = "application/pdf"; 18 19Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.pdf")); 20 21System.IO.MemoryStream ms = new System.IO.MemoryStream(); 22 23_provider.GetPrimaryStream().OpenStream().CopyTo(ms); 24 25Response.BinaryWrite(ms.ToArray()); 26 27Response.End(); 28 29}
5.4、HTML导出代码:
1protectedvoid btnHtml_Click(object sender, EventArgs e) 2 3{ 4 5GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("../") + "Reports/" + report + ".rdlx")); 6 7_reportDef.Report.DataSources[0].DataSourceReference = Server.MapPath("../Data/NWind_CHS_Access.rdsx"); 8 9GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef); 10 11GrapeCity.ActiveReports.Export.Html.Page.HtmlRenderingExtension _renderingExtension = new GrapeCity.ActiveReports.Export.Html.Page.HtmlRenderingExtension(); 12 13GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider(); 14 15GrapeCity.ActiveReports.Export.Html.Page.Settings s = new GrapeCity.ActiveReports.Export.Html.Page.Settings(); 16 17s.StyleStream = false; 18 19s.MhtOutput = false; 20 21s.Fragment = false; 22 23s.OutputTOC = true; 24 25s.Mode = GrapeCity.ActiveReports.Export.Html.Page.RenderMode.Paginated; 26 27_reportRuntime.Render(_renderingExtension, _provider, s); 28 29Response.ContentType = "text/html"; 30 31Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.html")); 32 33System.IO.MemoryStream ms = new System.IO.MemoryStream(); 34 35_provider.GetPrimaryStream().OpenStream().CopyTo(ms); 36 37Response.BinaryWrite(ms.ToArray()); 38 39Response.End(); 40 41}
5.5、 Text导出代码:
1protectedvoid btnText_Click(object sender, EventArgs e) 2 3{ 4 5GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("../") + "Reports/" + report + ".rdlx")); 6 7_reportDef.Report.DataSources[0].DataSourceReference = Server.MapPath("../Data/NWind_CHS_Access.rdsx"); 8 9GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef); 10 11GrapeCity.ActiveReports.Export.Xml.Section.TextExport txtExport1 = new GrapeCity.ActiveReports.Export.Xml.Section.TextExport(); 12 13txtExport1.Encoding = Encoding.Unicode; 14 15System.IO.MemoryStream ms = new System.IO.MemoryStream(); 16 17txtExport1.Export(_reportRuntime, ms); 18 19Response.ContentType = "text/plain"; 20 21Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.txt")); 22 23Response.BinaryWrite(ms.ToArray()); 24 25Response.End(); 26 27}
5.6、CSV导出代码:
1protectedvoid btnCSV_Click(object sender, EventArgs e) 2 3{ 4 5GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("../") + "Reports/" + report + ".rdlx")); 6 7_reportDef.Report.DataSources[0].DataSourceReference = Server.MapPath("../Data/NWind_CHS_Access.rdsx"); 8 9GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef); 10 11GrapeCity.ActiveReports.Export.Xml.Section.TextExport csvExport1 = new GrapeCity.ActiveReports.Export.Xml.Section.TextExport(); 12 13csvExport1.Encoding = Encoding.Unicode; 14 15csvExport1.TextDelimiter = "\t"; 16 17csvExport1.SuppressEmptyLines = true; 18 19System.IO.MemoryStream ms = new System.IO.MemoryStream(); 20 21csvExport1.Export(_reportRuntime, ms); 22 23Response.ContentType = "text/plain"; 24 25Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.csv")); 26 27Response.BinaryWrite(ms.ToArray()); 28 29Response.End(); 30 31}
5.7、高级PDF导出代码:
1protectedvoid btnExport_Click(object sender, EventArgs e) 2 3{ 4 5GrapeCity.ActiveReports.PageReport report = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("../Reports/" + reportname + ".rdlx"))); 6 7report.Report.DataSources[0].DataSourceReference = ""; 8 9report.Report.DataSources[0].ConnectionProperties.DataProvider = "OLEDB"; 10 11report.Report.DataSources[0].ConnectionProperties.ConnectString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", Server.MapPath("../Data/NWind_CHS.mdb")); 12 13GrapeCity.ActiveReports.Document.PageDocument document = new GrapeCity.ActiveReports.Document.PageDocument(report); 14 15GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport pdfExport1 = new GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport(); 16 17switch (C1Tabs1.Selected) 18 19{ 20 21case 0: 22 23break; 24 25case 1: 26 27pdfExport1.Security.Encrypt = true; 28 29pdfExport1.Security.Use128Bit = true; 30 31if (txtPwd.Text.Length > 0) 32 33pdfExport1.Security.UserPassword = txtPwd.Text; 34 35pdfExport1.Security.Permissions = GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.None; 36 37if (chkCopy.Checked) 38 39pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowCopy; 40 41if (chkEidt1.Checked) 42 43{ 44 45pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowFillIn; 46 47pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowModifyAnnotations; 48 49pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowModifyContents; 50 51} 52 53if (chkPrint.Checked) 54 55pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowPrint; 56 57break; 58 59case 2: 60 61// ImageText signature. 62 63pdfExport1.Signature.VisibilityType = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.VisibilityType.ImageText; 64 65// Bounds (Container of Text & Image). 66 67pdfExport1.Signature.Stamp.Bounds = new RectangleF(0, 0, 4, 1); 68 69// Text area. 70 71pdfExport1.Signature.Stamp.TextAlignment = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.Alignment.Left; 72 73pdfExport1.Signature.Stamp.Font = new Font("Comic Sans MS", 8, FontStyle.Regular); 74 75// Note: Specify (x, y) in relative coordinate from Bounds top-left. 76 77pdfExport1.Signature.Stamp.TextRectangle = new RectangleF(1, 0, 3, 1); 78 79// Image area. 80 81pdfExport1.Signature.Stamp.Image = System.Drawing.Image.FromFile(Server.MapPath("../Resources/Grapecity_powertools_bg.png")); 82 83pdfExport1.Signature.Stamp.ImageAlignment = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.Alignment.Center; 84 85// Note: Specify (x, y) in relative coordinate from Bounds top-left. 86 87pdfExport1.Signature.Stamp.ImageRectangle = new RectangleF(0, 0, 1, 1); 88 89// Set certificate & password. 90 91pdfExport1.Signature.Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(Server.MapPath("../Resources/ActiveReports6.pfx"), "123456"); 92 93// set the certifiation level 94 95if (chkEidt2.Checked) 96 97pdfExport1.Signature.CertificationLevel = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.CertificationLevel.FormFillingAnnotations; 98 99else 100 101pdfExport1.Signature.CertificationLevel = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.CertificationLevel.NoChangesAllowed; 102 103//Signature items. 104 105pdfExport1.Signature.Contact = new GrapeCity.ActiveReports.Export.Pdf.Section.Signing.SignatureField<string>(txtEmail.Text, true); 106 107if (chkDate.Checked) 108 109pdfExport1.Signature.SignDate = new GrapeCity.ActiveReports.Export.Pdf.Section.Signing.SignatureField<System.DateTime>(System.DateTime.Now, true); 110 111break; 112 113default: 114 115break; 116 117} 118 119System.IO.MemoryStream ms = new System.IO.MemoryStream(); 120 121pdfExport1.Export(document,ms); 122 123Response.ContentType = "application/pdf"; 124 125Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.pdf")); 126 127Response.BinaryWrite(ms.ToArray()); 128 129Response.End(); 130 131}
在线演示及源码下载地址:
http://www.gcpowertools.com.cn/products/activereports_demo.htm

本文出自 “葡萄城控件博客” 博客,请务必保留此出处http://powertoolsteam.blog.51cto.com/2369428/1255874
