Category Archives: apache-commons-csv

Apache commons csv

I can't understand the core of apache commons csv library. Could you advice a method I should use when I want to get columns by header name from csv file by apache-commons-csv? For example, I have such csv file:

Delays, Computation time, Communication time
0., 500000, 10
20., 563210, 20
55., 546541., 23.
12., 465487., 23.
45., 456547., 28.
87., 985458., 56.
47., 456778., 45.

I want to get arrays: double[] delays, double[] compTime, double[] commTime consist of values from csv.

Java : Apache commons CSV file, all data in one column in MS office, Libre office is fine

I am working on a project, in which I am getting some data from database, and inserting it into a CSV file. For this I have Apache Commons CSV in the POM.

When I output the file, I can see all the sorting between different columns in LibreOffice, but when I open the file with Microsoft office, it just puts all the data in first column(0th). I was actually not using Apache commons CSV, as I was able to directly write data in file using FileWriter, and save it. That was also fine with LibreOffice, but MS office didn't recognize special characters... Drives me nuts...

So, any ideas what is going wrong? Thank you.

Code :

 @Override
    public byte[] createCsvFileOutOfCanvas(int canvasId) {
        String str = new BigInteger(130, random).toString(32);

        List<GroupSection> groupSectionList = this.groupSectionService.listGroupSectionByCanvasid(canvasId, true);
        try {
            FileWriter writer = new FileWriter("/home/deploy/csv/" + str + ".csv");
   NEW_LINE_SEPERATOR = "\n"
            CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);

            CSVPrinter csvFilePrinter = new CSVPrinter(writer,csvFileFormat);

            for (GroupSection groupSection : groupSectionList) {
                List sectionRecord = new ArrayList<>();
                sectionRecord.add("Bereich, ");
                sectionRecord.add(groupSection.getMsectionname());
                csvFilePrinter.printRecord(sectionRecord);
                csvFilePrinter.println();

                List textRecord = new ArrayList<>();
                textRecord.add("Nummer, ");
                textRecord.add("Überschrift, ");
                textRecord.add("Beschreibung, ");
                textRecord.add("Erstelldatum, ");
                textRecord.add("Startdatum, ");
                textRecord.add("Enddatum, ");
                textRecord.add("Verantwortlich, ");
                csvFilePrinter.printRecord(textRecord);
                csvFilePrinter.println();

                List<GroupNotes> groupNotesList = this.groupNotesDAO.listGroupNotesBySectionId(groupSection.getMsectionid());
                for (GroupNotes groupNotes : groupNotesList) {
                    List noteRecord = new ArrayList<>();
                    noteRecord.add(String.valueOf(groupNotes.getNoteNumber())+",");
                    if (!(groupNotes.getMnotetag() == null)) {
                        noteRecord.add("\""+groupNotes.getMnotetag()+"\",");
                    }
                    if (!(groupNotes.getMnotetext() == null)) {
                        String noteText = Jsoup.parse(groupNotes.getMnotetext()).text();
                        noteRecord.add("\""+noteText+"\",");
                    }
                    if (!(groupNotes.getNoteCreationTime() == null)) {
                        noteRecord.add(new SimpleDateFormat("MM/dd/yyyy HH:mm").format(groupNotes.getNoteCreationTime())+",");
                    }
                    if (!(groupNotes.getStartDateTimestamp() == null)) {
                        noteRecord.add(new SimpleDateFormat("MM/dd/yyyy HH:mm").format(groupNotes.getStartDateTimestamp())+",");
                    }
                    if (!(groupNotes.getNoteTarget() == null)) {
                        noteRecord.add(new SimpleDateFormat("MM/dd/yyyy HH:mm").format(groupNotes.getNoteTarget())+",");
                    }
                    if (!(groupNotes.getZugwisenPersonId() == 0)) {
                        noteRecord.add(this.personService.getPersonById(groupNotes.getZugwisenPersonId()).getFirstName()+",");
                    }

                    csvFilePrinter.printRecord(noteRecord);
                }
                csvFilePrinter.println();
            }

            writer.flush();
            writer.close();
            Path path = Paths.get("/home/deploy/csv/" + str + ".csv");
            return Files.readAllBytes(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new byte[0];
    }

As an example CSV, I am uploading the document in Public Dropbox here . Kindly have a look. Thank you. :-)