Export Report to HTML

This example shows how to export the report to the HTML format from code. You need to create the report object of the StiReport type, then load the report template file by calling the loadFile() method. After this, you should render the report by calling the render() method of the report object:

// Create a new report instance
var report = new Stimulsoft.Report.StiReport();
// Load report from url
report.loadFile("../reports/SimpleList.mrt");
// Render report
report.render();


For export a report to the HTML format you should create two objects of the StiHtmlExportSettings and StiHtmlExportService classes. The first object defines all export settings, the second object is an HTML export service. Also you need to create the TextWriter and StiHtmlTextWriter objects - they need to output the result of the export.

To start the export, simply invoke the exportTo() method of the export service object which accepts the rendered report, text writer and export settings. For example, the result is displayed in the DIV container by its Id:

function exportReportHtml() {
// Create an HTML settings instance. You can change export settings.
var settings = new Stimulsoft.Report.Export.StiHtmlExportSettings();
// Create an HTML service instance.
var service = new Stimulsoft.Report.Export.StiHtmlExportService();

// Create a text writer objects.
var textWriter = new Stimulsoft.System.IO.TextWriter();
var htmlTextWriter = new Stimulsoft.Report.Export.StiHtmlTextWriter(textWriter);
// Export HTML using text writer.
service.exportTo(report, htmlTextWriter, settings);

// Write HTML text to DIV element.
var container = document.getElementById("htmlContainer");
container.innerHTML = textWriter.getStringBuilder().toString();
}


If you need to save the HTML export result to the file, you can use the Object.saveAs() method. This method was added in the Stimulsoft library, you can use it for saving files. The export process will be the same as in the HTML output in a DIV container:

function saveReportHtml() {
// Create an HTML settings instance. You can change export settings.
var settings = new Stimulsoft.Report.Export.StiHtmlExportSettings();
// Create an HTML service instance.
var service = new Stimulsoft.Report.Export.StiHtmlExportService();

// Create a text writer objects.
var textWriter = new Stimulsoft.System.IO.TextWriter();
var htmlTextWriter = new Stimulsoft.Report.Export.StiHtmlTextWriter(textWriter);
// Export HTML using text writer.
service.exportTo(report, htmlTextWriter, settings);

// Get HTML string from text writer object
var html = textWriter.getStringBuilder().toString();
// Get report file name
var fileName = String.isNullOrEmpty(report.reportAlias) ? report.reportName : report.reportAlias;
// Save data to file
Object.saveAs(html, fileName + ".html", "text/html;charset=utf-8");
}


In the screenshot below you can see the result of the sample code.

Export Report to HTML