导出为EXCEL.cs

csharp

浏览数:143

2019-1-8


导出为EXCEL.cs

protected void btnExport_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            Tools.NPOI.NPOIHelper.ExportByWeb(Export(), "记录消费汇总表", "记录消费汇总表.xls");
        }

        private DataTable Export()
        {
            DataTable dt = new DataTable("记录消费汇总表");
            //创建列
            var i = 1;
            DataColumn dtc = new DataColumn("序号", typeof(string));
            dt.Columns.Add(dtc);

            dtc = new DataColumn("说明", typeof(string));
            dt.Columns.Add(dtc);

            dtc = new DataColumn("卡号", typeof(string));
            dt.Columns.Add(dtc);

            dtc = new DataColumn("消费金额", typeof(double));
            dt.Columns.Add(dtc);

            dtc = new DataColumn("消费时间", typeof(string));
            dt.Columns.Add(dtc);

            DB.Read(()=> {
                this.Search();
                var EntityList = this.Manager.GetList(this.Filter);
                foreach (var m in EntityList)
                {
                    DataRow dr = dt.NewRow();
                    dr["序号"] = i++;
                    dr["说明"] = m.Note;
                    dr["卡号"] = m.GiveCoin?.Card?.CardNo;
                    dr["消费金额"] = m.Coin;
                    dr["消费时间"] = m.CreatedTime;
                    dt.Rows.Add(dr);
                }
            });
            return dt;

        }