I create csv file with data by the means of java. And I faced the following well-known issue: the letters in Portuguese were displayed by the wrong way in Excel (when opening by double click).
I solved this by UTF-16LE+BOM, but excel started to recognize tabs as columns separators instead of commas.
So I looked up for another solution and saw many posts, in which people say that just adding UTF-8 BOM and writing file in UTF-8 will do the job for Excel 2007 and later. I tried the simpliest sample on my work computer and it failed. But when I tried this at my home computer it worked like a charm.
Both computers have the same versions of java installed and operating system Windows 7. I am confused. Can anyone tell what can cause such a strange behaviour?
You can see my simpliest sample below:
String filename = "D:/check/test_with_bom.csv";
FileOutputStream fos = new FileOutputStream(filename);
byte[] bom = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF };
fos.write(bom);
OutputStreamWriter osw = new OutputStreamWriter(fos , "UTF-8");
PrintWriter printWriter = new PrintWriter(osw);
printWriter.print("Hello,Olá,ão,ção");
printWriter.close();
PrintWriter
? Is callingwrite
on theWriter
so much harder than callingprint
on thePrintWriter
? – Holger