首页 > 代码库 > 报表页面设计思路
报表页面设计思路
应用情景:
一、简单型
只有一个数据列表,从数据库读取表数据,在程序端构造DataTable,格式化为中文(同时处理合计结果),前台页面绑定(使用中文字段绑定),后台调用NOPI导出到EXCEL;
二、复杂型
在一的基础上增加:
1、存在多个数据列表(例如:汇总、明细);
2、可按行进行合计;
3、列名可能是动态增加的(例如支付方式:微信、支付宝、现金、刷卡);
解决方案:
(一)定义页面全局DataTable
private DataTable dtTicketSumReport = new DataTable(); protected Dictionary<string, decimal> dcTicketSum = new Dictionary<string, decimal>(); //用作合计
( 二)初始化字典对象
private void InitDictionary(){ this.dcTicketSum.Add("数量", 0); this.dcTicketSum.Add("手续费", 0); this.dcTicketSum.Add("优惠金额", 0); this.dcTicketSum.Add("合计", 0); this.dcTicketSum.Add("成本合计", 0); foreach (UserOperatorMoneyPaymentType item in Enum.GetValues(typeof(UserOperatorMoneyPaymentType))) { this.dcTicketSum.Add(item.ToString(), decimal.Zero); //动态添加字典项; }}
(三)初始化全局Datatable
private void InitGlobalTable(){ DataTable dtMoneyDetail = ds.Tables[0]; //从数据库获取数据 //格式化列名(使用中文,导出时也是使用同一个Table) this.dtTicketSumReport.Columns.Add("OperatorId", typeof(int)); //..... this.dtTicketSumReport.Columns.Add("成本", typeof(decimal)); dcMoney = new DataColumn("手续费", typeof(decimal)); //增加列 dcMoney.DefaultValue = http://www.mamicode.com/"0.00"; this.dtTicketSumReport.Columns.Add(dcMoney); Dictionary<byte, string> dcPaymentType = new Dictionary<byte, string>(); //根据数据库的实际记录,把要增加的列先添加到字典dcPaymentType中 foreach (DataRow dr in dtMoneyDetail.Rows) { byte paymentType = (byte)dr["PaymentType"]; if (!dcPaymentType.ContainsKey(paymentType)) { string columnName = null; UserOperatorMoneyPaymentType paymentTypeEnum; if (!Enum.TryParse<UserOperatorMoneyPaymentType>(paymentType.ToString(), out paymentTypeEnum)) { Response.Write("未定义支付方式" + paymentType.ToString()); Response.End(); } columnName = paymentTypeEnum.ToString(); dcPaymentType.Add(paymentType, columnName); } } foreach (DataRow dr in dtOrderTicketNumSum.Rows) { //读取列名 string amountColumnName = dcPaymentType[paymentType]; //合计: Dictionary<string, decimal> dcTicketSum = new Dictionary<string, decimal>(); this.dcTicketSum["合计"] = this.dcTicketSum["合计"] + realAmount + poundage; this.dcTicketSum[amountColumnName] = this.dcTicketSum[amountColumnName] + realAmount; DataRow drFind = this.dtTicketSumReport.Rows.Find(new object[] { operatorId, scheduleId }); //检查是否已经添加过要新增加的列 if (!this.dtTicketSumReport.Columns.Contains(amountColumnName)) { //增加列 dcMoney = new DataColumn(amountColumnName, typeof(decimal)); dcMoney.DefaultValue = "0.00"; this.dtTicketSumReport.Columns.Add(dcMoney); } if (drFind == null) { drFind = this.dtTicketSumReport.NewRow(); //...... this.dtTicketSumReport.Rows.Add(drFind); } else { drFind["合计"] = (Convert.ToDecimal(drFind["合计"]) + realAmount + poundage).ToString(); } } if (this.dtTicketSumReport!= null) { this.dtTicketSumReport.PrimaryKey = null; this.dtTicketSumReport.Columns.Remove("TicketId"); //去掉不再需要的列(前台页面不用绑定;导出时不用显示); }}
四、绑定前台页面、
private void InitControl(){ this.InitGlobalTable(); this.rpTicketSum.DataSource = this.dtTicketSumReport; this.rpTicketSum.DataBind();}
五、导出EXCEL
private void ProcessExport() { this.InitGlobalTable(); DateTime beginDate = DateTime.Parse(this.dpBeginDate.Text); DateTime endDate = DateTime.Parse(this.dpEndDate.Text); DataTable[] dtArr = null; string[] dtArrTips = null; if (true) { dtArr = .... dtArrTips = new string[] { ........ }; } else { dtArr = ...... dtArrTips = new string[] { .......}; } string fileName = (beginDate == endDate ? beginDate.ToString("yyyy-MM-dd") : string.Format("{0}至{1}", beginDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"))) + salerName + "....."; string excelSubTitle = fileName; string[] sumColumnNameArray = new string[] { ....... }; SSM_EC.Site.NpoiExcel.ExportExcel(dtArr, dtArrTips, fileName, AspxHelper.GetExcelHeader(), excelSubTitle, false, AspxHelper.GetExcelProtectPassword(), sumColumnNameArray, null, false, null); }
报表页面设计思路
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。