如果POI-3.10往一個工作表(sheet)里面插入數據的話,需要注意了,其有一個不太被容易發現的bug。 被插入的工作表(sheet)里面的單元格沒有包含任何的注解(comment)的時候,插入一行數據,不會有任何問題。但是如果被插入的工作表(sheet)里面的單元格只要包含任何的注解(comment)的時候,這個時候插入一行數據的時候,就會破壞這個文件。當程序執行完后,如果打開被插入數據的Excel文件,我們將會發現,其會彈出下面的對話框。
程序代碼如下,
?
- package?com.tibco.poi.xssf;??
- ??
- import?java.io.File;??
- import?java.io.FileInputStream;??
- import?java.io.FileNotFoundException;??
- import?java.io.FileOutputStream;??
- import?java.io.IOException;??
- ??
- import?org.apache.poi.xssf.usermodel.XSSFCell;??
- import?org.apache.poi.xssf.usermodel.XSSFRow;??
- import?org.apache.poi.xssf.usermodel.XSSFSheet;??
- import?org.apache.poi.xssf.usermodel.XSSFWorkbook;??
- ??
- public?class?CreatRowTest?{??
- ????//當前文件已經存在??
- ????private?String?excelPath?=?"D:\\exceltest\\comments.xlsx";??
- ????//從第幾行插入進去??
- ????private?int?insertStartPointer?=?3;??
- ????//在當前工作薄的那個工作表單中插入這行數據???
- ????private?String?sheetName?=?"Sheet1";??
- ??
- ????/**?
- ?????*?總的入口方法?
- ?????*/??
- ????public?static?void?main(String[]?args)?{??
- ????????CreatRowTest?crt?=?new?CreatRowTest();??
- ????????crt.insertRows();??
- ????}??
- ????/**?
- ?????*?在已有的Excel文件中插入一行新的數據的入口方法?
- ?????*/??
- ????public?void?insertRows()?{??
- ????????XSSFWorkbook?wb?=?returnWorkBookGivenFileHandle();??
- ????????XSSFSheet?sheet1?=?wb.getSheet(sheetName);??
- ????????XSSFRow?row?=?createRow(sheet1,?insertStartPointer);??
- ????????createCell(row);??
- ????????saveExcel(wb);??
- ??
- ????}??
- ????/**?
- ?????*?保存工作薄?
- ?????*?@param?wb?
- ?????*/??
- ????private?void?saveExcel(XSSFWorkbook?wb)?{??
- ????????FileOutputStream?fileOut;??
- ????????try?{??
- ????????????fileOut?=?new?FileOutputStream(excelPath);??
- ????????????wb.write(fileOut);??
- ????????????fileOut.close();??
- ????????}?catch?(FileNotFoundException?e)?{??
- ????????????e.printStackTrace();??
- ????????}?catch?(IOException?e)?{??
- ????????????e.printStackTrace();??
- ????????}??
- ??
- ????}??
- ????/**?
- ?????*?創建要出入的行中單元格?
- ?????*?@param?row?
- ?????*?@return?
- ?????*/??
- ????private?XSSFCell?createCell(XSSFRow?row)?{??
- ????????XSSFCell?cell?=?row.createCell((short)?0);??
- ????????cell.setCellValue(999999);??
- ????????row.createCell(1).setCellValue(1.2);??
- ????????row.createCell(2).setCellValue("This?is?a?string?cell");??
- ????????return?cell;??
- ????}??
- ???/**?
- ????*?得到一個已有的工作薄的POI對象?
- ????*?@return?
- ????*/??
- ????private?XSSFWorkbook?returnWorkBookGivenFileHandle()?{??
- ????????XSSFWorkbook?wb?=?null;??
- ????????FileInputStream?fis?=?null;??
- ????????File?f?=?new?File(excelPath);??
- ????????try?{??
- ????????????if?(f?!=?null)?{??
- ????????????????fis?=?new?FileInputStream(f);??
- ????????????????wb?=?new?XSSFWorkbook(fis);??
- ????????????}??
- ????????}?catch?(Exception?e)?{??
- ????????????return?null;??
- ????????}?finally?{??
- ????????????if?(fis?!=?null)?{??
- ????????????????try?{??
- ????????????????????fis.close();??
- ????????????????}?catch?(IOException?e)?{??
- ????????????????????e.printStackTrace();??
- ????????????????}??
- ????????????}??
- ????????}??
- ????????return?wb;??
- ????}??
- ???/**?
- ????*?找到需要插入的行數,并新建一個POI的row對象?
- ????*?@param?sheet?
- ????*?@param?rowIndex?
- ????*?@return?
- ????*/??
- ????private?XSSFRow?createRow(XSSFSheet?sheet,?Integer?rowIndex)?{??
- ????????XSSFRow?row?=?null;??
- ????????if?(sheet.getRow(rowIndex)?!=?null)?{??
- ????????????int?lastRowNo?=?sheet.getLastRowNum();??
- ????????????sheet.shiftRows(rowIndex,?lastRowNo,?1);??
- ????????}??
- ????????row?=?sheet.createRow(rowIndex);??
- ????????return?row;??
- ????}??
- ??
- ??????
- ??
- }??
不過,值得高興的是,這個bug已經在POI-3.12的版本解決了