ActiveReports Report Application Tutorial (16) --- Report Export

original
2014/06/11 12:01
Reading number 308

ActiveReports supports report export in various formats, including PDF, Excel, Word, RTF, HTML, Text, TIFF and other image formats. Users can apply them to Windows Forms, Web, WPF, Silverlight and other application systems.

In the professional version of ActiveReports, data output in PDF format has been enhanced. Now users can add invisible digital signatures or visible text patterns to the report. Personalize digital signatures through multiple attributes, verify report authors with digital signatures, and set user access permissions through Certification Level. Use the time stamp function to create a third-party authorized version. These new features are fully compatible with Adobe's new security mechanisms.

This article uses customer orders as an example to demonstrate how to export ActiveReports reports to various formats.

1. Create Report File

Create an ActiveReports report file named rptInvoice.rdlx in the application. The project template used is the ActiveReports page report.

2. Open Report Explorer and create a report data source according to the following information

name: NWind_CHS
Type: Micorsoft OleDb Provider
OLE DB provider: Microsoft.Jet.OLEDB.4.0
Server or file name: Data\NWind_CHS.mdb

3. Add Dataset

Right click the new NWind_CHS data source and select Add Dataset to add the following two datasets:

General - Name: OrderDetails

Query - Query:

 SELECT TOP 10 Order. Order ID, order. customer ID, order. order date, product. product name, order details. quantity, order details. unit price, order details. discount, order. owner city, order. owner address, order. owner name, order. owner zip code, customer. telephone
 FROM (Order INNER JOIN order details  ON Order. Order ID=Order Details. Order ID) INNER JOIN product ON Order details. Product ID=Product. Product ID) INNER JOIN customer ON Order.customer ID=Customer.customer ID
 ORDER BY Order. Order Date DESC ;

 

4. Design report interface

4.1 Select a report file and set the following attributes:

General - Dataset name: OrderDetails
grouping: Name: FixedPage1_Group
Expression:=[Order ID]

 

4.2. Add the Table control to the report design interface from VS, and set the corresponding properties according to the following list:

form attribute
DataSetname OrderDetails
FixedSize 19cm*15.75cm
RepeatHeaderOnNewPage True
RepeatToFill True
Cell attribute
Cells[2,1] Value:=RowNumber("Table1")
Cells[2,2] Value:=Fields! Product name Value
Cells[2,3] Value:=Fields! number. Value
Cells[2,4] Value:=Fields! Unit Price. Value
Cells[2,5] Value:=Fields! number. Value * Fields! Unit Price. Value
Total Cells attribute
TextBox38 Value:=Sum(Fields! number. Value * Fields! Unit Price. Value, "FixedPage1_Group")
TextBox42 Value:=ReportItems!TextBox38.Value * 0.17
TextBox39 Value:=ReportItems!TextBox38.Value + ReportItems! TextBox42.Value

 

The final design interface is as follows:

 The ActiveReports report function displays exported reports

5. Add report export function

5.1 Excel export code:

 protected void btnExcel_Click( object sender, EventArgs e)
 {
 GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport( new System.IO.FileInfo(Server. MapPath(" ../ ") + " Reports/ " + report + " .rdlx "));
 _reportDef. Report.DataSources[0]. DataSourceReference = Server.MapPath(" ../Data/NWind_CHS_Access.rdsx ");
 GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
 GrapeCity.ActiveReports.Export.Excel.Section.XlsExport XlsExport1 = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport();
 System.IO.MemoryStream ms = new System.IO.MemoryStream();
 XlsExport1.FileFormat = GrapeCity.ActiveReports.Export.Excel.Section.FileFormat.Xlsx;
 XlsExport1.Export(_reportRuntime, ms);
 Response.ContentType = " application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ";
 Response.AddHeader(" content-disposition ", Server.UrlPathEncode(" Attachment; filename=Customer Order.xlsx "));
 Response.BinaryWrite(ms.ToArray());
 Response.End();
 }

 

5.2. Word export code:

 protected void btnWord_Click( object sender, EventArgs e)
 {
 GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport( new System.IO.FileInfo(Server. MapPath(" ../ ") + " Reports/ " + report + " .rdlx "));
 _reportDef. Report.DataSources[0]. DataSourceReference = Server.MapPath(" ../Data/NWind_CHS_Access.rdsx ");
 GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
 GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension _renderingExtension = new GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension();
 GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
 GrapeCity.ActiveReports.Export.Word.Page.Settings s = new GrapeCity.ActiveReports.Export.Word.Page.Settings();
 s.UseMhtOutput = true ;
 _reportRuntime. Render(_renderingExtension, _provider, s);
 Response.ContentType = " application/msword ";
 Response.AddHeader(" content-disposition ", Server.UrlPathEncode(" Attachment; filename=Customer Order.doc "));
 System.IO.MemoryStream ms = new System.IO.MemoryStream();
 _provider. GetPrimaryStream(). OpenStream(). CopyTo(ms);
 Response.BinaryWrite(ms.ToArray());
 Response.End();
 }

 

