ActiveReports報表實戰應用教程(十)—— 報表導出

ActiveReports支持多種格式的報表導出,包括PDF、Excel、Word、RTF、HTML、Text、TIFF以及其它圖片格式,用戶可以將它們應用到Windows Forms、Web、WPF、Silverlight等應用系統中。

在專業版的 ActiveReports 裏,對PDF格式的數據輸出又有了增強功能。現在用戶可以將不可見的數字簽名或者可見的文字圖案加入到報表裏。通過多種屬性對數字簽名進行個性化設置, 用數字簽名驗證報表作者,還可通過Certification Level 來設定用戶訪問權限。用時間印章功能建立第三方授權版本。這些新功能完全和Adobe的新安全機制兼容。

本文以客戶訂單爲例演示如何將 ActiveReports 報表導出爲各種格式。

點擊獲取ActiveReports最新版下載

1.創建報表文件

在應用程序中創建一個名爲 rptInvoice.rdlx 的 ActiveReports 報表文件,使用的項目模板爲 ActiveReports 頁面報表。

2. 打開報表資源管理器,並按照以下信息創建報表數據源

ActiveReports報表實戰應用教程(十)—— 報表導出

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 選中報表文件,並設置以下屬性:

ActiveReports報表實戰應用教程(十)—— 報表導出

4.2 從 VS 中將 Table 控件添加到報表設計界面,並按照以下列表設置相應屬性:

ActiveReports報表實戰應用教程(十)—— 報表導出

最終設計界面如下:

ActiveReports報表實戰應用教程(十)—— 報表導出

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();
}

 

ActiveReports 報表控件| 下載試用

ActiveReports 是一款專注於 .NET 平臺的報表控件,全面滿足 HTML5 / WinForm / ASP.NET / ASP.NET MVC / WPF 等平臺下報表設計和開發工作需求,作爲專業的報表工具爲全球超過 300,000 開發人員提供了全面的報表開發服務。

本文轉載自葡萄城

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章