0

I tried the program below to write data from the DB to a CSV file. When I open the CSV file, it's displaying junk values for Spanish special characters.

public class T {
    CSVWriter out = null;

    private void write(String[] values) throws IOException {
        out.writeNext(values);
    }

    public static void main(String[] args) throws IOException {

        File f  = new File("s.csv");

        FileOutputStream os = new FileOutputStream(f, false);
        os.write('\ufeff');

        CSVWriter out = new CSVWriter(
            new BufferedWriter(
                new OutputStreamWriter(
                    os, "UTF-8")));
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
ramya
  • 43
  • 1
  • 6
  • Are you starting off with a CSV dump from a database, or are you trying to create a CSV file? – Tim Biegeleisen Jun 11 '17 at 14:37
  • i am reading data from db and writing into csv file. in database its stored correct spanish characters database characterset is WE8MSWIN1252. i am creating a csv file and writing spanish data from db to csv – ramya Jun 11 '17 at 14:40
  • You are opening the csv directly into excel; probably by double-clicking it from an explorer window. Open it using Data, Get External Data, From Text and choose 65001: Unicode (UTF-8) as the File Origin. –  Jun 11 '17 at 14:49
  • See [How to open a text file with Excel in UTF-8 encoding?](https://stackoverflow.com/questions/43307776/how-to-open-a-text-file-with-excel-in-utf-8-encoding/43308865#43308865). –  Jun 11 '17 at 14:51
  • Acually i dont want to open manually, java program is reading the data and doing some process. – ramya Jun 11 '17 at 14:56
  • I'm not going to test for a Java platform but VBA has a [Workbooks.OpenText](https://msdn.microsoft.com/en-us/library/office/ff837097.aspx) you should be able to access with an application.workbooks.opentext. Set the *origin* to 65001 (unicode utf-8) or maybe 1252 (western european windows). –  Jun 11 '17 at 14:59
  • If your data is already being stored in windows-1252 encoding, just use that instead of utf8 and get rid of the BOM. Microsoft compatibility with utf8 is notoriously bad. – Phil Jun 12 '17 at 01:51
  • I cannot hardcode characterset as windows-1252, in the user environment he can use any characterset for db. UTF8 should support all languages. But why it is not supporting Spanish. I changed incoding from UTF8 to ISO-8859-1. Now the excel is opening correctly. But i cannot hardcode ISO-8859-1 in my code. because ISO-8859-1 will not support all languages – ramya Jun 12 '17 at 03:37

1 Answers1

0

Change encoding from utf8 to ISO-8859-1 works. But its the common code used for other languages also. i cannot hardcode ISO-8859-1. Why excel will dispay junk values when I specify UTF8.

ramya
  • 43
  • 1
  • 6