Popular Posts

There was an error in this gadget

Oct 22, 2013

Exporting to Excel in Crystal Reports .NET



Export crystal report to excel in asp.net 

ReportDocument crystalReport;

protected void Page_Load(object sender, EventArgs e)
    {
       
        crystalReport = new ReportDocument();
        crystalReport.Load(Server.MapPath("GenReportMaster.rpt"));
       
        //Set DataBase Login Info
        crystalReport.SetDatabaseLogon("sa", "Nopass123", @"BD-DAC-014", "lamolinsDB");
        string year = (Request.QueryString["year"] != "") ? Request.QueryString["year"] : "";
        string month = (Request.QueryString["month"] != "") ? Request.QueryString["month"] : "";
        string zoneString = Request.QueryString["zone"];
        string zone = (zoneString.Equals("All")) ? "" : zoneString;
        string branchString = Request.QueryString["branch"];
        string branch = (branchString.Equals("All")) ? "" : branchString;
        string brand = (Request.QueryString["brand"]!= "") ? Request.QueryString["brand"] : "";




        crystalReport.SetParameterValue("@month", month);
        crystalReport.SetParameterValue("@year", year);
        crystalReport.SetParameterValue("@zone", zone);
        crystalReport.SetParameterValue("@branch", branch);
        crystalReport.SetParameterValue("@brand", brand);
        CwrVatCrystalReportViewer.Visible = true;
        CwrVatCrystalReportViewer.DisplayGroupTree = false;
        CwrVatCrystalReportViewer.HasPageNavigationButtons = true;
        CwrVatCrystalReportViewer.HasCrystalLogo = false;
        CwrVatCrystalReportViewer.HasDrillUpButton = false;
        CwrVatCrystalReportViewer.HasSearchButton = false;
        CwrVatCrystalReportViewer.HasViewList = false;
        CwrVatCrystalReportViewer.HasToggleGroupTreeButton = false;
        CwrVatCrystalReportViewer.HasZoomFactorList = false;
        CwrVatCrystalReportViewer.ToolbarStyle.Width = new Unit("750px");


        CwrVatCrystalReportViewer.ReportSource = crystalReport;
       
    }



Convert to excel:

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            ExportOptions CrExportOptions;

            DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
            ExcelFormatOptions CrFormatTypeOptions = new ExcelFormatOptions();
            CrDiskFileDestinationOptions.DiskFileName = "c:\\report.xls";
            CrExportOptions = crystalReport.ExportOptions;
            CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
            CrExportOptions.ExportFormatType = ExportFormatType.Excel;
            CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
            CrExportOptions.FormatOptions = CrFormatTypeOptions;
            crystalReport.Export();
            messageLabel.Text = "Download excel successful...";
        }
        catch (Exception ex)
        {
            messageLabel.Text = "Download excel failed..."+ex.ToString();
        }

    }



1 comment:

  1. User this code to download in pdf:

    ExportOptions exportOpts = new ExportOptions();
    DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
    exportOpts = crystalReport.ExportOptions;
    exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
    exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat;
    diskOpts.DiskFileName = "C:\\Report.pdf";
    exportOpts.DestinationOptions = diskOpts;
    crystalReport.ExportToHttpResponse(exportOpts, this.Page.Response, true, "rpt01" + DateTime.Now.ToString("yyyyMMdd"));
    messageLabel.Text = "Download excel successful...";

    ReplyDelete