I am trying to How to export the crystal reports to Excel pdf etc. I am not getting the solution it shows the invalid path but i am giving correct path.pls suggest me,How to solve the problem.
Thanks in Advance.

Recommended Answers

All 3 Replies

You can try this link for exporting data to excel in vb.net and procedure will also be application in Asp.net too.

http://www.encodedna.com/2012/12/export-data-to-excel.htm

And to export your crystal report data to PDF try the below function.

Sub printReport()
    ' FIRST ADD REFERENCES.
    ' CrystalDecisions.CrystalReports.Engine
    ' CrystalDecisions.ReportSource

    Dim Report As CrystalDecisions.CrystalReports.Engine.ReportDocument = New CrystalDecisions.CrystalReports.Engine.ReportDocument
    Dim CrystalReportViewer As CrystalDecisions.Web.CrystalReportViewer = New CrystalDecisions.Web.CrystalReportViewer

    CrystalReportViewer.BorderStyle = BorderStyle.Solid                 ' OPTIONAL
    CrystalReportViewer.DisplayGroupTree = False                        ' OPTIONAL
    CrystalReportViewer.Zoom(150)                                       ' OPTIONAL
    CrystalReportViewer.HasCrystalLogo = False

    CrystalReportViewer.BestFitPage = False             ' SET THIS FALSE, SO YOU CAN SET THE WIDTH AND HEIGHT.
    CrystalReportViewer.Width = "1200" : CrystalReportViewer.Height = "800"

    ' NOW LOAD THE REPORT.
    Report.Load(System.AppDomain.CurrentDomain.BaseDirectory() & "\reports\" & YourCrystalFileName & "")

    ' NOW EXPORT THE DATA TO A "PDF" FILE.

    Dim sPath_N_File As String = ""
    sPath_N_File = "FOLDER\TESTFILE.pdf"

    Report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
    Report.ExportToDisk(ExportFormatType.PortableDocFormat, Server.MapPath(sPath_N_File))
End Sub

Crystal report itsel can export to pdf and excel, but you have to install Office interop. Here I introduce you a data export component that can export data to PDF, excel, word, html, xml etc without using any third party library. http://www.e-iceblue.com/Introduce/data-export-for-net-intro.html.

you can see below code:

protected void exportReport(CrystalDecisions.CrystalReports.Engine.ReportClass selectedReport, CrystalDecisions.Shared.ExportFormatType eft)
{
    selectedReport.ExportOptions.ExportFormatType = eft;

    string contentType ="";
    // Make sure asp.net has create and delete permissions in the directory
    string tempDir = System.Configuration.ConfigurationSettings.AppSettings["TempDir"];
    string tempFileName = Session.SessionID.ToString() + ".";
    switch (eft)
    {
    case CrystalDecisions.Shared.ExportFormatType.PortableDocFormat : 
        tempFileName += "pdf";
        contentType = "application/pdf";
        break;
    case CrystalDecisions.Shared.ExportFormatType.WordForWindows : 
        tempFileName+= "doc";
        contentType = "application/msword";
        break;
    case CrystalDecisions.Shared.ExportFormatType.Excel : 
        tempFileName+= "xls";
        contentType = "application/vnd.ms-excel";
        break;
    case CrystalDecisions.Shared.ExportFormatType.HTML32 : 
    case CrystalDecisions.Shared.ExportFormatType.HTML40 : 
        tempFileName+= "htm";
        contentType = "text/html";
        CrystalDecisions.Shared.HTMLFormatOptions hop = new CrystalDecisions.Shared.HTMLFormatOptions();
        hop.HTMLBaseFolderName = tempDir;
        hop.HTMLFileName = tempFileName;
        selectedReport.ExportOptions.FormatOptions = hop;
        break;
    }

    CrystalDecisions.Shared.DiskFileDestinationOptions dfo = new CrystalDecisions.Shared.DiskFileDestinationOptions();
    dfo.DiskFileName = tempDir + tempFileName;
    selectedReport.ExportOptions.DestinationOptions = dfo;
    selectedReport.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;

    selectedReport.Export();
    selectedReport.Close();

    string tempFileNameUsed;
    if (eft == CrystalDecisions.Shared.ExportFormatType.HTML32 || eft == CrystalDecisions.Shared.ExportFormatType.HTML40)
    {
        string[] fp = selectedReport.FilePath.Split("\\".ToCharArray());
        string leafDir = fp[fp.Length-1];
        // strip .rpt extension
        leafDir = leafDir.Substring(0, leafDir.Length – 4);
        tempFileNameUsed = string.Format("{0}{1}\\{2}", tempDir, leafDir, tempFileName);
    }
    else
        tempFileNameUsed = tempDir + tempFileName;

    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = contentType;

    Response.WriteFile(tempFileNameUsed);
    Response.Flush();
    Response.Close();

    System.IO.File.Delete(tempFileNameUsed);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.