I am using this code to convert CSV file into Excel. But I am getting records of only 2 columns in my converted excel file. What changes could be made to this code..
public static void main(String[] args) throws Exception {
/* Step -1 : Read input CSV file in Java */
String inputCSVFile = "csv_2_xls.csv";
CSVReader reader = new CSVReader(new FileReader(inputCSVFile));
/* Variables to loop through the CSV File */
String [] nextLine; /* for every line in the file */
int lnNum = 0; /* line number */
/* Step -2 : Define POI Spreadsheet objects */
HSSFWorkbook new_workbook = new HSSFWorkbook(); //create a blank workbook object
HSSFSheet sheet = new_workbook.createSheet("CSV2XLS"); //create a worksheet with caption score_details
/* Step -3: Define logical Map to consume CSV file data into excel */
Map<String, Object[]> excel_data = new HashMap<String, Object[]>(); //create a map and define data
/* Step -4: Populate data into logical Map */
while ((nextLine = reader.readNext()) != null) {
lnNum++;
excel_data.put(Integer.toString(lnNum), new Object[] {nextLine[0],nextLine[1]});
}
/* Step -5: Create Excel Data from the map using POI */
Set<String> keyset = excel_data.keySet();
int rownum = 0;
for (String key : keyset) { //loop through the data and add them to the cell
Row row = sheet.createRow(rownum++);
Object [] objArr = excel_data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
Cell cell = row.createCell(cellnum++);
if(obj instanceof Double)
cell.setCellValue((Double)obj);
else
cell.setCellValue((String)obj);
}
}
/* Write XLS converted CSV file to the output file */
FileOutputStream output_file = new FileOutputStream(new File("CSV2XLS.xls")); //create XLS file
new_workbook.write(output_file);//write converted XLS file to output stream
output_file.close(); //close the file
}
It is because you are only adding two column values
excel_data.put(Integer.toString(lnNum), new Object[] {nextLine[0],nextLine[1]});
1.There is no need to have Object[]
- you can declare it to hold String[]
as returned by CSVReader.readNext
excel_data.put(Integer.toString(lnNum), nextLine);
2.As rightly pointed out by @gagan singh in the comment, using a HashMap will lose the insertion order. You can directly create excel cell values as you read from the CSV. If you still want to use a map, use a LinkedHashMap
as that preserves the insertion order.
Also, I see that you are checking obj instanceof Double
when creating the excel cell. CSVParser treats all the values as a String and hence this cannot be true.
code can be simplified to something like this.
String inputCSVFile = "csv_2_xls.csv";
CSVReader reader = new CSVReader(new FileReader(inputCSVFile));
HSSFWorkbook new_workbook = new HSSFWorkbook();
HSSFSheet sheet = new_workbook.createSheet("CSV2XLS");
int rownum = 0;
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
Row row = sheet.createRow(rownum++);
int cellnum = 0;
for (String value : nextLine) {
Cell cell = row.createCell(cellnum++);
cell.setCellValue(value);
}
}
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.