Question

I am using opencsv for getting values out of multiple csv sheets to write them into one csv sheets. This is what I am doing:

//reading all entries in a huge list
for (int j = 0; j < (fileList.size() - 740); j++) { 
    String csvFile = "C:\\" + fileList.get(j);
    reader = new CSVReader(new FileReader(csvFile), ';');
    hugeList = reader.readAll();            
    
    List<String[]> data = new ArrayList<String[]>();
    List<String> tmp= new ArrayList<String>();
     for(int m = 0; m < hugeList.size(); m++) {
              String[] values = hugeList.get(m);
              tmp.add(values[0]);       
       }
     data.add(tmp.toArray(new String[0]));


    writer.writeAll(data);
    
}

As you can see I am getting the file and write its content into a list(hugeList) and then mapping each value on a new data array which I am writing into my new sheet. The problem is I am getting the data in a row and not in a column: enter image description here

How to write my data column-by-column? What is wrong in my algorithm?

I appreciate your reply!

Was it helpful?

Solution

What is wrong here is very simple: you must write a every entry as new line

E.g.:   writer.writeNext(data);

See example below for more details

My Approach for a result in Column

 for (int j = 0; j < fileList.size(); j++) {
    String csvFile = readPath + fileList.get(j);
    System.out.println("Read:  " + csvFile);
    reader = new CSVReader(new FileReader(csvFile), ';');
    hugeList = reader.readAll();

    String[] data = new String[1];
    for (int m = 0; m < hugeList.size(); m++) {
        String[] values = hugeList.get(m);
        data[0] = values[0];
        writer.writeNext(data);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top