http://192.168.1.246/ReportServer/ReportExecution2005.asmx
192.168.1.246是报表地址,要先引用这个webservice
using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
//using System.Windows.Forms;
using System.IO;
namespace TSDashboard.AutoTasks.RdlExport
{
public enum ReportFormat
{
CSV, EXCEL, PDF, XML
}
[Serializable()]
public class Report
{
#region constructors
public Report() { }
public Report(string reportFolderPath)
{
FolderPath = reportFolderPath;
}
public Report(string reportFolderPath, ReportFormat format)
{
FolderPath = reportFolderPath;
Format = format;
}
#endregion
#region properties
public string UserName { get; set; }
public string Password { get; set; }
public string Domain { get; set; }
public System.Net.NetworkCredential _NetworkCredentials;
public System.Net.NetworkCredential NetworkCredentials
{
get
{
if (_NetworkCredentials == null)
{
return new System.Net.NetworkCredential(UserName, Password, Domain);
}
else
return _NetworkCredentials;
}
set
{
_NetworkCredentials = value;
}
}
public ParameterValue[] Parameters { get; set; }
public ReportFormat Format { get; set; }
public string FormatExtension
{
get
{
string format = Enum.GetName(typeof(ReportFormat), this.Format);
if (format == "EXCEL") format = "XLS";
return format;
}
}
public string FormatName
{
get
{
switch (this.FormatExtension.ToUpper())
{
case "PDF":
return "Adobe PDF";
case "XLS":
return "Excel";
case "CSV":
return "Comma Delimitted";
case "XML":
return "Extensible Markup";
default:
return "Unknown";
}
}
}
public string ServerUrl { get; set; }
public string FolderPath { get; set; }
#endregion
#region helper methods
public ReportFormat GetReportFormat(string reportFormat)
{
return (ReportFormat)Enum.Parse(typeof(ReportFormat), reportFormat);
}
public bool PathIsValid(string fileName)
{
string rptDir = Path.GetDirectoryName(fileName);
if (!Directory.Exists(rptDir))
{
try
{
Directory.CreateDirectory(rptDir);
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
throw ex;
}
}
return true;
}
#endregion
#region export methods
/// <summary>
/// Exports a report from Reporting Services to a file.
/// </summary>
/// <param name="filePathAndName">Export file path where the report should be saved.</param>
/// <param name="timeOut">Timeout is in milliseconds, use -1 for infinite.</param>
/// <remarks></remarks>
public void Export(string filePathAndName, int timeOut)
{
ReportExecutionService rs = new ReportExecutionService();
Warning[] warnings = null;
ExecutionHeader execHeader = new ExecutionHeader();
byte[] result = null;
string historyId = null;
string devinfo = "<DeviceInfo><Toolbar>True</Toolbar></DeviceInfo>";
string extension = "";
string mimetype = "";
string encoding = "";
string[] streamid = null;
try
{
if (!string.IsNullOrEmpty(filePathAndName))
{
rs.Url = this.ServerUrl + "/ReportExecution2005.asmx";
rs.Credentials = this.NetworkCredentials;
rs.Timeout = timeOut;
rs.ExecutionHeaderValue = execHeader;
rs.LoadReport(this.FolderPath, historyId);
if ((this.Parameters != null))
{
rs.SetExecutionParameters(this.Parameters, "en_us");
}
string format = FormatExtension == "XLS" ? "EXCEL" : FormatExtension;
result = rs.Render(format, devinfo, out extension, out mimetype, out encoding, out warnings, out streamid);
// make sure the file path is valid
if (!PathIsValid(filePathAndName)) { return; }
FileStream fs = new FileStream(filePathAndName, FileMode.OpenOrCreate);
fs.Write(result, 0, result.Length);
fs.Flush();
fs.Close();
fs = null;
}
else
{
//MessageBox.Show("The file path is invalid!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (Exception ex)
{
throw ex;
//MessageBox.Show(ex.Message);
}
finally
{
rs = null;
execHeader = null;
}
}
/// <summary>
/// Exports a Reporting Services report to a file.
/// </summary>
/// <param name="filePathAndName">Export file path where the report should be saved.</param>
/// <remarks></remarks>
public void Export(string filePathAndName)
{
// 15 mins. so the server doesn‘t crash
Export(filePathAndName, 900000);
}
/// <summary>
/// Prompts the user for a valid file path then exports
/// the Reporting Services report to a file.
/// </summary>
public void Export()
{
//SaveFileDialog saveDialog = new SaveFileDialog();
//saveDialog.FileName = "*." + this.FormatExtension;
//saveDialog.Filter = this.FormatName + "|*." + this.FormatExtension;
//if (saveDialog.ShowDialog() == DialogResult.OK)
//{
// // 15 mins. so the server doesn‘t crash
// Export(saveDialog.FileName, 900000);
//}
}
#endregion
}
}
原文:http://www.cnblogs.com/demonyx/p/6801131.html