zoukankan      html  css  js  c++  java
  • 将GridView导入到Excel

    前几天做项目需要用到将GridView中的数据导入到Excel,原以为很简单,上网搜了一下资料,果然方法不少,于是就采用了一个:

    private void GridViewToExcel()
            {
               Response.Clear();
               Response.BufferOutput = true;
               //设定输出的字符集 
               Response.Charset = "GB2312";
    
               //假定导出的文件名为FileName.xls 
               Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
               Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
               //设置导出文件的格式 
               Response.ContentType = "application/vnd.ms-excel";
               //关闭ViewState 
               EnableViewState = false;
               System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
               System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
               System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
               GridView1.RenderControl(textWriter);
               //把HTML写回浏览器 
               Response.Write(stringWriter.ToString());
               Response.End();
            }

    引用此方法:

    protected void btnToExcel_Click(object sender, EventArgs e)
            {
                //直接导出Excel 
                 GridViewToExcel();
            }

    以上方法还不能真正实现功能,还需参考:

    http://www.cnblogs.com/XACOOL/p/5663778.html

    http://www.cnblogs.com/XACOOL/p/5663767.html

  • 相关阅读:
    ASP.NET实现文件下载的功能
    优先队列的使用
    关于CSS设置样式的一点建议
    ASP.NET后台弹出消息的方法
    创建漂亮的 CSS 按钮的 10 个代码片段(转载)
    2020高考游记
    最后的OI(HAOI2020游记)
    2020 2 2 20 20
    祭最后的亡魂
    一个感人至深的故事
  • 原文地址:https://www.cnblogs.com/XACOOL/p/5664273.html
Copyright © 2011-2022 走看看