特殊的StiReportResponse类包含两种报表打印模式:打印为PDF并以HTML 格式打印。例如,为一个报表实现这些模式以便比较它们。为每种打印模式添加两个按钮,并为这些按钮添加单击事件处理程序。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Print_Report_from_Code.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Print and Export Report from Code</title> </head> <body> <form id="form1" runat="server"> <br /><br /> <asp:Button ID="ButtonPrintPDF" runat="server" Text="Print Report as PDF" OnClick="ButtonPrintPdf_Click" /> <br /><br /> <asp:Button ID="ButtonPrintHTML" runat="server" Text="Print Report as HTML" OnClick="ButtonPrintHtml_Click" /> <br /><br /> </form> </body> </html>
在ButtonPrintPdf_Click事件中,它获取报表并调用StiReportResponse类的PrintAsPdf()静态方法。报表将自动生成,导出为PDF并发送打印 - 将显示系统打印对话框。
protected void ButtonPrintPdf_Click(object sender, EventArgs e) { StiReport report = this.GetReport(); StiReportResponse.PrintAsPdf(report); }
在ButtonPrintHtml_Click事件中,它获取报表并调用StiReportResponse类的PrintAsHtml()静态方法。报表将自动生成,导出为HTML并发送以进行打印 - 将显示系统打印对话框。
protected void ButtonPrintHtml_Click(object sender, EventArgs e) { StiReport report = this.GetReport(); StiReportResponse.PrintAsHtml(report); }
要获取报表,使用了GetReport()方法。此方法加载报表模板,加载XML数据文件并为加载的报表注册此数据。
private StiReport GetReport() { string reportPath = Server.MapPath("Reports/SimpleList.mrt"); StiReport report = new StiReport(); report.Load(reportPath); string dataPath = Server.MapPath("Data/Demo.xml"); DataSet data = new DataSet(); data.ReadXml(dataPath); report.RegData(data); return report; }
示例代码的结果如下图所示: