6

是否有使用 TFS API 和 SSRS 生成 PDF、Excel、Word 或 TIFF 格式的简单方法?我可以通过使用 TFS 的 SSRS 仪表板从组合框中选择参数来手动生成报告,但我想在我的 Web 应用程序中包含一个导出按钮,以便我可以轻松绕过 Team Web Access 来生成报告。有任何想法吗?

我在这篇文章中读到我可以使用ReportExecutionServiceSSRS 中的类,所以我想知道这是否可以从 TFS 服务器中使用。

我已使用此 Web 服务参考导入了命名空间: http://<TFS server>/reportserver/ReportExecution2005.asmx

4

1 回答 1

4

我找到了一种使用ReportExecutionServiceWeb 服务生成报告的方法。如果已将 Web 服务引用添加到项目中,这将生成具有给定参数的文件。此处开始和结束日期作为参数提供,并且使用Render() 方法生成报告。

private void GenerateReport()
{
    ReportExecutionService rs = new ReportExecutionService();
    rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
    rs.Url = "http://<TFS server name>/reportserver/ReportExecution2005.asmx";

    // Render arguments
    byte[] result = null;
    string reportPath = @"<SSRS report path>";
    string format = "PDF";
    string historyID = null;
    string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";

    // Prepare report parameter.
    ParameterValue[] parameters = new ParameterValue[2];
    parameters[0] = new ParameterValue();
    parameters[0].Name = "StartDateParam";
    parameters[0].Value = "2012-06-01 00:00:00";
    parameters[1] = new ParameterValue();
    parameters[1].Name = "EndDateParam";
    parameters[1].Value = "2012-09-01 00:00:00";
    parameters[2] = new ParameterValue();
    parameters[2].Name = "AreaParam";
    parameters[2].Value = "[Work Item].[Area Hierarchy].[All]";
    parameters[3] = new ParameterValue();
    parameters[3].Name = "WorkItemTypeParam";
    parameters[3].Value = "[Work Item].[System_WorkItemType].&[Task]";
    parameters[4] = new ParameterValue();
    parameters[4].Name = "StateParam";
    parameters[4].Value = "[Work Item].[System_State].&[Active]";
    parameters[5] = new ParameterValue();
    parameters[5].Name = "TrendLineParam";
    parameters[5].Value = "both";

    DataSourceCredentials[] credentials = null;
    string showHideToggle = null;
    string encoding;
    string mimeType;
    string extension;
    Warning[] warnings = null;
    ParameterValue[] reportHistoryParameters = null;
    string[] streamIDs = null;

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

    rs.ExecutionHeaderValue = execHeader;

    execInfo = rs.LoadReport(reportPath, historyID);

    var parameters_ = rs.GetExecutionInfo().Parameters;

    rs.SetExecutionParameters(parameters, "en-us"); 
    String SessionId = rs.ExecutionHeaderValue.ExecutionID;

    Console.WriteLine("SessionID: {0}", rs.ExecutionHeaderValue.ExecutionID);

    try
    {
        result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);

        execInfo = rs.GetExecutionInfo();

        Console.WriteLine("Execution date and time: {0}", execInfo.ExecutionDateTime);

    }
    catch (SoapException e)
    {
        Console.WriteLine(e.Detail.OuterXml);
    }
    // Write the contents of the report to an MHTML file.
    try
    {
        FileStream stream = File.Create("report.pdf", result.Length);
        Console.WriteLine("File created.");
        stream.Write(result, 0, result.Length);
        Console.WriteLine("Result written to the file.");
        stream.Close();
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }

}

顺便说一句,为参数设置的值是 MDX 查询,我还没有找到如何设置特定的 AreaPath,所以现在我只能列出“全部”。

于 2012-09-11T15:27:51.903 回答