• C# GridView导出excel和word文件


    //导出excel按钮事件

    protected void Button1_Click(object sender, EventArgs e)

    {

    Export("application/ms-excel", "农田环境数据.xls");

    }

    //导出word按钮事件

    protected void Button2_Click(object sender, EventArgs e)

    {

    Export("application/ms-excel", "农田环境数据.doc");

    //Export("application/ms-word", "农田环境数据.doc");//都可以

    }

    private void Export(string FileType, string FileName)

    {

    Response.Charset = "GB2312";

    Response.ContentEncoding = System.Text.Encoding.UTF8;

    Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());

    Response.ContentType = FileType;

    this.EnableViewState = false;

    StringWriter tw = new StringWriter();

    HtmlTextWriter hw = new HtmlTextWriter(tw);

    GridView1.RenderControl(hw); Response.Write(tw.ToString());

    Response.End();

    }

    // 此方法必重写,否则会出错

    public override void VerifyRenderingInServerForm(Control control)

    {

    }

  • 相关阅读:
    vs2005 配置winpcap
    qt 解决中文乱码问题
    [翻译] QT正则表达式
    使用QSetting 读写ini文件
    [转]GNOME快捷键
    华为面试题之大整数相加
    qt 程序windows 上发布
    win7英文版中文乱码问题
    CURL命令 Alex
    Sendfile机制 Alex
  • 原文地址:https://www.cnblogs.com/zssblog/p/5616499.html
Copyright © 2020-2023  润新知