码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-05-03 13:16:18      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:get   and   word   final   board   except   else   default   finally   

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中提供的程序调用的导出报告功能

标签:get   and   word   final   board   except   else   default   finally   

原文地址:http://www.cnblogs.com/demonyx/p/6801131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!