简体   繁体   中英

OpenCSV writing a CSV file using multiple arrays

I have 7 arrays and I want each array to be a column in a CSV file. I'm currently using OpenCSV. But, I haven't figured out a way to generate a CSV file from multiple arrays.

String[] Title = new String[1000];
String[] Starring = new String[1000];
String[] Director = new String[1000];
CSVWriter writer = new CSVWriter(new FileWriter("export.csv"), ',');
String[] entries = Title[1] + Starring[1].split('#');
writer.writeNext(entries);

Assuming you have a 2D array of String , here's what you can do:

CSVWriter writer = //get csv writer
String[][] lines = //lines
for(String[] lines : lines){
    writer.writeNext(lines);
}

The writeNext method in openCSV takes an array of String with each element being the value if you have separate arrays you have to build the single array to feed the writeNext. I am not sure if this will work but its worth a shot.

private String[] createRowFromSeparateArrays(String... columns) {
   return columns;
}

And then in your code you would have

for (int i = 0; i < Title.length; i++) {
 writer.writeNext(createRowFromSeparateArrays(Title[i],Starring[i],Director[i]));
}

good luck :)

You first need to create parallel arrays and cast them into String then Parse the actual array toString. GOOD LUCK

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM