Excel can sometimes have trouble opening UTF-8-encoded CSV files without a BOM. You can add a BOM at the beginning of the file to ensure proper encoding. Try this:
FileOutputStream fos = new FileOutputStream("test.csv");
fos.write(0xEF);
fos.write(0xBB);
fos.write(0xBF);
Writer writer = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
writer.append("ID,name\n"); // Writing header
writer.append("1,John Doe\n"); // Writing data
writer.flush();
writer.close();
Why this helps?
Excel sometimes misinterprets the encoding, but adding a BOM ensures it recognizes UTF-8 correctly.