用户点击导出EXCEL按钮后,出现文件保存对话框,再点击保存,这里默认的文件名是乱码
部分代码如下Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName,Encoding.Unicode)); 
Response.ContentType = "application/vnd.ms-excel";
Response.WriteFile(fileName); 
Response.End();
若改成HttpUtility.UrlEncode(fileName,Encoding.UTF8),可以解决中文名的问题,但是,用户在文件保存对话框上不点保存,而是点打开,会出现找不到文件的错误提示。请问有没有一个解决方法,既可以直接打开文件,还可以保存时默认为中文文件名

解决方案 »

  1.   

    Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("Excel.xls", System.Text.Encoding.GetEncoding("GB2312"))); 
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); 
      

  2.   

    // Response.ContentType = "application/x-rar-compressed";
       Export("application/ms-excel", filename ); //注意文件一类型
    filename//为路径
     private void Export(string FileType, string FileName)
            {
                Response.Charset = "GB2312";
                Response.ContentEncoding = System.Text.Encoding.UTF7;
                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();
               
            }常用这个