zoukankan      html  css  js  c++  java
  • 在ASP.NET中将GridView数据导出到Word、Excel

    在ASP.NET中将GridView数据导出到Word、Excel

    asp.net,导出gridview数据到Word,Excel,PDF
     

        #region Export to Word, Excel and PDF
        protected void btnword_Click(object sender, EventArgs e)
        {
            Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.word";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            // Create a form to contain the grid
            HtmlForm frm = new HtmlForm();
            GridView4.Parent.Controls.Add(frm);
            frm.Attributes["runat"] = "server";
            frm.Controls.Add(GridView4);
            frm.RenderControl(htmlWrite);

            //GridView1.RenderControl(htw);
            Response.Write(stringWrite.ToString());
            Response.End();
        }
        protected void btnexcel_Click(object sender, EventArgs e)
        {
            string attachment = "attachment; filename=Contacts.xls";
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            // Create a form to contain the grid
            HtmlForm frm = new HtmlForm();
            GridView1.Parent.Controls.Add(frm);
            frm.Attributes["runat"] = "server";
            frm.Controls.Add(GridView1);
            frm.RenderControl(htw);

            //GridView1.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
        protected void btnpdf_Click(object sender, EventArgs e)
        {
            Response.Clear();

            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            GridView1.RenderControl(htw);

            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment; filename=MypdfFile.pdf");
            Response.Write(sw.ToString());
            Response.End();
        }

  • 相关阅读:
    Java13新特性 -- 重新实现旧版套接字API
    Java13新特性 -- switch表达式动态CDS档案(动态类数据共享归档)
    Java13新特性 -- 文本块
    Java13新特性 -- switch表达式
    Java12新特性 -- 其他新增,移除,废弃项
    Java12新特性 -- 增强G1,自动返回未用堆内存给操作系统
    Java12新特性 -- 可中断的 G1 Mixed GC
    Java12新特性 -- 默认生成类数据共享(CDS)归档文件
    Java12新特性 -- 只保留一个 AArch64 实现
    python使用requests发送text/xml报文数据
  • 原文地址:https://www.cnblogs.com/xianyin05/p/3187258.html
Copyright © 2011-2022 走看看