Мы уже рассмотрели пример отображения диалогового окна печати отчета в формате HTML и PDF. Теперь, я расскажу, как сохранять отчеты в нужном формате на примере Excel.
Воспользуемся веб приложением MVC.
Сохранение отчета будет осуществляться кнопкой. Добавим ее на веб страницу Home. Для этого открываем файл Index.cshtml в папке Views.
And place the following code is in the desired location:
1 2 3 4 5 6 7 |
@using (Html.BeginForm("Save", "Home")) { <input id="print" type="submit" value="Save report in Excel" /> } |
There Save - name of the handler in the controller. Home - name of the controller.
Now add a Save method to the Home page controller. To do this, open the file HomeController.cs from Controllers folder:
The method will look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public void Save() { WebReport webReport = new WebReport(); System.Data.DataSet dataSet = new System.Data.DataSet(); dataSet.ReadXml("C://Program Files (x86)//FastReports//FastReport.Net//Demos//Reports//nwind.xml"); webReport.Report.RegisterData(dataSet, "NorthWind"); webReport.Report.Load("C://Program Files (x86)//FastReports//FastReport.Net//Demos//Reports//Simple List.frx"); webReport.ExportExcel2007(); } |
Consider the following order:
1. Create an instance of an object WebReport;
2. Create an instance of a DataSet for data processing;
3. Load the database xml;
4. Registering a data source in a report object;
5. Load the report template into WebReport object;
6. Save the report in Excel format.
Now you need to add a handler in the Web.config file located in the root of the project:
1 2 3 4 5 |
<handlers> <add name="FastReportHandler" path="FastReport.Export.axd" verb="*" type="FastReport.Web.Handlers.WebExport"/> </handlers> |
We run the Web application, and see our button:
Click on it. It will appear the save dialog of a report file in xlsx format.
After downloading the report, it will be open:
Using the example above you can implement the saving a report in other formats are available for export. Using the code above, you can allow users to download the report in the desired format without displaying the report itself.