gridview 导出excel

        /// <param name="gv">需要导出数据的Gridview</param>
        /// <param name="dt">Gridview的数据源</param>
        /// <param name="strFileName">默认的导出Excel的文件名</param>
        /// <param name="bolPart">全部还是部分导出到Excel.部分:true. 全部:false</param>

        public static void ConvertToExcel(GridView gv, DataTable dt, string strFileName, bool bolPart)
        {
            gv.AllowPaging = bolPart;//设置导出数据是全部还是部分
            gv.DataSource = dt;
            gv.DataBind();

            //循环遍历GridView中的每一列
            for (int i = 0; i < gv.Columns.Count; i++) //设置每个单元格
            {
                gv.Columns[i].ItemStyle.HorizontalAlign = HorizontalAlign.Left;
                for (int j = 0; j < gv.Rows.Count; j++)
                {
                    gv.Rows[j].Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
                }
            }

            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");



            strFileName += ".xls";
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(strFileName));//设置默认文件名
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            //预防出现控件必须放在具有 runat=server 的窗体标记内的错误
            Page page = new Page();
            HtmlForm form = new HtmlForm();
            gv.EnableViewState = false;
            page.EnableEventValidation = false;
            page.DesignerInitialize();
            page.Controls.Add(form);
            form.Controls.Add(gv);
            page.RenderControl(htw);

            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();


 protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            string Year = System.DateTime.Now.Year.ToString();
            string Month = System.DateTime.Now.Month.ToString();
            string Day = System.DateTime.Now.Day.ToString();
            string DateString = Year + "-" + Month + "-" + Day;

            ExportToExcel("application/ms-excel", DateString + ".xls");
        }
        public void ExportToExcel(string FileType, string FileName)
        {
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
            Response.ContentType = FileType;
            this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            GridView1.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {    
            //base.VerifyRenderingInServerForm(control);
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值