【问题标题】:Mojibake characters showing after exporting excel导出 excel 后显示的 Mojibake 字符
【发布时间】:2012-07-26 03:48:20
【问题描述】:

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

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

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

实际: - 显示包含 mojibaki 字符的奇怪页面。请查看此网址中的图片http://i.imgur.com/dIsZc.png

以下是使用活动报告生成 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);

    }

任何帮助将不胜感激!太感谢了! :)

【问题讨论】:

    标签: asp.net excel encoding export mojibake


    【解决方案1】:

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

    查看这个相关的 SO 问题:Character encoding in Excel spreadsheet (and what Java charset to use to decode it)

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-05
        • 1970-01-01
        • 2014-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-12
        相关资源
        最近更新 更多