zoukankan      html  css  js  c++  java
  • 导出excel/xml

    1.GridView导出Excel表

    protected void Page_Load(object sender, EventArgs e)   { if (!IsPostBack)  BindData(); }
    private void BindData() {...}
    protected void Button1_Click(object sender, EventArgs e)
        {
            GridView1.AllowPaging = false;  //导出Gv的全部页数据,不加则导出一页
            BindData();
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
            Response.ContentEncoding = System.Text.Encoding.UTF7;   // GetEncoding("GB2312")会乱码
            Response.ContentType = "application/ms-excel";    //输出类型为excel ,要输出word时为ms-word
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            this.GridView1.RenderControl(oHtmlTextWriter);
            Response.Output.Write(oStringWriter.ToString());
            Response.Flush();
            Response.End();
            GridView1.AllowPaging = true;
            BindData();
        }

    public override void VerifyRenderingInServerForm( Control control )  { }  //不加此函数会出现导出错误

    2.定时自动导出XML(即无保存提示)

      //content:<news><newsid>..</newsid><newstitle>...</newstitle>..</news>
        //fileName:news.xml
        //filePath:@"D:\XML\"
        //web.config的路径配置可以是:D:\XML\,D:\\XML\\,D:/XML/,但D:\,D:\\不行,D:\\XML不是期望值
        private void OutXmlFile(string content, string fileName, string filePath)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(content);

            XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            XmlElement root = doc.DocumentElement;
            doc.InsertBefore(xmldecl, root);

            if (!Directory.Exists(Path.GetDirectoryName(filePath))) Directory.CreateDirectory(Path.GetDirectoryName(filePath));
            doc.Save(filePath + fileName);
        }

  • 相关阅读:
    Mac新手必看教程——轻松玩转Mac OS
    《图解 HTTP》 学习笔记
    在浏览器输入 URL 回车之后发生了什么(超详细版)
    VMware Ubuntu 19最新安装详细过程
    【学习笔记】第八章 python3核心技术与实践--条件与循环
    【学习笔记】第七章 python3核心技术与实践--输入与输出
    【学习笔记】第六章 python核心技术与实践--深入浅出字符串
    Prometheus搭建
    java---yml文件配置
    Nginx一个server配置多个location(使用alias)
  • 原文地址:https://www.cnblogs.com/vipcjob/p/1532342.html
Copyright © 2011-2022 走看看