导出excel后显示的Mojibake字符

时间:2012-07-26 03:48:20

标签: asp.net excel encoding export mojibake

我不确定是否有人遇到过这个问题。以下是复制问题的步骤。

步骤: 1.转到列表 - >联系人页面。 2.单击“导出”按钮以生成联系人列表的.XLS报告。 3.关闭.XLS报告并导航到其他页面,例如联系人列表。 4.在联系人列表中,单击“关闭”按钮以重定向回“联系人”列表页面。

预期: - 页面显示联系人列表页面。

实际值: - 显示包含mojibaki字符的奇怪页面。请参阅此网址http://i.imgur.com/dIsZc.png

中的图片

以下是使用Active报告生成Excel的代码:

private static void PushContentToHttp(ActiveReport report,MemoryStream msData,string fileName,string url)         {

        report.Run();
        XlsExport xls = new XlsExport();
        xls.DisplayGridLines = true;
        xls.AutoRowHeight = true;
        xls.Export(report.Document, msData);

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.Buffer = true;

        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        HttpContext.Current.Response.AddHeader("Content-Length", msData.Length.ToString());
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename= \"" + fileName + ".xls\"");
        HttpContext.Current.Response.AddHeader("Refresh", "3; url=" + url);
        HttpContext.Current.Response.Charset = "utf-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding(1252);
        //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
        HttpContext.Current.Response.BinaryWrite(msData.ToArray());
        HttpContext.Current.Response.End();
 //       HttpContext.Current.Response.Redirect(url,true);

    }

非常感谢任何帮助!非常感谢! :)

2 个答案:

答案 0 :(得分:0)

我打赌这是因为Excel的默认编码是ANSI 125x,而您正试图将其写入UTF-8。

请参阅此相关SO问题:Character encoding in Excel spreadsheet (and what Java charset to use to decode it)

答案 1 :(得分:0)

实际上,造成这种情况的原因是,您的页面会尝试重定向回浏览器缓存的Excel页面。我可以建议你做一个完整的重定向,而不是只做一个浏览器,以便上一页,将完全加载,而不是加载缓存的Excel页面。与ANSI编码无关,等等,只是不正确的重定向问题。