用反射簡化 asp.net 報表的一點總結

來源:互聯網
上載者:User

有幾個報表, 查詢條件都一樣,僅僅裡面GridView中有幾個列區別,以前圖快,就把原來Report.aspx檔案拷貝一份,改名為Report1.aspx,然後,修改裡面的column,然後再由後台庫查出資料,填充到這個report中來。

今天終於不耐煩了,這個方法確實太笨了,改一改,方案如下:

1.建立一個IReport的介面,所有的report都實現這個介面。

    public interface IReport
    {
        // 該report所有可用列集合(每一次查詢不一定全部顯示)
        Dictionary<string, string> DictColMapping { get; }       

 

        // (本次查詢)該report顯示的列KeyField和HeaderText集合

        Dictionary<string, int> DictColIndexMapping { get; }

        /// (本次查詢)該report顯示的列SortExpressField和所在位置Index集合

        List<ReportColumn> ReportColumnCollection { get; }

        // 匯出excel的檔案名稱
        string ToExcelName { get; }

 

        // 該report的唯一編號,用於安全驗證
        string ReportResourceID { get; }

 

        // 產生report,輸入條件為一個參數數組
        DataSet ProccessReport(params object[] ParamList);
    }

 

2.建立一個ReportColumn類。

    public class ReportColumn
    {
        public string ColumnDataField { set; get; }
        public string ColumnHeaderText { set; get; }
        public string ColumnSortExpression { set; get; }
        public int ColumnIndex { set; get; }

        public ReportColumn(string _ColumnDataField, string _ColumnHeaderText, string _ColumnSortExpression, int _ColumnIndex)
        {
            this.ColumnDataField = _ColumnDataField;
            this.ColumnHeaderText = _ColumnHeaderText;
            this.ColumnSortExpression = _ColumnSortExpression;
            this.ColumnIndex = _ColumnIndex;
        }
    }

 

3.建立報表的抽象基類,裡面放些處理類似報表的通用方法。 

  public abstract class clsReportBase
  {}

4.建立實際的報表類

   public class clsReport_SalesOrder : clsReportBase, IReport
    {
        private List<ReportColumn> _ReportColumnCollection; //該report所有可用列集合(每一次查詢不一定全部顯示)
        private Dictionary<string, string> _DictColMapping; //(本次查詢)該report顯示的列KeyField和HeaderText集合
        private Dictionary<string, int> _DictColIndexMapping; //(本次查詢)該report顯示的列SortExpressField和所在位置Index集合        

        public clsReport_SalesOrder()
        {
            _ReportColumnCollection = new List<ReportColumn>();
            _DictColMapping = new Dictionary<string, string>();
            _DictColIndexMapping = new Dictionary<string, int>();            
            FillReportColumnCollection();
        }

        /// <summary>
        /// 填充該report所有可用列集合
        /// </summary>
        private void FillReportColumnCollection()
        {
            AddColumnToCollection(new ReportColumn("Province", "省", "省", 0));
            AddColumnToCollection(new ReportColumn("Vertical", "行業", "行業", 1));
            AddColumnToCollection(new ReportColumn("ProductLine", "產品", "產品", 2));

            AddColumnToCollection(new ReportColumn("Subtotal", "訂單額($)", "訂單額($)", 3));
        }

        /// <summary>
        /// 該report所有可用列集合(每一次查詢不一定全部顯示)
        /// </summary>
        public List<ReportColumn> ReportColumnCollection
        {
            get
            {
                return _ReportColumnCollection;
            }
        }

        /// <summary>
        /// (本次查詢)該report顯示的列KeyField和HeaderText集合
        /// </summary>
        public Dictionary<string, string> DictColMapping
        {
            get
            {
                return _DictColMapping;
            }
        }

        /// <summary>
        /// (本次查詢)該report顯示的列SortExpressField和所在位置Index集合
        /// </summary>
        public Dictionary<string, int> DictColIndexMapping
        {
            get
            {
                return _DictColIndexMapping;
            }
        }       

        /// <summary>
        /// (本次查詢)該report需要格式化為貨幣的列集合
        /// </summary>
        public List<string> MoneyFormatColumnCollection
        {
            get
            {
                return _MoneyFormatColumnCollection;
            }
        }

        /// <summary>
        /// report名稱
        /// </summary>
        public string ToExcelName
        {
            get
            {
                return "訂單統計.xls";
            }
        }

        /// <summary>
        /// report唯一編碼
        /// </summary>
        public string ReportResourceID
        {
            get
            {
                return "12345";
            }
        }

        private void AddColumnToCollection(ReportColumn column)
        {
            _ReportColumnCollection.Add(column);
            _DictColMapping.Add(column.ColumnDataField, column.ColumnHeaderText);
            _DictColIndexMapping.Add(column.ColumnSortExpression, column.ColumnIndex);
        }

        /// <summary>
        /// 產生report,輸入條件為一個參數數組
        /// </summary>
        /// <param name="ParamList"></param>
        /// <returns></returns>
        public DataSet ProccessReport(params object[] ParamList)
        {

             ...從ParamList參數列表裡解析出查詢條件,然後從資料庫取資料。
        }

      }   

      

5.前台aspx頁面裡的GridView

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"  CellPadding="2" CssClass="autoTable" AllowSorting="false">
 <Columns>
 </Columns>
</asp:GridView>

 

6.在前台aspx.cs頁面裡,通過url傳來的報表類名,反射出報表的執行個體。

 

   protected void Page_Load(object sender, EventArgs e)
    {
        _InitReport(this.Request.QueryString["Report"]);

    }

    private void _InitReport(string QueryString)
    {
        string reportClassName = QueryString;
        string reportAssemblyName = "Test";
        string reportFullClassName = reportAssemblyName + "." + reportClassName;
        report = (IReport)Assembly.Load(reportAssemblyName).CreateInstance(reportFullClassName);   

        _DictColMapping = new Dictionary<string, string>();
        _DictColMapping = report.DictColMapping;

        _DictColIndexMapping = new Dictionary<string, int>();
        _DictColIndexMapping = report.DictColIndexMapping;

        GridView1.Columns.Clear();
        foreach (ReportColumn column in report.ReportColumnCollection)
        {
            BoundField bf = new BoundField();
            bf.DataField = column.ColumnDataField;
            bf.HeaderText = column.ColumnHeaderText;
            bf.SortExpression = column.ColumnSortExpression;
            bf.HeaderStyle.Wrap = false;
            bf.ItemStyle.Wrap = false;
            GridView1.Columns.Add(bf);
        }
    }

 

    protected void btn_Search_Click(object sender, EventArgs e)
    {
        object[] arr = new object[] { UserObject, ConditionObject ... };
        DataSet m_Data = report.ProccessReport(arr);

        HideSomeColumns(m_Data); //如果需要,可以在此屏蔽不顯示的列,設定列visiable=false.

        this.GridView_SalesOrderSummarization.DataSource = m_Data.Tables[0];
        this.GridView_SalesOrderSummarization.DataBind();
    }

 
7.這樣, 就只用一個aspx檔案文成多個報表了, 只要把類型當作參數傳過來即可.

Report.aspx?Report=clsReport_SalesOrder
Report.aspx?Report=clsReport_SalesOrder2

 

這樣就精簡了前台aspx的程式數量, 方便了以後系統的維護.

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.