zoukankan      html  css  js  c++  java
  • 通过GridView导出Excel

    protected void ToExcel(GridView gv,string name) {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + name + ".xls");
                // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
                Response.ContentEncoding = System.Text.Encoding.UTF7;
                Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                gv.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();
            }

            public override void VerifyRenderingInServerForm(Control control)
            {

            }
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
                }
            }


            protected void btnToExcel_Click(object sender, EventArgs e)
            {
                if (gvStat.Rows.Count > 0 && gvStat.Visible == true) {
                    ToExcel(gvStat, "from " + ViewState["StartDate"].ToString() + " to " + ViewState["EndDate"].ToString());
                }
                else if (gvUser.Rows.Count > 0 && gvUser.Visible == true) {
                    ToExcel(gvUser, "from " + ViewState["StartDate"].ToString() + " to " + ViewState["EndDate"].ToString());
                }
            }

  • 相关阅读:
    【转载】python自动提取文本中的时间(包含中文日期)
    【转载】PCA降维
    【转载】从头到尾彻底理解KMP算法
    【转载】傅里叶分析之掐死教程(完整版)更新于2014.06.06(Heinrich)
    【转载】白素贞的身世之谜
    React 路由参数改变页面不刷新问题
    前端学习资源笔记
    Nginx配置网站默认https
    基于Docker搭建大数据集群(一)Docker环境部署
    基于Docker搭建大数据集群(三)Hadoop部署
  • 原文地址:https://www.cnblogs.com/zhuawang/p/748636.html
Copyright © 2011-2022 走看看