简体   繁体   English

单击按钮即可调用SSRS报告,并需要将输出获取为PDF文件

[英]Call the SSRS report in button click and need to get the output as PDF file

I had an grid view where I had placed an link button to print an report. 我有一个网格视图,在其中放置了一个链接按钮以打印报告。 In this button click event I need to call the SSRS report and need to get the output as PDF file. 在此按钮单击事件中,我需要调用SSRS报告,并需要将输出获取为PDF文件。

I had used this below code,the code is running fine, but I'm unable to see the prompt to open/save pdf file. 我使用了下面的代码,代码运行良好,但是我看不到打开/保存pdf文件的提示。

   protected void btnAuthenticateAndPrint_Click(object sender, EventArgs args)
     {
       try
        {

        //Getting Values from grid
        LinkButton lb = (LinkButton)sender;
        GridViewRow row = (GridViewRow)lb.NamingContainer;
        Label lbOrderID = row.FindControl("lbOrderID") as Label;
        int OrderId = Convert.ToInt32(lbOrderID.Text);
        da = new SqlDataAdapter("Get_PODetails", con);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        da.SelectCommand.Parameters.AddWithValue("@MPDI_ID", OrderId);
        ds = new DataSet();
        da.Fill(ds, "PO");
        if (ds.Tables["PO"].Rows.Count > 0)
        {
            lblPOId.Text=ds.Tables["PO"].Rows[0]["MPDI_ID"].ToString();
            lblVendid.Text = ds.Tables["PO"].Rows[0]["MVDI_ID"].ToString();
            lblBranch.Text = ds.Tables["PO"].Rows[0]["MBRI_ID"].ToString();
            lblDate.Text = Convert.ToDateTime(ds.Tables["PO"].Rows[0]["MPDI_Date"]).ToString("dd-MM-yyyy");
        }

        //SSRS Report Print
        rs = new RSWebService.ReportingService2005();
        rsExec = new REWebService.ReportExecutionService();
        rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
        rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
        rs.Url = "http://localhost/ReportServer/ReportService2005.asmx";
        rsExec.Url = "http://localhost/ReportServer/ReportExecution2005.asmx";
        byte[] Sendresults = null;
        byte[] bytes = null;
        string historyID = null;
        string deviceInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
        string format = "PDF";
        string encoding = null;
        string mimeType = null;
        string extension = null;
        REWebService.Warning[] warnings = null;
        string[] streamIDs = null;
        string _reportName = @"/FIMO GOF Assets Reports/PURCHASE ORDER";
        REWebService.ExecutionInfo ei = rsExec.LoadReport(_reportName, historyID);
        REWebService.ParameterValue[] parameters = new REWebService.ParameterValue[4];
        parameters[0] = new REWebService.ParameterValue();
        parameters[0].Name = "MVDI_ID";
        parameters[0].Value = lblVendid.Text;
        parameters[1] = new REWebService.ParameterValue();
        parameters[1].Name = "MBRI_ID";
        parameters[1].Value = lblBranch.Text;
        parameters[2] = new REWebService.ParameterValue();
        parameters[2].Name = "MPDI_Date";
        parameters[2].Value = lblDate.Text;
        parameters[3] = new REWebService.ParameterValue();
        parameters[3].Name = "ReportParameter1";
        parameters[3].Value = lblPOId.Text;
        rsExec.SetExecutionParameters(parameters, "en-us");
        Sendresults = rsExec.Render(format, deviceInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
        MemoryStream ms = new MemoryStream(Sendresults);


        //To create a PDF
        if (format == "PDF")
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-disposition", "inline;filename=output.pdf");
            Response.AddHeader("Content-Length", Sendresults.Length.ToString());
        }
        Response.OutputStream.Write(Sendresults, 0, Sendresults.Length);
        Response.OutputStream.Flush();
        Response.OutputStream.Close();

      }
       catch(Exception Ex)
       {
        throw Ex;
        }
   }

您可以使用URL访问,如下所述: http : //msdn.microsoft.com/zh-cn/library/ms154040(v=sql.105).aspx

http://<Server Name>/reportserver?/Sales/YearlySalesSummary&rs:Format=PDF&rs:Command=Render

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM