保存为Excel文件

来源:互联网 发布:lz4压缩算法 编辑:程序博客网 时间:2024/04/28 01:50

    Response.Clear();
    Response.Buffer= true;
    Response.Charset="GB2312";
    string fileName = Server.UrlEncode(nameTxt.Value);
    if(fileName == "")
     fileName = "file";

    Response.AppendHeader("Content-Disposition","attachment;filename=" + fileName + ".xls");
    Response.ContentType = "application/ms-excel";
    Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");

    this.EnableViewState = false;

    System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("zh-CHS",true);
    System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
    this.RenderControl(oHtmlTextWriter);
    Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
    Response.Write(oStringWriter.ToString());
    Response.End();