使用Reporting Services将SSRS报告另存为pdf

时间:2013-12-17 08:14:01

标签: pdf reporting-services ssrs-2008

我一直在尝试将SSRS报告转换为PDF并使用Reporting Web Services保存到我的本地驱动器。虽然我能够生成相应的pdf文件但是文件的内容丢失了。我已经检查过我想要转换的报告不是空的。生成的pdf文件中仅存在标题部分。以下是我正在使用的代码:

protected void GeneratePDFFromReport(object sender, EventArgs e)
    {
        RS2005.ReportingService2005 rs;
        RE2005.ReportExecutionService rsExec;

        // Create a new proxy to the web service
        rs = new RS2005.ReportingService2005();
        rsExec = new RE2005.ReportExecutionService();

        // Authenticate to the Web service using Windows credentials
        rs.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
        rsExec.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
        //rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;

        rs.Url = "http://servername/reportserver/reportservice2005.asmx";
        rsExec.Url = "http://servername/reportserver/reportexecution2005.asmx";

        string historyID = null;
        string deviceInfo = null;
        string format = "PDF";
        Byte[] results;
        string encoding = String.Empty;
        string mimeType = "application/pdf";
        string extension = String.Empty;
        RE2005.Warning[] warnings = null;
        string[] streamIDs = null;

        // Path of the Report - XLS, PDF etc.
        string fileName = @"C:\Report\MemberReport.pdf";
        // Name of the report - Please note this is not the RDL file.
        string _reportName = @"/ReportFolder/ReportName";
        string _historyID = null;
        bool _forRendering = false;
        RS2005.ParameterValue[] _values = null;
        RS2005.DataSourceCredentials[] _credentials = null;
        RS2005.ReportParameter[] _parameters = null;

        try
        {
            _parameters = rs.GetReportParameters(_reportName, _historyID, _forRendering, _values, _credentials);
            RE2005.ExecutionInfo ei = rsExec.LoadReport(_reportName, historyID);

            results = rsExec.Render(format, deviceInfo,
                      out extension, out encoding,
                      out mimeType, out warnings, out streamIDs);

            try
            {
                FileStream stream = File.Create(fileName, results.Length);
                stream.Write(results, 0, results.Length);
                stream.Close();
            }
            catch { }

            results = null;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

任何帮助都将受到高度赞赏。感谢。

1 个答案:

答案 0 :(得分:0)

假设SSRS使用浏览器正常工作,请修改您发布的代码,如下所示:

1)设备信息字符串,请按如下方式设置:

string deviceInfo =  @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>"; //Initial value was null

2)在使用Web调用LoadReport之前创建Header实例:

ExecutionHeader execHeader = new ExecutionHeader();
RE2005.ExecutionHeaderValue = execHeader;