Example of export in MVC
When using FastReport .NET together with the ASP.NET MVC framework there is an easy method for creating a file in any supported format from a button press on the HTML form.
Add this code in the View:
@using (Html.BeginForm("GetFile", "Home"))
{
<input id="pdf" type="submit" value="Export to PDF" />
}
GetFile
- name of the controller handler;Home
- name of the controller (eg: HomeController.cs).
Add the name space in the controller:
using FastReport.Export.Pdf;
Add method GetFile
in the controller:
public FileResult GetFile()
{
WebReport webReport = new WebReport();
// bind data
System.Data.DataSet dataSet = new System.Data.DataSet();
dataSet.ReadXml(report_path + "nwind.xml");
webReport.Report.RegisterData(dataSet, "NorthWind");
// load report
webReport.ReportFile = this.Server.MapPath("~/App_Data/report.frx");
// prepare report
webReport.Report.Prepare();
// save file in stream
Stream stream = new MemoryStream();
webReport.Report.Export(new PDFExport(), stream);
stream.Position = 0;
// return stream in browser
return File(stream, "application/zip", "report.pdf");
}
Example for Excel 2007:
using FastReport.Export.OoXML;
...
webReport.Report.Export(new Excel2007Export(), stream);
...
return File(stream, "application/xlsx", "report.xlsx");