3

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;
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1Mayur
  • 3,419
  • 5
  • 40
  • 64
  • 1
    You haven't shown how you're writing it in Excel, or whether you're telling Excel that it's UTF-8 data in any way, shape or form... – Jon Skeet Nov 01 '11 at 10:34
  • 1
    Can you please show more of your code? The line you posted only converts your UTF-8 string into a byte array. No problem there :-) – Christoph Grimmer Nov 01 '11 at 10:35
  • What's this `partialtorender` variable? HTML, CSV, ...? – Darin Dimitrov Nov 01 '11 at 10:44
  • @msirwani, yes I understand that it is a string variable. But what is it contents? You realize that `xls` files are binary files with proprietary format. In order to generate an xls file you might need to use Office Interop. – Darin Dimitrov Nov 01 '11 at 12:42

3 Answers3

10

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");
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • With some minor fixes in code, It worked like a charm. Thanks Dimitrov – 1Mayur Nov 02 '11 at 07:15
  • Do you also have the problem that opening & saving this csv with Excel converts the csv to space delimited instead of ';' delimited? – MichaelD Apr 04 '18 at 11:24
  • thank you, also works when opening csv (utf8) containing kazakh symbols in excel (with default encoding 1251) encode kz1048 not supportd in dotnet – nayaganov Oct 07 '22 at 04:54
0

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!.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Ronen Festinger
  • 2,260
  • 1
  • 23
  • 32
0

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?

Christoph Grimmer
  • 4,210
  • 4
  • 40
  • 64
  • This does not solve the issue. I have tried doing this before. I am using Ms Office 2010... Does that create an issue?? – 1Mayur Nov 01 '11 at 11:10
  • The main problem is, that you do not show the code that generates the xls content. Do you create a xls file using the interop assemblies? And if so - why don't you create an xlsx file? – Christoph Grimmer Nov 01 '11 at 11:29
  • As Darin Dimitrov already asked: How is the content generated? How do you create the string that is supposed to be an Excel sheet in the end? – Christoph Grimmer Nov 01 '11 at 11:43