zoukankan      html  css  js  c++  java
  • C#导出Word [ZT]

    private void Button13_Click(object sender, System.EventArgs e)
    {
    this.Datagrid4.Visible=true;
    Response.Clear();
    Response.Buffer= true;
    Response.Charset="GB2312";
    Response.AppendHeader("Content-Disposition","attachment;filename=File1.DOC");
    Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
    Response.ContentType = "application/ms-word";
    this.Datagrid4.EnableViewState = false;
    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter (oStringWriter);
    this.Datagrid4.RenderControl(oHtmlTextWriter);
    Response.Write(oStringWriter.ToString());
    Response.End();
    }

    在做ASP.NET项目时,会经常遇到要导出文件的问题,如将DataGrid中的数据导出到excel文件等,经常使用的是Office中的OWC组件,这个组件提供的功能很强大,在一般的项目中都可以满足当前的需要.但是这个功能强大的组件使用起来却不是很方便,不但有版本的问题,而且代码量也相对比较大.如果要利用Respone对象和相关的IO,也可以实现到处excel/word等文件,而且使用方便.

    代码如下:

    System.IO.StringWriter SW = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter HTW=new System.Web.UI.HtmlTextWriter(SW);
    Page.RenderControl(HTW);
    //Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以
    Response.Buffer=true;

  • 相关阅读:
    hdu-5492 Find a path(dp)
    hdu-5493 Queue(二分+树状数组)
    bzoj-2243 2243: [SDOI2011]染色(树链剖分)
    codeforces 724
    codeforces 422A A. Borya and Hanabi(暴力)
    codeforces 442C C. Artem and Array(贪心)
    codeforces 442B B. Andrey and Problem(贪心)
    hdu-5918 Sequence I(kmp)
    poj-3739. Special Squares(二维前缀和)
    hdu-5927 Auxiliary Set(树形dp)
  • 原文地址:https://www.cnblogs.com/RobotTech/p/902697.html
Copyright © 2011-2022 走看看