Telerik报表服务器?下载30天免费试用

如何通过.NET客户端从Telerik报表服务器导出报表

环境

产品 Progress®Telerik®报表服务器

描述

Telerik报表服务器提供了一个REST API可用于从外部应用程序消费资源。如何使用JavaScript客户端与REST API通信演示如下:

本文的目的是演示使用. net客户端的相同功能。

解决方案

同样的JavaScript客户端可以被写成. net客户端来执行请求并处理它们的响应。下面的示例说明如何向报表服务器API发送请求,并最终将报表导出到报表文档。

c#
使用Newtonsoft.Json.Linq;使用系统;使用System.Collections.Generic;使用System.Net.Http;使用System.Net.Http.Headers;使用System.Threading.Tasks;namespace GetAndExportReportFromRSWebApi{类程序{静态只读string reportServerUrl = "http://localhost:83/";静态只读string reportServerApiUrl = reportServerUrl + "api/reportserver/";静态只读string username = "yourUsername";静态只读字符串password = "yourPassword"; static void Main(string[] args) { var reportName = "Dashboard"; var parameters = new { ReportYear = 2004 }; var format = "PDF"; CreateAndDownloadReportDocument(reportName, parameters, format).Wait(); } static async Task CreateAndDownloadReportDocument(string reportName, object parameters, string format) { using (var client = new HttpClient()) { client.BaseAddress = new Uri(reportServerApiUrl); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Authenticate via OAuth 2.0 Password Grant. var authToken = GetAuthenticationToken(client, username, password); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken); // List all reports. var reports = await GetAllReports(client, reportServerApiUrl); // Render the desired report. var report = reports.Find(r => r["Name"].Equals(reportName))["Id"]; var documentUrl = await CreateDocument(client, reportServerApiUrl, format, report, parameters); // Download the created document. var downloadPath = DownloadDocument(documentUrl, reportName, false); // Open the document. System.Diagnostics.Process.Start(downloadPath); } } static string GetAuthenticationToken(HttpClient client, string usernameInput, string passwordInput) { var data = new FormUrlEncodedContent(new[] { new KeyValuePair("grant_type", "password"), new KeyValuePair("username", usernameInput), new KeyValuePair("password", passwordInput) }); // POST Token var response = client.PostAsync(reportServerUrl + "Token", data).Result; response.EnsureSuccessStatusCode(); var result = response.Content.ReadAsStringAsync().Result; var token = JObject.Parse(result).SelectToken("access_token").ToString(); return token; } static async Task>> GetAllReports(HttpClient client, string apiUrl) { // GET api/reportserver/reports var response = await client.GetAsync(apiUrl + "reports"); if (response.IsSuccessStatusCode) { return await response.Content.ReadAsAsync>>(); } return null; } static async Task CreateDocument(HttpClient client, string apiUrl, string format, string reportId, object parameterValues) { var data = new { ReportId = reportId, Format = format, ParameterValues = parameterValues }; // POST api/reportserver/documents var response = await client.PostAsJsonAsync(apiUrl + "documents", data); response.EnsureSuccessStatusCode(); var documentUrl = response.Headers.Location; return documentUrl.ToString(); } static string DownloadDocument(string url, string reportName, bool asAttachment) { var queryString = asAttachment ? "?content-disposition=attachment" : ""; var fileName = reportName + ".pdf"; var folderName = System.IO.Path.GetTempPath(); var filePath = System.IO.Path.Combine(folderName, fileName); using (var webClient = new System.Net.WebClient()) { webClient.DownloadFile(url + queryString, filePath); } return filePath; } } }
在本文中
Baidu
map