大于65535的数据GridView导出Excel压缩包

。。。

/// <summary>
    /// 大于65535时导出压缩包
    /// </summary>
     private void getExcelFilesZip()
     {
         List<DataTable> dtList = createDTList();
         MemoryStream ms = new MemoryStream();
         ZipOutputStream zos = new ZipOutputStream(ms);
         DateTime datetime = DateTime.Now;
         for (int indexDt = 0; indexDt < dtList.Count; indexDt++)
         {
             s_DgToExcel(dtList[indexDt], ref zos, "Excel_" + indexDt.ToString() + ".xls");
         }
         zos.Finish();
         zos.Close();
         //压缩
         Response.ContentType = "application/octet-stream";
         Response.AddHeader("Content-Disposition", "attachment; filename=Excel" + datetime.ToString("yyyyMMddHHmmss") + ".zip");
         Response.Clear();
         Response.BinaryWrite(ms.ToArray());
         Response.End();
     }

     private void s_DgToExcel(DataTable Dt,ref ZipOutputStream zos,string fileName)
     {
         GridView gv = new GridView();
         gv.AllowPaging = false;
         gv.PageIndex = 0;
         gv.DataSource = Dt;
         gv.DataBind();

         //设置datagrid界面样式,即保存给excel的样式
         gv.SelectedRowStyle.BackColor = Color.White;
         gv.AlternatingRowStyle.BackColor = Color.White;
         gv.RowStyle.BackColor = Color.White;
         gv.HeaderStyle.BackColor = Color.Silver;
         gv.HeaderStyle.ForeColor = Color.Black;
         gv.BorderColor = Color.Black;

         EnableViewState = false;
         System.IO.StringWriter stwExcel = new System.IO.StringWriter();
         HtmlTextWriter htwExcel = new System.Web.UI.HtmlTextWriter(stwExcel);
         htwExcel.AddAttribute(HtmlTextWriterAttribute.Title, "charset=gb2312");
         htwExcel.Write("<META>");

         gv.RenderControl(htwExcel);
         byte[] unicodeBytes = (System.Text.Encoding.GetEncoding("GB2312")).GetBytes(stwExcel.ToString());
         if (zos != null)
         {
             ZipEntry entry = new ZipEntry(fileName);
             zos.PutNextEntry(entry);
             zos.Write(unicodeBytes, 0, unicodeBytes.Length);
         }
         htwExcel.Close();
         stwExcel.Close();
     }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

L_serein

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值