ReportServer中提供的程序调用的导出报告功能

Posted 严于律己,宽以待人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ReportServer中提供的程序调用的导出报告功能相关的知识,希望对你有一定的参考价值。

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
    }
}

 

以上是关于ReportServer中提供的程序调用的导出报告功能的主要内容,如果未能解决你的问题,请参考以下文章

获取报告参数作为 URL 字符串

ReportServer中的SMTP配置和java错误。没有为PKCS#12 KeyStore提供密码

如何查找特定报告的上次运行时间

使用 ReportService2010.asmx 以编程方式从共享点导出 s-s-rS 报告

使用 T-SQL 的 ReportServer 数据库中的 s-s-rS 站点 URL

ReportServer中的字符无效