今天遇到一个有趣的问题,不报任何异常, 程序跑完了,居然没往excel里写数据
后来 将SXSSFWorkbook改为XSSFWorkbook才成功!!!
/**
* 向已存在的excel中追加数据
*
* @param absolutePath 已存在的excel绝对路径
* @param sheetName sheet的序号,从0开始
* @param dataList cell数据
* @return
* @throws IOException
*/
public static Boolean addExcel(String absolutePath, String sheetName, List<String[]> dataList) throws IOException {
XSSFWorkbook wb = null;
FileOutputStream out = null;
FileInputStream fileInputStream = null;
try{
int columnsNum = dataList.get(0).length;
fileInputStream = new FileInputStream(absolutePath);
wb = new XSSFWorkbook();
wb = (XSSFWorkbook) WorkbookFactory.create(fileInputStream);
Sheet sheet =wb.getSheet(sheetName);
out = new FileOutputStream(absolutePath);
Row row;
int lastRowNum = sheet.getLastRowNum();
for (int i = 0; i < dataList.size(); i++) {
//row = sheet.createRow(++lastRowNum);
row = sheet.getRow(++lastRowNum);
if (row == null) {
row = sheet.createRow(lastRowNum);
}
String[] addOneRowData = dataList.get(i);
for (int j = 0; j < addOneRowData.length; j++) {
String str = addOneRowData[j];
row.createCell(j).setCellValue(str);
}
}
out.flush();
wb.write(out);
}catch (Exception e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
fileInputStream.close();
}
if (wb != null) {
//wb.dispose();// 删除临时文件,很重要,否则磁盘可能会被写满
}
try {
if(out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(wb != null) {
wb.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
那他们的区别