Search code examples
javamavencsvapache-commons

CSVPrinter datas appears not separated in columns


I'm trying to use this library commons-csv (1.5) for generating the csv file, i've make a simple program for seeing the result :

String SAMPLE_CSV_FILE = "./sample.csv";

        try (
            BufferedWriter writer = Files.newBufferedWriter(Paths.get(SAMPLE_CSV_FILE));
            CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.EXCEL
                    .withHeader("ID", "Name", "Designation", "Company"));
        ) {
            csvPrinter.printRecord("1", "Name1 ", "CEO", "Google");
            csvPrinter.printRecord("2", "Name2", "CEO", "Microsoft");

            csvPrinter.flush();            
        }

My CSV is well generated but the header and the data isnt separated in each column, When I open the CSV file, I see all the data and header in the first column

I dont found yet the good CSVFormat, how to separate them ?


Solution

  • When I run your program, I get all the columns separated by comma's as expected. If you're using Excel to open the file, make sure you have selected the comma as the delimiter instead of the tab.