I use the following code to write a csv file:
public static void main(String[] args) throws IOException {
String str = "aa\tbb\tcc\tdd";
Writer writer = new OutputStreamWriter(new FileOutputStream(new File("c:\\test\\test.csv")), "UTF-16LE");
writer.append(str);
writer.flush();
writer.close();
}
when I open the file in excel, the columns are not separated. They are all in one column.
I open the file in notepad++ and see that its encoded with ‘UCS-2 LE w/o BOM’.
If I save the file encoded with ‘UCS-2 Little endian’ and them open it in excel, the columns are separated .
How can I write the file in java in this encoding so that it will show separated columns in excel?
Use a comma instead of a tab, e.g.
I don’t believe Excel separates data columns delimited with Tabs by default. You can do it but it takes an additional step.