2012-09-06 97 views
6

使用TFS API和SSRS以PDF,Excel,Word或TIFF格式生成報表是否有一種簡單的方法?我可以通過使用用於TFS的SSRS儀表板從組合框中選擇參數來手動生成報告,但是我想在我的Web應用程序中包含一個導出按鈕,這樣我就可以輕鬆繞過Team Web Access以生成報告。有任何想法嗎?使用TFS API和SSRS以編程方式生成報告

我已閱讀this post,我可以使用SSRS中的ReportExecutionService類,所以我想知道是否可以從TFS服務器使用它。

我已導入使用此Web服務引用命名空間: http://<TFS server>/reportserver/ReportExecution2005.asmx

回答

4

我已經找到了一種方法來生成使用ReportExecutionService Web服務報告。如果已經將Web服務引用添加到項目中,則會生成具有給定參數的文件。在這裏,開始和結束日期作爲參數提供,報告使用Render() method生成。

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,所以現在只有我可以列出「全部」。