0

I'm trying to write a file with utf-8 encoding. This file I would like to see on PC in Excel I've looked at enough examples of implementing this code. But I did not achieve the desired results. My code:

    String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
    String fileName = etName.getText().toString() + ".csv";
    String filePath = baseDir + File.separator + fileName;
    File sdCardFile = new File(filePath);

    String DATE = "01.04.2017 - 30.04.2017";
    String NEW_LINE = "\n";
    String NEW_CELL = ";";
    String HEADER = "Место;" +
            "Фамилия ИО;" +
            "Цикл ТО-1;" +
            "Культурное состояние л-ва;" +
            "Посещение тех.занятий;" +
            "Расшифровка лент;" +
            "Охрана труда;" +
            "Отказы";

BufferedWriter bufferedWriter = new BufferedWriter
                (new OutputStreamWriter(new FileOutputStream(sdCardFile), "UTF-8"));
        bufferedWriter.write(DATE);
        bufferedWriter.write(NEW_LINE);
        bufferedWriter.write(HEADER);
        for(int i = 0; i < machinists.size(); i ++){
            bufferedWriter.write(NEW_LINE);
            bufferedWriter.write(String.valueOf(i+1));
            bufferedWriter.write(NEW_CELL);
            bufferedWriter.write(machinists.get(i).getLastName());
            bufferedWriter.write(NEW_CELL);
            bufferedWriter.write(String.valueOf(machinists.get(i).getPoints().getTo()));
            bufferedWriter.write(NEW_CELL);
            bufferedWriter.write(String.valueOf(machinists.get(i).getPoints().getCultureState()));
            bufferedWriter.write(NEW_CELL);
            bufferedWriter.write(String.valueOf(machinists.get(i).getPoints().getTechnicalSessions()));
            bufferedWriter.write(NEW_CELL);
            bufferedWriter.write(String.valueOf(machinists.get(i).getPoints().getTapeTranscript()));
            bufferedWriter.write(NEW_CELL);
            bufferedWriter.write(String.valueOf(machinists.get(i).getPoints().getOt()));
            bufferedWriter.write(NEW_CELL);
            bufferedWriter.write(String.valueOf(machinists.get(i).getPoints().getFault()));
            bufferedWriter.write(NEW_CELL);
        }
        bufferedWriter.close();
    }

But I see this.

screenshot excel csv

Why?

niklas1987
  • 61
  • 9

2 Answers2

3

I solved this problem!

FileOutputStream os = new FileOutputStream(sdCardFile);
        os.write(0xef);<-!!!!!!!!
        os.write(0xbb);<-!!!!!!!!
        os.write(0xbf);<-!!!!!!!!
        CSVWriter writer = new CSVWriter(new OutputStreamWriter(os), ';');
writer.writeNext(HEADER);
...
writer.writeNext('something data');
writer.close();
niklas1987
  • 61
  • 9
0

I am not sure if this will help, but maybe formatting the string to UTF-8 this way can help:

bufferedWriter.write(new String(machinists.get(i).getLastName().getBytes(), "UTF-8"));

And similarly for the headers.

Edit:

Another thing you can try is to add " for all the values and , as the delimiter (csv stands for "comma separated values" after all):

"1","Аркаев","10","7","6","5","0","6",
Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60
  • No. This not help me (( – niklas1987 May 11 '17 at 14:39
  • bufferedWriter.write("\"" +machinists.get(i).getLastName() + "\"," ); bufferedWriter.write("\"" + new String(HEADER.getBytes(), "UTF-8") + "\","); doesn't help me (((( – niklas1987 May 11 '17 at 15:18
  • Maybe the problem is with how you open the file? – Doron Yakovlev Golani May 11 '17 at 15:35
  • @ Doron Yakovlev-Golani just double click. – niklas1987 May 11 '17 at 15:44
  • I don't have Excel installed - I see Cyrilic letters when I open the file in Numbers (Apple's version of Excel). Anyway, the text is not formatted as CSV as you are using the wrong delimiter. If I were you, I'd fix that first and then make sure I am importing the file properly (maybe instead of double clicking it, you should try to manually import from within - I've seen changes in the behavior in the past between the two methods). – Doron Yakovlev Golani May 11 '17 at 15:50