Export to PDF, Excel and Image in asp.net

by Vahid 18. July 2009 19:48

recently in our project we had a requirement to export html to PDF in asp.net. there are so many components around which we can use to do this but the restriction for us was that we had to do it without spending money means we had to go for an open source component. doing so much I&D on this, we came to know about itextSharp which is a powerful open source component to create PDF files in .net. first it seemed good to us but spending sometimes on it we came to know it was not what we were looking for. two reason for that:

  1. it requires a big learning curve
  2. it’s not at all easy to work with

then the idea of using crystal report came to our mind. we could build the report in crystal report, load it through code without showing anything to user and through it’s API calling the export to pdf function. but it did not work since we had to run some setup components on the live server which we are not allowed to do.

meantime i just remembered we have rdlc reports in .net framework and also Microsoft report viewer. since these are .net component we don't have to install anything on the production server and Xcopy of the required assemblies will work for us. normally you can deploy the .net report files (.rdlc) using xcopy but in order to be able to use the reports you need to install Microsoft report viewer redistributable package which is freely available to download and distribute. this is fine when you have the rights to install components on the server. anyways spending sometime on this technology i found the files required to make the functionality up and running. if you don't want to install report viewer component you need to put these four dll in your bin folder:

Microsoft.ReportViewer.Common.dll
Microsoft.ReportViewer.ProcessingObjectModel.dll
Microsoft.ReportViewer.WebForms.dll
Microsoft.ReportViewer.WinForms.dll


you can download the files from here:
http://cid-c1dc0bf06aa2e3a6.skydrive.live.com/self.aspx/BlogFiles/ReportViewerFiles.zip

you need to create a report file by click on add new item in your project and selecting Report. then you need to design your report in visual studio as desired. you can use report parameters as placeholder for the values to be passed to to report.

then you need the following lines of code to export:

string[] streamids;
string mimeType;
string encoding;
string extension;
string fileFormat = "PDF";//can also be "Excel" or "Image";
Microsoft.Reporting.WebForms.LocalReport localReport = 
new Microsoft.Reporting.WebForms.LocalReport();
localReport.ReportPath = Server.MapPath("Report1.rdlc");
Microsoft.Reporting.WebForms.Warning[] warnings;
byte[] bytes = localReport.Render(
fileFormat, null, out mimeType, out encoding,
out extension,
out streamids, out warnings);
System.IO.FileStream fs = 
new System.IO.FileStream(Server.MapPath("output.pdf"),
System.IO.FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
Response.Redirect("output.pdf");

remember you can export to PDF, Excel and Image

you can use this method both for asp.net applications and windows application.

hope this comes handy for you.

Tags: , ,

.Net | Technical