使用Apache POI将CSV转换为XLS / XLSX?
我需要在项目中将csv转换为xls / xlsx吗?我怎样才能做到这一点?谁能给我发一些例子?我想用Apache
poi做到这一点。我还需要从Java端创建一个单元格。
回答:
您可以尝试以下方法使用apache-poi创建xlsx文件。
public static void csvToXLSX() { try {
String csvFileAddress = "test.csv"; //csv file address
String xlsxFileAddress = "test.xlsx"; //xlsx file address
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet("sheet1");
String currentLine=null;
int RowNum=0;
BufferedReader br = new BufferedReader(new FileReader(csvFileAddress));
while ((currentLine = br.readLine()) != null) {
String str[] = currentLine.split(",");
RowNum++;
XSSFRow currentRow=sheet.createRow(RowNum);
for(int i=0;i<str.length;i++){
currentRow.createCell(i).setCellValue(str[i]);
}
}
FileOutputStream fileOutputStream = new FileOutputStream(xlsxFileAddress);
workBook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("Done");
} catch (Exception ex) {
System.out.println(ex.getMessage()+"Exception in try");
}
}
以上是 使用Apache POI将CSV转换为XLS / XLSX? 的全部内容, 来源链接: utcz.com/qa/422439.html