5.3. Regular PDF export code:

 protected void btnPdf_Click( object sender, EventArgs e)
 {
 GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport( new System.IO.FileInfo(Server. MapPath(" ../ ") + " Reports/ " + report + " .rdlx "));
 _reportDef. Report.DataSources[0]. DataSourceReference = Server.MapPath(" ../Data/NWind_CHS_Access.rdsx ");
 GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
 GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension _renderingExtension = new GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension();
 GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
 _reportRuntime. Render(_renderingExtension, _provider);
 Response.ContentType = " application/pdf ";
 Response.AddHeader(" content-disposition ", Server.UrlPathEncode(" Attachment; filename=Customer Order.pdf "));
 System.IO.MemoryStream ms = new System.IO.MemoryStream();
 _provider. GetPrimaryStream(). OpenStream(). CopyTo(ms);
 Response.BinaryWrite(ms.ToArray());
 Response.End();
 }

 

5.4 HTML export code:

 protected void btnHtml_Click( object sender, EventArgs e)
 {
 GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport( new System.IO.FileInfo(Server. MapPath(" ../ ") + " Reports/ " + report + " .rdlx "));
 _reportDef. Report.DataSources[0]. DataSourceReference = Server.MapPath(" ../Data/NWind_CHS_Access.rdsx ");
 GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
 GrapeCity.ActiveReports.Export.Html.Page.HtmlRenderingExtension _renderingExtension = new GrapeCity.ActiveReports.Export.Html.Page.HtmlRenderingExtension();
 GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
 GrapeCity.ActiveReports.Export.Html.Page.Settings s = new GrapeCity.ActiveReports.Export.Html.Page.Settings();
 s.StyleStream = false ;
 s.MhtOutput = false ;
 s.Fragment = false ;
 s.OutputTOC = true ;
 s.Mode = GrapeCity.ActiveReports.Export.Html.Page.RenderMode.Paginated;
 _reportRuntime. Render(_renderingExtension, _provider, s);
 Response.ContentType = " text/html ";
 Response.AddHeader(" content-disposition ", Server.UrlPathEncode(" Attachment; filename=Customer Order.html "));
 System.IO.MemoryStream ms = new System.IO.MemoryStream();
 _provider. GetPrimaryStream(). OpenStream(). CopyTo(ms);
 Response.BinaryWrite(ms.ToArray());
 Response.End();
 }

 

5.5 Text export code:

 protected void btnText_Click( object sender, EventArgs e)
 {
 GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport( new System.IO.FileInfo(Server. MapPath(" ../ ") + " Reports/ " + report + " .rdlx "));
 _reportDef. Report.DataSources[0]. DataSourceReference = Server.MapPath(" ../Data/NWind_CHS_Access.rdsx ");
 GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
 GrapeCity.ActiveReports.Export.Xml.Section.TextExport txtExport1 = new GrapeCity.ActiveReports.Export.Xml.Section.TextExport();
 txtExport1.Encoding = Encoding.Unicode;
 System.IO.MemoryStream ms = new System.IO.MemoryStream();
 txtExport1.Export(_reportRuntime, ms);
 Response.ContentType = " text/plain ";
 Response.AddHeader(" content-disposition ", Server.UrlPathEncode(" Attachment; filename=Customer Order.txt "));
 Response.BinaryWrite(ms.ToArray());
 Response.End();
 }

 

5.6 CSV export code:

 protected void btnCSV_Click( object sender, EventArgs e)
 {
 GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport( new System.IO.FileInfo(Server. MapPath(" ../ ") + " Reports/ " + report + " .rdlx "));
 _reportDef. Report.DataSources[0]. DataSourceReference = Server.MapPath(" ../Data/NWind_CHS_Access.rdsx ");
 GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
 GrapeCity.ActiveReports.Export.Xml.Section.TextExport csvExport1 = new GrapeCity.ActiveReports.Export.Xml.Section.TextExport();
 csvExport1.Encoding = Encoding.Unicode;
 csvExport1.TextDelimiter = " \t ";
 csvExport1.SuppressEmptyLines = true ;
 System.IO.MemoryStream ms = new System.IO.MemoryStream();
 csvExport1.Export(_reportRuntime, ms);
 Response.ContentType = " text/plain ";
 Response.AddHeader(" content-disposition ", Server.UrlPathEncode(" Attachment; filename=Customer Order.csv "));
 Response.BinaryWrite(ms.ToArray());
 Response.End();
 }

 

