Sunday, 12 November 2017

How to change the of Name of Report Will Exporting it in PDF OR Excel.


Added below given code in .Net report viewer.

Don’t forget to add web reference to following URL

  http://<Report Server Name>/ReportServer/ReportExecution2005.asmx

   private void ReportExport(string ReportPath, string Format)
        {
            try
            {
                SEReports.ReportExecutionService.ReportExecutionService rs = new SEReports.ReportExecutionService.ReportExecutionService();

                rs.Credentials = new NetworkCredential
                    (
                        ConfigurationManager.AppSettings["ServiceAccountUserId"],
                        ConfigurationManager.AppSettings["ServiceAccountPwd"],
                        ConfigurationManager.AppSettings["ServiceAccountDomain"]
                    );

                // Render arguments
                byte[] result = null;
                string historyID = null;
                string encoding;
                string mimeType;
                string extension;

                SEReports.ReportExecutionService.Warning[] warnings = null;
                string[] streamIDs = null;

                ExecutionInfo execInfo = new ExecutionInfo();
                ExecutionHeader execHeader = new ExecutionHeader();

                rs.ExecutionHeaderValue = execHeader;
                rs.Url = ConfigurationManager.AppSettings["ReportServerWSURL"];

              
                execInfo = rs.LoadReport(ReportPath, historyID);
             
                rs.SetExecutionParameters(ParamArray, "en-US");
                String SessionId = rs.ExecutionHeaderValue.ExecutionID;
               
                result = rs.Render(Format, nullout extension, out mimeType, out encoding, out warnings, out streamIDs);
                int Count = streamIDs.Length;
              

                Response.ClearContent();
               
               string FileName;

                switch (Format)
                {
                    case "PDF":
                        {
                            Response.ContentType = "application/pdf";
           FileName=”< Name of Choice >” +”.pdf”;
                            break;
                        }
                    default:
                        {
                            Response.ContentType = "application/vnd.ms-excel";
           FileName=”< Name of Choice >” +”.xls”;
                            break;
                        }
                }


                Response.AddHeader("Content-Disposition""attachment;filename=" + FileName);

                Response.BinaryWrite(result);
               
            }
            catch (Exception ex)
            {
               
            }
            finally
            {
                Response.Close();
            }
        }