Reputation: 3485
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;
Upvotes: 3
Views: 5795
Reputation: 2405
I found out the solution after a lot of struggle. If you look at the text of the xls file you will see it's not a well formed html, anyway excel does handle it. But I thought i should let excel know it's utf-8. so i used:
Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>");
before writing the rest of the stream, and it does work!.
Upvotes: 0
Reputation: 1038710
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");
}
Upvotes: 10
Reputation: 4313
Your problem could be that the default encoding for XLS files is not UTF-8. Please save the file you got and open it according to this post: Choose an encoding standard when you open a file Does this fix your problem?
Upvotes: 0