private void ExportExcelUserCoupons() { DbQueryResult couponsList = CouponHelper.GetCouponsListToExport(new CouponItemInfoQuery { CounponName = this.couponName, OrderId = this.couponOrder, CouponId = this.CouponId, UserName = this.userName, AddUserName = this.addUserName, BeginTime = !this.dateStart.HasValue ? "" : this.dateStart.Value.ToString("yyyy-MM-dd HH:mm:ss"), EndTime = !this.dateEnd.HasValue ? "" : (this.dateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59"), CouponStatus = this.couponstatus }); DataTable dtResult = (DataTable)couponsList.Data; if (dtResult == null || dtResult.Rows.Count == 0) { this.ShowMsg("没有数据需要导出!", false); return; } DataTable dtcoupons = new DataTable(); dtcoupons.Columns.Add("用户名", typeof(string)); dtcoupons.Columns.Add("劵名称", typeof(string)); dtcoupons.Columns.Add("发放时间", typeof(string)); dtcoupons.Columns.Add("使用时间", typeof(string)); dtcoupons.Columns.Add("使用金额", typeof(string)); dtcoupons.Columns.Add("订单号", typeof(string)); for (var i = 0; i < dtResult.Rows.Count; i++) { DataRow dr = dtcoupons.NewRow(); dr["用户名"] = dtResult.Rows[i]["UserName"].ToString(); dr["劵名称"] = dtResult.Rows[i]["Name"].ToString(); dr["发放时间"] = dtResult.Rows[i]["GenerateTime"].ToString(); dr["使用时间"] = dtResult.Rows[i]["UsedTime"].ToString(); dr["使用金额"] = dtResult.Rows[i]["DiscountValue"].ToString(); dr["订单号"] = dtResult.Rows[i]["Orderid"].ToString(); dtcoupons.Rows.Add(dr); } MemoryStream ms = NPOIExcelHelper.ExportToExcel(dtcoupons, "优惠券使用明细"); this.Page.Response.Clear(); this.Page.Response.Buffer = false; this.Page.Response.Charset = "GB2312"; if (!(this.calendarStart.SelectedDate.HasValue && this.calendarEnd.SelectedDate.HasValue)) { this.Page.Response.AppendHeader("Content-Disposition", "attachment;filename=优惠券使用明细.xlsx"); } else { this.Page.Response.AppendHeader("Content-Disposition", "attachment;filename=优惠券使用明细_" + this.dateStart.Value.ToString("yyyyMMdd") + "-" + this.dateEnd.Value.ToString("yyyyMMdd") + ".xlsx"); } this.Page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); this.Page.Response.ContentType = "application/octet-stream"; this.Page.EnableViewState = false; this.Page.Response.BinaryWrite(ms.ToArray()); ms.Dispose(); ms.Close(); this.Page.Response.End(); }