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());
                }
            }

  • 相关阅读:
    如何制作挖空的填空题试卷?
    原型制图工具有哪些?
    书籍推荐?来几本吧
    离线部署ELK+kafka日志管理系统【转】
    Elasticsearch5.0 安装问题集锦【转】
    在Nginx服务器上屏蔽IP
    MySQL Warning: Using a password on the command line interface can be insecure.解决办法
    不老的神器:安全扫描器Nmap渗透使用指南【转】
    MySQL数据库设置为只读及测试【转】
    Linux中切换用户变成-bash4.1-$的解决方法
  • 原文地址:https://www.cnblogs.com/zhuawang/p/748636.html
Copyright © 2011-2022 走看看