Excel POI当某些行已经存在时意外缺less行
有人已经看到这个错误?
我只是在创buildHSSFWorkbook时才得到它
try { LOGGER.info("Open Excel file: " + filename); InputStream inputStream = new FileInputStream(filename); Workbook wb = new HSSFWorkbook(inputStream); Sheet sheet = wb.getSheetAt(0); /* save excel */ FileOutputStream fileOut = new FileOutputStream(filenameOutput); wb.write(fileOut); fileOut.close(); wb.close(); inputStream.close(); } catch (IOException e1) { LOGGER.log(Level.SEVERE, e1.getMessage(), e1); }
错误是在new HSSFWorkbook(inputstream)
Exception in thread "main" java.lang.RuntimeException: Unexpected missing row when some rows already present at org.apache.poi.hssf.usermodel.HSSFSheet.setPropertiesFromSheet(HSSFSheet.java:212) at org.apache.poi.hssf.usermodel.HSSFSheet.<init>(HSSFSheet.java:137) at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:338) at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:289) at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:224) at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:382) at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:364)
任何想法 ?
我已经看到http://apache-poi.1045710.n5.nabble.com/Unexpected-missing-row-when-some-rows-already-present-td5527417.html这个问题,但他们没有给出好的答案
我使用POI 3.12
更新:我的Excel包含53行和数百列,所以他不是空的。 excel是用Buisness Object生成的, 有些人没有解决scheme也有同样的问题 。
HSSFSheet的源代码在这里
好的,我find了解决scheme。
升级到3.14解决问题。
3.13和之前的例外是由这个代码生成的
RowRecord row = sheet.getNextRow(); boolean rowRecordsAlreadyPresent = row != null; 207 if (hrow == null) { 208 // Some tools (like Perl module Spreadsheet::WriteExcel - bug 41187) skip the RowRecords 209 // Excel, OpenOffice.org and GoogleDocs are all OK with this, so POI should be too. 210 if (rowRecordsAlreadyPresent) { 211 // if at least one row record is present, all should be present. 212 throw new RuntimeException("Unexpected missing row when some rows already present"); 213 }
但在3.14 他们只是评论它
if (hrow == null) { /* we removed this check, see bug 47245 for the discussion around this // Some tools (like Perl module Spreadsheet::WriteExcel - bug 41187) skip the RowRecords // Excel, OpenOffice.org and GoogleDocs are all OK with this, so POI should be too. if (rowRecordsAlreadyPresent) { // if at least one row record is present, all should be present. throw new RuntimeException("Unexpected missing row when some rows already present"); }*/ // create the row record on the fly now. RowRecord rowRec = new RowRecord(cval.getRow()); sheet.addRow(rowRec); hrow = createRowFromRecord(rowRec); }
正如他们所说的,有些工具跳过了RowRec,在我的情况下,当我用Buisness对象生成我的.xls
例:
Workbook workbook = null; try { workbook = WorkbookFactory.create(is); } catch (IOException e) { e.printStackTrace(); } catch (InvalidFormatException e) { e.printStackTrace(); } Sheet sheet = workbook.getSheetAt(0);
XSSF或HSSF无关紧要,都应该运作良好。