5.7 Advanced PDF export code:

 protected void btnExport_Click( object sender, EventArgs e)
 {
 GrapeCity.ActiveReports.PageReport report = new GrapeCity.ActiveReports.PageReport( new System.IO.FileInfo(Server. MapPath(" ../Reports/ " + reportname + " .rdlx ")));
 report.Report.DataSources[0]. DataSourceReference = "";
 report.Report.DataSources[0]. ConnectionProperties.DataProvider = " OLEDB ";
 report.Report.DataSources[0]. ConnectionProperties.ConnectString = string .Format(" Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; ", Server.MapPath(" ../Data/NWind_CHS.mdb "));
 GrapeCity.ActiveReports.Document.PageDocument document = new GrapeCity.ActiveReports.Document.PageDocument(report);
 GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport pdfExport1 = new GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport();
 switch (C1Tabs1.Selected)
 {
 case 0:
 break ;
 case 1:
 pdfExport1.Security.Encrypt = true ;
 pdfExport1.Security.Use128Bit = true ;
 if (txtPwd.Text.Length > 0)
 pdfExport1.Security.UserPassword = txtPwd.Text;
 pdfExport1.Security.Permissions = GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.None;
 if (chkCopy.Checked)
 pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowCopy;
 if (chkEidt1.Checked)
 {
 pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowFillIn;
 pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowModifyAnnotations;
 pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowModifyContents;
 }
 if (chkPrint.Checked)
 pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowPrint;
 break ;
 case 2:
 // ImageText signature.
 pdfExport1.Signature.VisibilityType = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.VisibilityType.ImageText;
 // Bounds (Container of Text & Image).
 pdfExport1.Signature.Stamp.Bounds = new RectangleF(0, 0, 4, 1);
 // Text area.
 pdfExport1.Signature.Stamp.TextAlignment = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.Alignment.Left;
 pdfExport1.Signature.Stamp.Font = new Font(" Comic Sans MS ", 8, FontStyle.Regular);
 // Note: Specify (x, y) in relative coordinate from Bounds top-left.
 pdfExport1.Signature.Stamp.TextRectangle = new RectangleF(1, 0, 3, 1);
 // Image area.
 pdfExport1.Signature.Stamp.Image = System.Drawing.Image.FromFile(Server. MapPath(" ../Resources/Grapecity_powertools_bg.png "));
 pdfExport1.Signature.Stamp.ImageAlignment = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.Alignment.Center;
 // Note: Specify (x, y) in relative coordinate from Bounds top-left.
 pdfExport1.Signature.Stamp.ImageRectangle = new RectangleF(0, 0, 1, 1);
 // Set certificate & password.
 pdfExport1.Signature.Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(Server. MapPath(" ../Resources/ActiveReports6.pfx "), " one hundred and twenty-three thousand four hundred and fifty-six ");
 // set the certifiation level
 if (chkEidt2.Checked)
 pdfExport1.Signature.CertificationLevel = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.CertificationLevel.FormFillingAnnotations;
 else
 pdfExport1.Signature.CertificationLevel = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.CertificationLevel.NoChangesAllowed;
 //Signature items.
 pdfExport1.Signature.Contact = new GrapeCity.ActiveReports.Export.Pdf.Section.Signing.SignatureField< string >(txtEmail.Text, true );
 if (chkDate.Checked)
 pdfExport1.Signature.SignDate = new GrapeCity.ActiveReports.Export.Pdf.Section.Signing.SignatureField<System.DateTime>(System. DateTime.Now, true );
 break ;
 default :
 break ;
 }
 System.IO.MemoryStream ms = new System.IO.MemoryStream();
 pdfExport1.Export(document,ms);
 Response.ContentType = " application/pdf ";
 Response.AddHeader(" content-disposition ", Server.UrlPathEncode(" Attachment; filename=Customer Order.pdf "));
 Response.BinaryWrite(ms.ToArray());
 Response.End();
 }

 

Online demonstration and source code download address:


http://www.gcpowertools.com.cn/products/activereports_demo.htm
 ActiveReports report function display

 

 

This article is from“ Grapevine Control Blog ”Blog, please keep this source http://powertoolsteam.blog.51cto.com/2369428/1255874

Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
zero Collection
zero fabulous
 Back to top
Top