此示例项目说明了如何在不使用 表查看器的情况下从代码导出和打印 表。为此,使用特殊的StiReportResponse类足以实现 表导出和打印的方法。这些方法输入所有必需的参数,以配置导出和打印 表。
此示例项目说明了如何在不使用 表查看器的情况下从代码导出和打印 表。为此,使用特殊的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();}
在下面的屏幕截图中,您可以看到示例代码的结果。

下载示例代码
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!