Itz.Irshad
Itz.Irshad

Reputation: 1024

Why MS Excel does not open properly a CSV file containing Japanese characters in contents?

A CSV file containing Japanese characters (UTF-8 characterset) is created from server-side using ASP.NET.

Code:

public void ExportToCsv_Click(object sender, EventArgs e)
    {
        try
        {
        string sContents = string.Empty;
        string sTemp;

        for (int k = 1; k <= (ObjList.Columns.Count - 1); k++)
        {
            sContents += ObjList.HeaderRow.Cells[k].Text + ",";
        }

        sContents += "\r\n";

        for (int i = 0; i <= (ObjList.Rows.Count - 1); i++)
        {
            for (int j = 1; j <= (ObjList.Columns.Count - 1); j++)
            {
                sTemp = (ObjList.Rows[i].Cells[j].Text.ToString());
                if (sTemp == "&nbsp;")
                    sTemp = "";
                if (j < ObjList.Columns.Count - 1)
                    sTemp = (sTemp + ",");
                sContents += sTemp;
            }
            sContents += "\r\n";
        }

        Response.Clear();
        Response.ClearHeaders();
        Response.ClearContent();

        Response.AddHeader("Content-disposition", "attachment; filename=" + "partslist.csv");
        Response.ContentType = "application/csv";
        Response.Charset = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8  ;

        Response.Write(sContents);
        Response.Flush();
        Response.End();
        }
        catch (Exception ex)
        {
    throw new Exception(ex.Message);
    }
    }

But, when this file is opened in Microsoft Excel 2003, it does not show file contents properly.

How to make excel to open such a file properly. Help will be appreciated.

Upvotes: 1

Views: 1580

Answers (3)

jack
jack

Reputation: 11

Just add \xfeff at the beginning.

Upvotes: 1

htnux
htnux

Reputation: 63

Excel uses SJIS(Shift JIS) as default encoding.
You could try converting UTF-8 to SJIS or creating CSV file with BOM in UTF-8.
I was able to open UTF-8 csv file with BOM.

Upvotes: 0

d-stroyer
d-stroyer

Reputation: 2706

Follow this procedure :

  • Menu -> Data -> GetExternal Data -> From Text
  • Select your csv using the file dialog
  • In the text import wizard, step 1, select "65001 : Unicode (UTF-8)" as file origin.

Upvotes: 0

Related Questions