Java 无法将.csv文件转换为.xls格式
我正在使用此代码将CSV文件转换为Excel。但我在转换后的excel文件中只得到了两列记录。可以对此代码进行哪些更改Java 无法将.csv文件转换为.xls格式,java,csv,export-to-excel,Java,Csv,Export To Excel,我正在使用此代码将CSV文件转换为Excel。但我在转换后的excel文件中只得到了两列记录。可以对此代码进行哪些更改 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(
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
}
publicstaticvoidmain(字符串[]args)引发异常{
/*步骤1:读取Java中的输入CSV文件*/
字符串inputCSVFile=“csv_2_xls.csv”;
CSVReader reader=新CSVReader(新文件读取器(inputCSVFile));
/*要在CSV文件中循环的变量*/
字符串[]nextLine;/*用于文件中的每一行*/
int lnNum=0;/*行号*/
/*步骤2:定义POI电子表格对象*/
HSSFWorkbook new_工作簿=新建HSSFWorkbook();//创建一个空白工作簿对象
HSSFSheet sheet=new_workbook.createSheet(“CSV2XLS”);//创建一个带有标题分数_详细信息的工作表
/*步骤3:定义逻辑映射以将CSV文件数据使用到excel中*/
Map excel_data=new HashMap();//创建映射并定义数据
/*步骤4:将数据填充到逻辑映射中*/
而((nextLine=reader.readNext())!=null){
lnNum++;
excel_data.put(Integer.toString(lnNum),新对象[]{nextLine[0],nextLine[1]});
}
/*步骤5:使用POI从地图创建Excel数据*/
Set keyset=excel_data.keyset();
int rownum=0;
for(String key:keyset){//循环遍历数据并将它们添加到单元格中
Row-Row=sheet.createRow(rownum++);
Object[]objArr=excel\u data.get(key);
int-cellnum=0;
用于(对象对象对象:对象对象对象){
Cell Cell=row.createCell(cellnum++);
if(双精度的obj实例)
cell.setCellValue((双)obj);
其他的
cell.setCellValue((字符串)obj);
}
}
/*将XLS转换的CSV文件写入输出文件*/
FileOutputStream output_file=newfileoutputstream(新文件(“CSV2XLS.xls”);//创建xls文件
新建工作簿。写入(输出文件);//将转换后的XLS文件写入输出流
output_file.close();//关闭文件
}
这是因为您只添加了两列值
excel_data.put(Integer.toString(lnNum), new Object[] {nextLine[0],nextLine[1]});
1.不需要有对象[]
-您可以声明它持有CSVReader.readNext返回的字符串[]
excel_data.put(Integer.toString(lnNum), nextLine);
2.正如@gagan singh在评论中正确指出的,使用HashMap将丢失插入顺序。从CSV读取数据时,可以直接创建excel单元格值。如果仍要使用映射,请使用LinkedHashMap
,因为这样可以保留插入顺序
另外,我看到您在创建excel单元格时正在检查Double的obj instanceof
。CSVParser将所有值视为一个字符串,因此这不可能是真的。代码可以简化为如下内容
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);
}
}
为什么要用线号作为键呢。您将失去订单。在阅读csv阅读器时创建excel。您不必将数据复制到集合中,然后在单独的循环中创建excel。@gagansingh Yes。抢手货我已经更新了答案