How do I export Crystal Report in PDF, HTML and DOC formats using C# code in ASP.NET?

20,804

Solution 1

You have to do it by yourself: create a dropdown list with formats you want and a button to make a postback for exporting.

This is an example for the .Net 1.1 / CR9. When making a postback to the following:

  1. assign to your report class instance property value MyReport.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
  2. if you want to export to .pdf do the following: MyReport.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat; You can also choose WordForWindows, RichText, Excel, HTML40 and more.
  3. Then do the following:

    CrystalDecisions.Shared.DiskFileDestinationOptions fileOptions = new DiskFileDestinationOptions();

    fileOptions.DiskFileName = "someTmpFileName";

    MyReport.DestinationOptions = fileOptions;

    MyReport.Export();

You can find more about ExportOptions class here.

And here is an example for VS 2005 / .Net 2

Solution 2

try this:

  <asp:ImageButton Width="20px" Height="20px" ID="btnPdf" runat="server"
    OnClick="btnExport_Click" ImageUrl="~/Images/PDF.png" AlternateText="Export To PDF" CssClass="AddedButton" />
   <asp:ImageButton Width="20px" Height="20px" ID="btnXls" runat="server" 
    OnClick="btnExport_Click" ImageUrl="~/Images/XLS.png" AlternateText="Export To Excel" />   
   <asp:ImageButton Width="20px" Height="20px" ID="btnDoc" runat="server" 
    OnClick="btnExport_Click" ImageUrl="~/Images/DOC.png" AlternateText="Export To Word" />   

C# code:

protected void btnExport_Click(object sender, EventArgs e)
{
    // Stop buffering the response
    Response.Buffer = false;
    // Clear the response content and headers
    Response.ClearContent();
    Response.ClearHeaders();
    try
    {
        string senderID = ((ImageButton)sender).ID;
        if (senderID == "btnPdf")
            reportDocument.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, Page.Title);
        else if (senderID == "btnXls")
            reportDocument.ExportToHttpResponse(ExportFormatType.ExcelRecord, Response, true, Page.Title);
        else if (senderID == "btnDoc")
            reportDocument.ExportToHttpResponse(ExportFormatType.WordForWindows, Response, true, Page.Title);
        // There are other format options available such as Word, Excel, CVS, and HTML in the ExportFormatType Enum given by crystal reports
    }
    catch (Exception ex)
    {
       //error management

    }
}
Share:
20,804
love Computer science
Author by

love Computer science

I'm a student, not a pro but trying to make my way to software industry!

Updated on July 24, 2022

Comments

  • love Computer science
    love Computer science almost 2 years

    I have designed a Report in my ASP.NET web site now i need to provide options to export that report in PDF, HTML, and DOC formats, how do i achieve that?

    crystal report has one button to do that but when ever i try to save that report its saved as .aspx format as i am viewing it in asp.net web page.

  • love Computer science
    love Computer science almost 13 years
    its giving me following error 'CrystalDecisions.CrystalReports.Engine.ReportDocument' does not contain a definition for 'DestinationOptions' and no extension method 'DestinationOptions' accepting a first argument of type 'CrystalDecisions.CrystalReports.Engine.ReportDocument' could be found (are you missing a using directive or an assembly reference?)
  • love Computer science
    love Computer science almost 13 years
    also if i comment that statement it throws exception: Logon failed.\nDetails: crdb_adoplus : Object reference not set to an instance of an object.\rError in File D:\\Users\\myuser\\AppData\\Local\\Temp\\CrystalReport {199BAD00-9417-4101-BEA3-FF63B72B66B3}.rpt:\nUnable to connect: incorrect log on parameters. where as i am using integrated security
  • NetRat
    NetRat almost 13 years
    Guess, it's because it's an example written in VS2003 / .Net 1.1 / CR9 long time ago. I added a link for the article with example for .Net 2.
  • NetRat
    NetRat almost 13 years
    And as far as exception is concerned, you should check weather the user under which .net process is working (most likely it is NETWORK SERVICE) has correct access right to the Temp folder.
  • love Computer science
    love Computer science almost 13 years
    i think this problem is because i am binding data to report my .cs file and not using Database Expert in Crystal report, now i added this line of code MyReport.ExportToDisk(ExportFormatType.PortableDocFormat, "D:\\report.pdf"); its working fine but i had to bind the report again. now i dont know what could be the solution to avoid this!
  • love Computer science
    love Computer science almost 13 years
    Thanks man for solution, though it wasn't 100% helpful but you definitely provided me a right approach!
  • NetRat
    NetRat almost 13 years
    You're welcome. As far as place to export a file, take a look at Path.GetTempFileName (goo.gl/96kVq) - provides a path and a name where temporary fil can be written for sure, and Path.GetTempPath() (goo.gl/iB23P) - the path where temporary files for thread user can be written. Don't forget to delete them after the export is over, or by other means.