Fork me on GitHub

Reporting Servive代理类

Reference webservice

http://ip:port/ReportServer/ReportExecution2005.asmx

proxy class

//=============================================================================================
//Description: base web page for report 
//==============================================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;

namespace SGSGHome.Web
{
    public class ReportExecution
    {
        #region Report Variables 
        List<ReportService.ParameterValue> _parameter=new List<ReportService.ParameterValue>();
        string _mimeType;
        string _encoding;
        string _extension;
        ReportService.Warning[] _warnings;
        string[] _streamIds;
        ReportService.ReportExecutionService rs;  
        #endregion

        #region Properties
        public string ReportName { get; set; }
        public string ReportFileName { get; set; }
        public PrintFormatEnum PrintFormat { get; set; }
        #endregion

        #region Pass Parameter to report
        /// <summary>
        /// add parameter into database
        /// </summary> 
        public void AddParameter(string paramName, string paramValue)
        {
            ReportService.ParameterValue para = new ReportService.ParameterValue();
            para.Name = paramName;
            para.Value = paramValue;
            //add to local array
            _parameter.Add(para);
        }

        #endregion

        #region Create Report
        public void GetReportBytes(HttpResponse response)
        {
            rs = new ReportService.ReportExecutionService();
            string prformat = string.Empty;
            string devInfo = string.Empty;
            string postfix = string.Empty;
            string rptUser = WebConfigurationManager.AppSettings["ReportUser"];
            string rptPwd = WebConfigurationManager.AppSettings["ReportPwd"];
            string rptDomain = WebConfigurationManager.AppSettings["ReportDomain"];
            rs.Credentials = new System.Net.NetworkCredential(rptUser, rptPwd, rptDomain);
            ReportService.ExecutionInfo exceInfo = new ReportService.ExecutionInfo();
            ReportService.ExecutionHeader exceHeader = new ReportService.ExecutionHeader();
            string historyID = null;
            string sessionId;
            rs.ExecutionHeaderValue = exceHeader;
            exceInfo = rs.LoadReport(ReportFileName, historyID);
            rs.SetExecutionParameters(_parameter.ToArray(), null);
            sessionId = rs.ExecutionHeaderValue.ExecutionID;

            //output format
            switch (PrintFormat)
            {
                case PrintFormatEnum.PDF:
                    prformat = "PDF";
                    postfix = ".pdf";
                    devInfo = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
                    break;
                case PrintFormatEnum.EXCEL:
                    prformat = "EXCEL";
                    postfix = ".xls";
                    devInfo = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
                    break;
                case PrintFormatEnum.MHTML:
                    prformat = "MHTML";
                    postfix = ".mhtml";
                    devInfo = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
                    break;
                default:
                    break;
            }

            //dispose web service
            rs.Dispose();
            byte[] results;
            rs.Timeout = 30 * 60000;
            results = rs.Render(prformat, devInfo, out _extension, out _mimeType, out _encoding, out _warnings, out _streamIds);
            response.Clear();
            response.ClearHeaders();
            response.Buffer = false;
            response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(ReportName) + postfix);//inline
            response.AppendHeader("Content-Length", results.Length.ToString());
            response.ContentType = _mimeType;
            response.Charset = "UTF-8";
            response.ContentEncoding = System.Text.Encoding.UTF8;
            response.BinaryWrite(results);
            response.Flush();
            response.Close();

        }
        #endregion

        #region Print Format Enum
        public enum PrintFormatEnum
        {
            PDF,
            EXCEL,
            MHTML
        }
        #endregion
    }
}

Invoke

            ReportExecution rs = new ReportExecution();
            rs.AddParameter("title", txtTitle.Text);
            rs.AddParameter("content", txtContent.Text);
            rs.AddParameter("createdBy", txtCreatedBy.Text);
            rs.AddParameter("group", ddlMemberGroup.SelectedValue.Replace("全部", ""));
            rs.AddParameter("startDate", dtFrom == DateTime.MinValue ? null : ((DateTime?)dtFrom).Value.ToString());
            rs.AddParameter("endDate", dtTo == DateTime.MinValue ? null : ((DateTime?)dtTo).Value.ToString());
            rs.ReportFileName = "/SPS.Report/WorkReport";
            rs.ReportName = string.Format("工作周报[{0}-{1}]", dtFrom.ToString("yyyyMMdd"), dtTo.ToString("yyyyMMdd"));
            rs.PrintFormat = ReportExecution.PrintFormatEnum.EXCEL;
            rs.GetReportBytes(this.Response);

 

posted @ 2012-11-23 16:49  Nick.Chung  阅读(324)  评论(0编辑  收藏  举报