Many a time we are in need to export the data we have to some file in some particular format. Like export to excel, word or pdf etc. We all know that export to excel and word is pretty simple to do it with inbuilt .net library. But for export to PDF only two things come to our mind. Either we need to use some sort of third-party tool or additional component. This additional component could be Crystal Report or could be something else also. For now let us see how we can get to solution in a quick and simple way. Let us use Crystal Report method to export our data to a PDF file.
Although Crystal report itself gives us a functionality to save the file in PDF and I think a couple of other formats also but in case our requirement doesn't permit us to use report to display result. Like in my case I had one grid on my webpage displaying some data which I had to export on a PDF file on the click of a button just below the grid. Basic approach I used can be detailed as follows:
1. Create a dataset object and add a DataTable to it. 2. Create Crystal report attach it the DataTable with desired columns. 3. Create a new web page with Crystal report viewer on it. 4. Open the Crystal report by linking the DataTable you want to export. 5. Create the PDF by using HTTP Response.
1. To Create a DataSet Object
a. In the Solution Explorer, right-click the project name, point to Add, and click Add New Item. b. In the Categories area of the Add New Item dialog box, expand Web Project Items and select Data. c. In the Templates area, select Dataset.
Go to the design view of Dataset file (default name DataSet1.xsd), right click and add DataTable. Now manually add the correct column names you will get through the DataSet you want to export to PDF. This DataTable will create a template for the record we want to display. In later stage we will see that this DataTable will be linked to Crystal Report that we will generate now.
2. To create a Crystal report.
a. In the Solution Explorer, right-click the project name, point to Add, and click Add New Item. b. In the Categories area of the Add New Item dialog box, expand Web Project Items and select Crystal report.
When you create a report, the Crystal Report Designer opens with the Crystal Report Gallery. Under Create a New Crystal Report Document, choose Using the Report Expert. Under Choose an Expert, choose Standard.Now the standard report wizard will ask you to select the data you want to report on.Under ADO.NET DataSet select the DataTable you just created following the fields you want to display. We can select various format styles like Standard, Leading Break, Trailing Break, Table and many more formats as per our requirement. We can also go the crystal report and make changes to report header, footer etc for many Look and feel factors.
3. Create a new web page Add a new web page and get a report viewer from toolbox on it. If you don’t have crystal report in tool box make sure to add following reference of .net components CrystalDecisions.CrystalReports.Engine, CrystalDecisions.Shared.
4. Link the DataTable to Crystal report. On the page load event we can handle this. But make sure to to include these two refrences on top of page.
using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; To link the DataTable use the following code snippet in which first we assign the report we generated to a ReportDocument object. We then set the DataSource of this object to the DataSet or DataTable we want to display. Refer code below
string FilePath = Server.MapPath("CrystalReport.rpt"); ReportDocument rDoc = new ReportDocument(); rDoc.Load(FilePath); DataSet dsGrid = new DataSet(); dsGrid = (DataSet)Session["data"];//Took the dataset in Session rDoc.SetDataSource(dsGrid.Tables[0]); CrystalReportViewer1.ReportSource = rDoc;
5.Create the PDF by using HTTP Response. We would take all the data on our report page in a MemoryStream object.Subsequently we will use HTTP Response to allow us to send data to client in the desired format.In our case its a PDF.The AddHeader method can be ommited if we want to open the file in the browser itself and then manually save it to the disk.Refer the code below for these steps.
MemoryStream oStream; // using System.IO oStream = (MemoryStream)rDoc.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat); Response.Clear(); Response.Buffer = true; Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachment;filename=SearchResult.pdf"); Response.BinaryWrite(oStream.ToArray()); Response.End();
Hope we have got our desired data in desired format now.
|
| Author: Mahesh Raj 07 Jun 2008 | Member Level: Gold Points : 1 |
This is very good information,Continue posting such useful articles.
|
| Author: John Fernandez 08 Jun 2008 | Member Level: Gold Points : 1 |
Very well written Article.Thanks for sharing this information.
|
| Author: vilas fulzele 30 Jul 2008 | Member Level: Silver Points : 1 |
This article is very good. I learnt a lot from this article.
thank you very much.
|