发布时间 : 2019-10-30 14:18:58.587|阅读 297 次
概述:此示例项目说明了如何在不使用报表查看器的情况下从代码导出和打印报表。为此,使用特殊的StiReportResponse类足以实现报表导出和打印的方法。这些方法输入所有必需的参数,以配置导出和打印报表。
相关链接:
立即点击下载Stimulsoft Reports.Net最新版
此示例项目说明了如何在不使用报表查看器的情况下从代码导出和打印报表。为此,使用特殊的StiReportResponse类足以实现报表导出和打印的方法。这些方法输入所有必需的参数,以配置导出和打印报表。
特殊的StiReportResponse类包含用于导出任何格式的报表并将报表打印为PDF和HTML的方法。例如,为一个报表实施它以进行比较。为每种打印模式添加两个链接,为各种导出格式添加三个链接。
<div class="row"> <div class="col-md-4"> <h2>Print to PDF</h2> <p>@Html.ActionLink("Print", "PrintPdf")</p> </div> <div class="col-md-4"> <h2>Print to HTML</h2> <p>@Html.ActionLink("Print", "PrintHtml")</p> </div> </div> <hr /> <div class="row"> <div class="col-md-4"> <h2>Export to PDF</h2> <p>@Html.ActionLink("Export", "ExportPdf")</p> </div> <div class="col-md-4"> <h2>Export to HTML</h2> <p>@Html.ActionLink("Export", "ExportHtml")</p> </div> <div class="col-md-4"> <h2>Export to Excel</h2> <p>@Html.ActionLink("Export", "ExportXls")</p> </div> </div>
为了获取报表,使用了GetReport()方法。此方法加载报表模板、加载XML数据文件,并为加载的报表注册此数据。
private StiReport GetReport() { string reportPath = Server.MapPath("~/Content/Reports/TwoSimpleLists.mrt"); var report = new StiReport(); report.Load(reportPath); string dataPath = Server.MapPath("~/Content/Data/Demo.xml"); var data = new DataSet("Demo"); data.ReadXml(dataPath); report.RegData(data); return report; }
现在,我们需要确定单击链接时将调用的操作。对于打印,我们将使用两种操作方法——PrintPdf和PrintHtml。
public ActionResult PrintPdf() { StiReport report = this.GetReport(); StiReportResponse.PrintAsPdf(report); return View(); } public ActionResult PrintHtml() { StiReport report = this.GetReport(); StiReportResponse.PrintAsHtml(report); return View(); }
对于打印,我们将使用三种操作方法——ExportPdf、ExportHtml和ExportXls。以这些导出格式为例。同样,导出报表(和打印)的方法可以将导出设置和其他必要参数作为输入。
public ActionResult ExportPdf() { StiReport report = this.GetReport(); StiReportResponse.ResponseAsPdf(report); return View(); } public ActionResult ExportHtml() { StiReport report = this.GetReport(); StiReportResponse.ResponseAsHtml(report); return View(); } public ActionResult ExportXls() { StiReport report = this.GetReport(); StiReportResponse.ResponseAsXls(report); return View(); }
在下面的屏幕截图中,您可以看到示例代码的结果。