This sample project shows how to export and print the report from code without using the report viewer. For this action, it is enough to use the special
StiMvcReportResponse
class that implements the methods for report exporting and printing. These methods take the input of all the necessary parameters to configure exporting and printing the report.
The special
StiMvcReportResponse
class contains methods to export report in any format and print report to PDF and HTML. For example, implementing it for one report in order to compare them. Add two links for each print mode and three links for various export formats:
<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>
To get the report, the
GetReport()
method was used. This method loads the report template, loads the XML data file and registers this data for loaded report:
private StiReport GetReport()
{
var reportPath = Server.MapPath("~/Content/Reports/TwoSimpleLists.mrt");
var report = new StiReport();
report.Load(reportPath);
return report;
}
Now we need to determine the actions that will be invoked when clicking on links. For printing we will use two action methods -
PrintPdf
and
PrintHtml
:
public ActionResult PrintPdf()
{
var report = this.GetReport();
return StiMvcReportResponse.PrintAsPdf(report);
}
public ActionResult PrintHtml()
{
var report = this.GetReport();
return StiMvcReportResponse.PrintAsHtml(report);
}
For printing, we will use three action methods -
ExportPdf
,
ExportHtml
, and
ExportXls
. These export formats are taken for example. Also, the methods of exporting a report (and printing) can take, as input, export settings and other necessary parameters:
public ActionResult ExportPdf()
{
var report = this.GetReport();
return StiMvcReportResponse.ResponseAsPdf(report);
}
public ActionResult ExportHtml()
{
var report = this.GetReport();
return StiMvcReportResponse.ResponseAsHtml(report);
}
public ActionResult ExportXls()
{
var report = this.GetReport();
return StiMvcReportResponse.ResponseAsXls(report);
}
На скриншоте ниже Вы можете увидеть результат выполнения данного кода: