限定ReportViewr匯出格式

限定ReportViewr匯出格式

客戶有個需求,就是想要限制ReportViewr匯出格式,讓使用者只能用PDF,Excel匯出,參考網路上的程式,自己改寫了一個可以共用,
並指定各種想要的格式。
這是把ReportViewr下拉的格式隱藏那些自己不需要的

 

1.新增一個RVPubCom 的Class

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// RVPubCom 控制ReportViewr報表元件的共用類別
/// </summary>
public class RVPubCom
{
 public RVPubCom()
 {
  //
  // TODO: 在此加入建構函式的程式碼
  //
    }
 
    #region "限定ReportViewr匯出格式"
    private string mRVExpFmt;
 
    /// <summary>限制報表匯出格式 
    /// <param name="reportControl"> 傳入ReportViewr物件</param>
    /// <param name="ExpFmt">匯出格式:格式與格式之間以"|"符號分隔 
    ///     <list type="PDF">PDF</list> 
    ///     <list type="Excel">Excel</list> 
    ///     <list type="Word">Word</list> 
    ///     <list type="CSV">CSV</list>
    ///     <list type="XML">XML</list>
    ///     <list type="MHTML">MHTML</list>
    ///     <list type="MHTML">TIFF</list>
    /// </param>
    ///</summary>
    public void RVExport(System.Web.UI.Control reportControl, string ExpFmt)
    {
        
        foreach (System.Web.UI.Control childControl in reportControl.Controls)
        {
            if (childControl.GetType() == typeof(System.Web.UI.WebControls.DropDownList))
            {
                mRVExpFmt = set_mRVExpFmt(ExpFmt);
                System.Web.UI.WebControls.DropDownList ddList = (System.Web.UI.WebControls.DropDownList)childControl;
                ddList.PreRender += new EventHandler(ddList_PreRender);
            }
            if (childControl.Controls.Count > 0)
            {
                RVExport(childControl, ExpFmt);
            }
        }
    }
    private string set_mRVExpFmt(string strFormat)
    {
        strFormat = "選取格式|Select a format|" + strFormat;
        char[] separator1 = new char[] { '|' };
        string[] FormatList = strFormat.Split(separator1);
        for (int i = 0; i < FormatList.Length; i++)
        {
            switch (FormatList[i].ToUpper())
            {   
                case "PDF":
                    FormatList[i] = "Acrobat (PDF) 檔案|";
                    break;
                case "EXCEL":
                    FormatList[i] = "Excel|";
                    break;
                case "WORD":
                    FormatList[i] = "Word|";
                    break;
                case "CSV":
                    FormatList[i] = "CSV (逗號分隔)|";
                    break;
                case "XML":
                    FormatList[i] = "包含報表資料的 XML 檔|";
                    break;
                case "MHTML":
                    FormatList[i] = "MHTML (網頁封存)|";
                    break;
                case "TIFF":
                    FormatList[i] = "TIFF 檔案";
                    break;                    
                default:
                    break;
            }   
        }
 
        return String.Join("|", FormatList);
       
    }
    private  void ddList_PreRender(object sender, EventArgs e)
    {
        if (sender.GetType() == typeof(System.Web.UI.WebControls.DropDownList))
        {
            System.Web.UI.WebControls.DropDownList ddList = (System.Web.UI.WebControls.DropDownList)sender;
            System.Web.UI.WebControls.ListItemCollection listItems = ddList.Items;
            if ((listItems != null) && (listItems.Count > 0) && ((listItems.FindByText("選取格式") != null) || (listItems.FindByText("Select a format") != null)))
            {
                foreach (System.Web.UI.WebControls.ListItem list in listItems)
                {
                    if (mRVExpFmt.IndexOf(list.Text) < 0)
                    {
                        list.Enabled = false;
                    }
                }
            }
        }
    }
    #endregion
}

2.呼叫方式

 

{ //限定報表匯出格式 
RVPubCom Rv = new RVPubCom();
Rv.RVExport((System.Web.UI.Control)sender,"Excel|PDF");
}