This sample project shows how to export and print the dashboard from code without using the viewer.
For this action, it is enough to use the special
StiMvcReportResponse
class. This class implements the static methods for dashboard exporting and printing that take the input of all the necessary parameters for configuration. For example, add a link for print 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 Excel</h2>
<p>@Html.ActionLink("Export", "ExportExcel")</p>
</div>
<div class="col-md-4">
<h2>Export to Image</h2>
<p>@Html.ActionLink("Export", "ExportImage")</p>
</div>
</div>
To get the dashboard, the
GetDashboard()
method was used. This method creates the new dashboard object and loads the dashboard template from the file:
private StiReport GetDashboard()
{
var reportPath = Server.MapPath("~/Dashboards/DashboardChristmas.mrt");
var report = StiReport.CreateNewDashboard();
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 the
PrintPdf
and
PrintHTML
method:
public ActionResult PrintPdf()
{
var report = this.GetDashboard();
return StiMvcReportResponse.PrintAsPdf(report);
}
public ActionResult PrintHtml()
{
var report = this.GetDashboard();
return StiMvcReportResponse.PrintAsHtml(report);
}
For exporting, we will use three action methods -
ExportPdf
,
ExportExcel
, and
ExportImage
. These export formats are taken for example. Also, the methods of exporting a dashboard (and printing) can take, as input, export settings and other necessary parameters:
public ActionResult ExportPdf()
{
var report = this.GetDashboard();
return StiMvcReportResponse.ResponseAsPdf(report);
}
public ActionResult ExportExcel()
{
var report = this.GetDashboard();
return StiMvcReportResponse.ResponseAsExcel2007(report);
}
public ActionResult ExportImage()
{
var report = this.GetDashboard();
return StiMvcReportResponse.ResponseAsPng(report);
}
Auf dem Screenshot unten Sie können das Ergebnis des Beispiel-Codes ansehen: