UTF8不工作在Excel中 [英] UTF8 not working in Excel

查看:172
本文介绍了UTF8不工作在Excel中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的.NET code我使用

 字节[] bytesToSend = System.Text.Encoding.UTF8.GetBytes(partialtorender);
 

那么M以创作它脱颖而出

在印地文语言文字是未来胡言乱语在生成Excel中,可以请你建议该怎么办?

  System.IO.MemoryStream memStr =新System.IO.MemoryStream();

memStr.Write(bytesToSend,0,bytesToSend.Length);

 memStr.Position = 0;

FileStreamResult RESULT1 =新FileStreamResult(memStr,应用程序/ MS-Excel的);

Response.AddHeader(内容处置,附件;文件名=+newExcelSheet+的.xl​​s);

返回RESULT1;
 

解决方案

尝试发射的UTF-8 preamble以指示正确的编码到Excel。此外.XLS是一个专有的二进制格式。你不能只用一个字符串变量作为您code。

下面是一个CSV文件导出一个例子:

 公众的ActionResult指数()
{
    VAR CSV =मानकहिन्दी;其他一些价值;
    VAR数据= Encoding.UTF8.GetBytes(CSV);
    数据= Encoding.UTF8.Get preamble()CONCAT(数据).ToArray()。
    VAR CD =新ContentDisposition
    {
        内嵌=假,
        文件名=newExcelSheet.csv
    };
    Response.AddHeader(内容处置,cd.ToString());
    返回文件(数据,文/ CSV);
}
 

in my .net code i'm using

byte[] bytesToSend = System.Text.Encoding.UTF8.GetBytes(partialtorender);

then m writin it to excel

the text in "Hindi" Language is coming gibberish in generated excel, can you please suggest what to do?

System.IO.MemoryStream memStr = new System.IO.MemoryStream();

memStr.Write(bytesToSend, 0, bytesToSend.Length);

 memStr.Position = 0;

FileStreamResult result1 = new FileStreamResult(memStr, "application/ms-excel");

Response.AddHeader("content-disposition", "attachment; filename=" + "newExcelSheet" + ".xls");

return result1;

解决方案

Try emitting an UTF-8 preamble to indicate the correct encoding to Excel. Also .xls is a proprietary binary format. You cannot just use a string variable as in your code.

Here's an example with a CSV file export:

public ActionResult Index()
{
    var csv = "मानक हिन्दी;some other value";
    var data = Encoding.UTF8.GetBytes(csv);
    data = Encoding.UTF8.GetPreamble().Concat(data).ToArray();
    var cd = new ContentDisposition
    {
        Inline = false,
        FileName = "newExcelSheet.csv"
    };
    Response.AddHeader("Content-Disposition", cd.ToString());
    return File(data, "text/csv");
}

这篇关于UTF8不工作在Excel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