葡萄城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
查詢-查詢:
SELECT TOP 10 訂單.訂單ID, 訂單.客戶ID, 訂單.訂購日期, 產品.產品名稱, 訂單明細.數量, 訂單明細.單價, 訂單明細.折扣, 訂單.貨主城市, 訂單.貨主地址, 訂單.貨主名稱, 訂單.貨主郵政編碼, 客戶.電話
FROM ((訂單 INNER JOIN 訂單明細 ON 訂單.訂單ID = 訂單明細.訂單ID) INNER JOIN 產品 ON 訂單明細.產品ID = 產品.產品ID) INNER JOIN 客戶 ON 訂單.客戶ID = 客戶.客戶ID
ORDER BY 訂單.訂購日期 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導出代碼:
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=客戶訂單.xlsx"));
Response.BinaryWrite(ms.ToArray());
Response.End();
}
5.2、Word導出代碼:
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=客戶訂單.doc"));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
_provider.GetPrimaryStream().OpenStream().CopyTo(ms);
Response.BinaryWrite(ms.ToArray());
Response.End();
}
5.3、常規PDF導出代碼:
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=客戶訂單.pdf"));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
_provider.GetPrimaryStream().OpenStream().CopyTo(ms);
Response.BinaryWrite(ms.ToArray());
Response.End();
}
5.4、HTML導出代碼:
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=客戶訂單.html"));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
_provider.GetPrimaryStream().OpenStream().CopyTo(ms);
Response.BinaryWrite(ms.ToArray());
Response.End();
}
5.5、 Text導出代碼:
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=客戶訂單.txt"));
Response.BinaryWrite(ms.ToArray());
Response.End();
}
5.6、CSV導出代碼:
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=客戶訂單.csv"));
Response.BinaryWrite(ms.ToArray());
Response.End();
}
5.7、高級PDF導出代碼:
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"), "123456");
// 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=客戶訂單.pdf"));
Response.BinaryWrite(ms.ToArray());
Response.End();
}
在線演示及源碼下載地址:
http://www.gcpowertools.com.cn/products/activereports_demo.htm
相關閱讀:
文章列表