There's a lot of examples centered around the WebForms reportviewer, and which use the ReportProcessor independently. But I've got some complex reports that I've extended which require programmatic manipulation to process, and it seems that there's no examples available for how to generate those through the ReportProcessor. And as I found, calling ExportReport() simply pops up the export dialog in the UI.
So here's a nice concept to have in your toolbox - it's simple, but it eluded me for a bit so I thought I'd share it.
Assuming you have a ReportViewer in a windows form (or a RadForm), and you've already rendered the report there, you can simply grab the report object from the report viewer and pass it to the ReportProcessor like this:
private void SaveReport(string fileLocation) { |
ReportProcessor reportProcessor = new ReportProcessor(); |
//pass the reportViewer1's "Report" object to the RenderReport method |
//populate the HashTable if you need to specify DeviceInfo properties, otherwise just pass an empty one like I've done here |
RenderingResult renderingResult = reportProcessor.RenderReport("PDF", reportViewer1.Report, new Hashtable()); |
//now just create a filestream and write the DocumentBytes array to it - flush and close, and your file is ready. |
using (FileStream destinationFileStream = new FileStream(fileLocation, FileMode.Create, FileAccess.Write)) { |
destinationFileStream.Write(renderingResult.DocumentBytes, 0, renderingResult.DocumentBytes.GetLength(0)); |
destinationFileStream.Flush(); |
destinationFileStream.Close(); |
} |
} |
There's a sample project attached, but that little routine is the basis of it.