简体   繁体   中英

Creating TSV file in Android: when I try to open in Excel it detects it as ANSI, I need UTF-8

In my Android application I create a TSV file and I send it via e-mail. When I try to open it using Excel it detects it as ANSI, so some of my characters aren't seen as they should. I have another application not created by me that does the same, but Excel detects it as UTF-8. I've checked the HEX code of both files and I found that he uses C2 A0 in some of his spaces (not tabs), while I use 20.

This is the code I use to create the file:

        FileOutputStream writeFileHandler = context.openFileOutput(LOG_FILENAME_CSV, Context.MODE_WORLD_READABLE);
        OutputStreamWriter oputStreamWriter = new OutputStreamWriter(writeFileHandler, "UTF-8");

        for(int i=log_rows.length-1; i>=0; i--){
            String[] entries = getDailyLogEntries(log_rows[i]); // array of your values
            for(int j=0; j<entries.length; j++){
                if(j==0)  oputStreamWriter.write(entries[j]);
                else{
                    if(entries[j] != null) oputStreamWriter.write("\t"+entries[j]);
                }
            }
            oputStreamWriter.write("\n");
        }
        oputStreamWriter.flush();
        oputStreamWriter.close();
        writeFileHandler.close();

I tried to pull the file using DDMS to avoid the e-mail sending and the result is the same, it's detected as ANSI. How can I do it so Excel detects it as UTF-8?

Thanks!

Have you tried adding the UTF-8 BOM at the start of your TSV file? This may persuade Excel that it's a UTF-8 file. The byte sequence is 0xEF,0xBB,0xBF.

See http://en.wikipedia.org/wiki/Byte_order_mark

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM