In Web applications, we cannot export reports like Windows applications because the program is executed on the server side. When the export is executed, the results are also on the server side. So how can we achieve complete client-side export? In fact, this is not difficult. The method is: export the report to a pre-created temporary report file on a certain website, and then use the response.redirect() command to point the browser URL to the report location, so that the user's browser It will try to download the just exported file, and the file will be downloaded to the client, thereby achieving the effect we need. Part of the code is as follows:
public string ExportReport()
{
ExportOptions creo = new ExportOptions();
DiskFileDestinationOptions crdo = new DiskFileDestinationOptions();
string FileName = Request.PhysicalApplicationPath + "ExportFileExap.xls";
//Set export options
creo = Myrpt.ExportOptions;
creo.ExportFormatType = ExportFormatType.Excel;
creo.ExportDestinationType = ExportDestinationType.DiskFile;
//Set disk file options
crdo.DiskFileName = FileName;
creo.DestinationOptions = crdo;
//Export report
MyRpt.Export();
return FileName;
}
private void buttonExport_Click(object sender, System.EventArgs e)
{
string FileName = ExportReport();
Response.Redirect(Replace(FileName,Request.PhysicalApplicationPath + "ExportFile",""));
}
It should be noted that when exporting to the web, you need to have permission to create files in the export directory. If the permissions are insufficient, an error "Access to the report file is denied..." will appear. Just let the ASPNET user (the system user automatically generated when installing the .NET Framework) have "write" permissions in the export directory file.