NPOI 导出Excel 多文件打包下载
C# codeprotected void Button1_Click(object sender, EventArgs e) { DataTable dt = new DataTable("客户档案"); ArrayList arrColumns = GetXlsColumns(); DataTable dtMemberUser = DAL.Member_User.GetList(); for (int i = 0; i < arrColumns.Count; i++) { dt.Columns.Add(arrColumns[i].ToString()); } foreach (DataRow dr in dtMemberUser.Rows) { DataRow row = dt.NewRow(); Model.Sell_Order modelSellOrder = DAL.Sell_Order.GetModel(Convert.ToInt32(dr["UserID"])); if (modelSellOrder != null) { row["客户编码"] = ""; row["客户名称"] = dr["UserName"].ToString() != "" ? dr["UserName"] : dr["NickName"]; row["联系电话"] = dr["Tel"]; row["手机"] = dr["Mobile"]; row["旺旺ID"] = ""; row["收货人"] = modelSellOrder.Recipients; row["客户分类"] = ""; row["省市"] = string.Format("{0} {1}", dr["Province"], dr["City"]); row["MSN"] = ""; row["skype"] = ""; row["支付宝账号"] = ""; row["淘宝地址"] = ""; row["易趣地址"] = ""; row["拍拍地址"] = ""; row["传真"] = ""; row["电子邮件"] = dr["Email"]; row["邮编"] = modelSellOrder.Zip; row["收货人地址"] = string.Format("{0} {1} {2} {3}", modelSellOrder.Province, modelSellOrder.City, modelSellOrder.Area, modelSellOrder.Street); row["店名"] = ""; row["经营地址"] = ""; row["发展时间"] = dr["CreateTime"]; row["到期日期"] = ""; row["生日"] = dr["BirthDay"]; row["备注"] = ""; row["邮件内容"] = dr["Email"]; row["积分"] = dr["UsablePoint"]; row["性别"] = dr["Sex"]; row["证件类型"] = ""; row["证件号码"] = ""; row["店铺名称"] = "官网"; row["视力矫正要求"] = ""; row["屈光度状态"] = ""; row["自定义属性3"] = ""; row["自定义属性4"] = ""; row["自定义属性5"] = ""; row["自定义属性6"] = ""; row["自定义属性7"] = ""; row["自定义属性8"] = ""; row["自定义属性9"] = ""; dt.Rows.Add(row); } } //GridView1.DataSource = dt; //GridView1.DataBind(); string xlsHeaderText = string.Format("{0} (导入时删除此行)", "客户档案"); //Common.ExcelHelper.Export(dt, null, "客户档案(未下单).xls"); int recordCount = dt.Rows.Count; ; int pageSize = 10000; int pageCount = (recordCount % pageSize) == 0 ? recordCount / pageSize : (recordCount / pageSize) + 1; MemoryStream st = new MemoryStream(); using (ZipFile zip = ZipFile.Create(st)) { zip.BeginUpdate(); for (int pageIndex = 1; pageIndex <= pageCount; pageIndex++) { DataTable dtPage = Common.Utility.GetPage(pageIndex, pageSize, out recordCount, dt); MemoryStream ms = Common.ExcelHelper.GetExcelStream(dtPage, xlsHeaderText); StreamDataSource sds = new StreamDataSource(ms); zip.Add(sds, string.Format("客户档案({0}).xls", pageIndex)); } zip.CommitUpdate(); } string filename = "客户档案.zip"; Response.ContentType = "application/zip"; if (Request.UserAgent.ToLower().IndexOf("msie") > -1) { filename = HttpUtility.UrlPathEncode(filename); } if (Request.UserAgent.ToLower().IndexOf("firefox") > -1) { Response.AddHeader("Content-Disposition", string.Format("attachment;filename=\"{0}\"", filename)); } else { Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename)); } Response.BinaryWrite(st.GetBuffer()); Response.Flush(); Response.End(); }