You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

导出包含阿拉伯字符的Excel时触发GDI+通用错误的解决求助

导出包含阿拉伯字符的Excel时触发GDI+通用错误的解决求助

我现在碰到个棘手的问题:我写了个从报表生成Excel文件的方法,但当列里包含阿拉伯字符(比如" ُ"、" َ"这类符号)时,就会抛出“A generic error occurred in GDI+”的错误。这些字符是业务必须保留的,不能删掉,有没有遇到过同样问题的朋友能给我指点一下?

我已经搜过相关解决方案,还添加了这些响应头设置,但问题还是没解决:

HttpContext.Current.Response.ContentEncoding = Encoding.Unicode;
HttpContext.Current.Response.BinaryWrite(Encoding.Unicode.GetPreamble());

下面是我的完整代码,麻烦大家帮忙排查下哪里出了问题:

using( var wb = new XLWorkbook() )
{
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Buffer = true;
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + Guid.NewGuid() + ".xls");
    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
    
    using( var myMemoryStream = new MemoryStream() )
    {
        var maxRowsPerSheet = Server.GetConfig("MaxRow");
        if( !string.IsNullOrEmpty(maxRowsPerSheet) && int.TryParse(maxRow, out int max) )
        {
            var tables = SplitTable(table, max);
            foreach( var tbl in tables )
            {
                wb.Worksheets.Add(tbl, "sheet" + (tables.IndexOf(tbl) + 1));
            }
        }
        else
        {
            wb.Worksheets.Add(table, "sheet1");
        }
        
        wb.SaveAs(HttpContext.Current.Response.OutputStream);
        myMemoryStream.Position = 0;
    }
}

备注:内容来源于stack exchange,提问作者Sara Na

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 08:49:35