简体   繁体   English

无法将.csv文件转换为.xls格式

[英]unable to convert .csv file into .xls format

I am using this code to convert CSV file into Excel. 我正在使用此代码将CSV文件转换为Excel。 But I am getting records of only 2 columns in my converted excel file. 但是我在转换后的excel文件中仅获得2列的记录。 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 1.不需要Object[] -您可以声明它保存CSVReader.readNext返回的String[]

 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. 2.正如@gagan singh在评论中正确指出的那样,使用HashMap将丢失插入顺序。 You can directly create excel cell values as you read from the CSV. 从CSV读取时,您可以直接创建excel单元格值。 If you still want to use a map, use a LinkedHashMap as that preserves the insertion order. 如果仍要使用地图,请使用LinkedHashMap因为它可以保留插入顺序。

Also, I see that you are checking obj instanceof Double when creating the excel cell. 另外,我看到您在创建excel单元格时正在检查obj instanceof Double CSVParser treats all the values as a String and hence this cannot be true. CSVParser将所有值都视为String,因此这不能成立。

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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