0

I have the below code but when open the generated CSV using Excel, the Arabic word show as symbols. If I open the file using Notepad++, it displays the value correctly.

CSVWriter writer234 = new CSVWriter(new OutputStreamWriter(new FileOutputStream("C:\\Users\\Desktop\\file456789.csv"), StandardCharsets.UTF_8));

String[] header = { "Field1", "Field2", "Field3", "Field4", "Field5", "Field6", "Field7", "Field8" };
writer234.writeNext(header);

String[] data = { "مرحبا", "Field2", "Field3", "Field4", "Field5", "Field6", "Field7", "Field8" };
writer234.writeNext(data);

writer234.close();
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
user3586286
  • 321
  • 1
  • 3
  • 9
  • IIRC, Excel uses the ANSI code page of the default locale of your machine, so if your locale isn't set to some Arabian locale, it likely uses something like windows-1252 (i.e. Western European/US locales) to open it, which will mean the wrong characters will be derived. – Mark Rotteveel Aug 02 '23 at 13:39
  • Do you have an Arabic-capable font in Excel? – g00se Aug 02 '23 at 13:39
  • In any case, see also https://superuser.com/questions/280603/how-to-set-character-encoding-when-opening-a-csv-file-in-excel how to select a different character set – Mark Rotteveel Aug 02 '23 at 13:40
  • 1
    It seems Excel has [a problem](https://stackoverflow.com/a/6488070/636009) with UTF-8 [CSV files](https://stackoverflow.com/a/155176/636009). You can prepend the 'byte order mark' (BOM), `"\ufeff"`, to the beginning of the file (either in the first field or before you pass the output stream into the CSV writer), or I think the UTF-16 encoding would automatically add a BOM. But for UTF-8, you'll have to add the BOM yourself. – David Conrad Aug 02 '23 at 13:50
  • 3
    Do the same as has been recommended so many times in this forum. Do not **OPEN** the file, rather **IMPORT** the file using either Power Query or the legacy import wizard. At that point, you will have the opportunity to select the correct code page. – Ron Rosenfeld Aug 02 '23 at 15:14

0 Answers0