Export Crystal Reports to Excel File in MVC

Here's my way of exporting a report generated using Crystal Reports viewed in PDF format to Excel File that is already ready for download by the user using MVC.

Controller Method

 public ActionResult ExportPDFToEXCEL(string searchOption, string searchKeyWord)
        {

            ReportClass rptCls = new ReportClass ();//new instance of the class of the crystal report
            MemoryStream oStream;
            rptCls .SetDatabaseLogon("username", "password");

         
            rptCls .SetParameterValue(0, searchOption);
            rptCls .SetParameterValue(1, searchKeyWord);
             
            oStream =(MemoryStream)rptCls .ExportToStream(CrystalDecisions.Shared.ExportFormatType.Excel);
            rptCls .Close();
            rptCls .Dispose();
            var arr = oStream.ToArray();
            oStream.Close();
            oStream.Dispose();
            string contentType = "application/vnd.ms-excel";
            return new FileContentResult(arr, contentType);


        }

JS

$("#btnConvertReport").click(function () {
        var searchOption = $("#searchOption option:selected").text();
        var keyword = $("#keyOption option:selected").text();

                $("#IFrameReport").attr('src', '../Report/ExportPDFToEXCEL?searchOption=' + searchOption + "&searchKeyWord=" + keyword);
         

    });


Happy coding!



No comments:

Post a Comment