This example shows how to export a report from code. First, load scripts:
@using Stimulsoft.Base
@using Stimulsoft.Report
@using Stimulsoft.Report.Web
@using Stimulsoft.Report.Blazor
Next, create buttons:
<div align="center">
<button @onclick="ExportReportAsHTML">Export as HTML</button> <button @onclick="ExportReportAsPDF">Export as PDF</button>
<br /><br />
<input id="responseAsFile" type="checkbox" @bind="responseAsFile"><label for="responseAsFile">Response as File</label>
<br /><br />
<p>@message</p>
</div>
After that, initialize JSRuntime service:
@code
{
private bool responseAsFile;
private MarkupString message;
protected override void OnInitialized()
{
base.OnInitialized();
//Initialize JSRuntime service, only needed for response as file
StiBlazorHelper.Initialize(JSRuntime);
}
...
Then, use
ExportDocument
method to export report:
...
private void ExportReportAsHTML()
{
//Create empty report object
var report = new StiReport();
//Load report template
report.Load("Reports/TwoSimpleLists.mrt");
//Render, w/out showing progress
report.Render(false);
if (responseAsFile)
{
//Response as HMTL file
StiReportResponse.ResponseAsHtml(report);
message = new MarkupString("Export to HTML is completed.");
}
else
{
//Export to HTML file
var _exportFilePath = $"Reports/TwoSimpleLists_{DateTime.Now.ToString("yyyy-dd-MM_HH-mm-ss")}.html";
report.ExportDocument(Stimulsoft.Report.StiExportFormat.Html, _exportFilePath);
message = new MarkupString("The exported report is saved to a file:
" + _exportFilePath);
}
}
private void ExportReportAsPDF()
{
//Create empty report object
var report = new StiReport();
//Load report template
report.Load("Reports/TwoSimpleLists.mrt");
//Render, w/out showing progress
report.Render(false);
if (responseAsFile)
{
//Response as PDF file
StiReportResponse.ResponseAsPdf(report);
message = new MarkupString("Export to PDF is completed.");
}
else
{
//Export to PDF file
var _exportFilePath = $"Reports/TwoSimpleLists_{DateTime.Now.ToString("yyyy-dd-MM_HH-mm-ss")}.pdf";
report.ExportDocument(Stimulsoft.Report.StiExportFormat.Pdf, _exportFilePath);
message = new MarkupString("The exported report is saved to a file:
" + _exportFilePath);
}
}
}
In the screenshot below you can see the result of the sample code: