简体   繁体   中英

Write Non Latin Characters in CSV using Java

We have modified the application to support Non-Latin characters (Chinese & Thai) but we faced some issues in writing to CSV and resolved using below approach.

String line = "שלום, hello, привет";
    OutputStream os = new FileOutputStream("c:/temp/j.csv");
    os.write(239);
    os.write(187);
    os.write(191);

    PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));

    w.print(line);
    w.flush();
    w.close();

The above one works but we are facing some other issues listed below.

  1. We opened a CSV file using below code and it was working fine.

     File file = new File(System.getProperty(TEMP_DIR_SYSPROP), filename); FileUtils.writeByteArrayToFile(file, bytes); Desktop.getDesktop().open(file); 

But when it is written with UTF-8 we are facing below issue with excel.

NonLatin.csv is locked for editing by another user. Open 'Read-Only' or click 'Notify' to open and receive notification when the document is no longer in use. Or Cancel out.

  1. Double quotes is getting for few entries in csv which shows as different content when filter is applied.

Please suggest if there is any limitation with above approach (UTF-8) or is there any work around to handle this.

Since you are dealing with CSV, I would look at how Excel is reading the file as. With Libre Office it is available as soon as I try to import the CSV file, so look for something similar in Excel enter image description here

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