Apache POI throwing IOException when reading XLSX workbook

I'm trying to get the following code to run and am getting an `IOException`: String cellText = null; InputStream is = null; try { // Find /mydata/myworkbook.xlsx is = new FileInputStream("/mydata/myworkbook.xlsx"); is.close(); System.out.println("Found the file!"); // Read it in as a workbook and then obtain the "widgets" sheet. Workbook wb = new XSSFWorkbook(is); Sheet sheet = wb.getSheet("widgets"); System.out.println("Obtained the widgets sheet!"); // Grab the 2nd row in the sheet (that contains the data we want). Row row = sheet.getRow(1); // Grab the 7th cell/col in the row (containing the Plot 500 English Description). Cell cell = row.getCell(6); cellText = cell.getStringCellValue(); System.out.println("Cell text is: " + cellText); } catch(Throwable throwable) { System.err.println(throwable.getMessage()); } finally { if(is != null) { try { is.close(); } catch(IOException ioexc) { ioexc.printStackTrace(); } } } The output from running this in Eclipse is: Found the file! Stream Closed java.io.IOException: Stream Closed at java.io.FileInputStream.readBytes(Native Method) at java.io.FileInputStream.read(FileInputStream.java:236) at java.io.FilterInputStream.read(FilterInputStream.java:133) at java.io.PushbackInputStream.read(PushbackInputStream.java:186) at java.util.zip.ZipInputStream.readFully(ZipInputStream.java:414) at java.util.zip.ZipInputStream.readLOC(ZipInputStream.java:247) at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:91) at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.(ZipInputStreamZipEntrySource.java:51) at org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:83) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:267) at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:204) at me.myorg.MyAppRunner.run(MyAppRunner.java:39) at me.myorg.MyAppRunner.main(MyAppRunner.java:25) The exception is coming from the line: Workbook wb = new XSSFWorkbook(is); According to the [XSSFWorkbook Java Docs](http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFWorkbook.html) this is a valid constructor for an `XSSFWorkbook` object, and I don't see anything "jumping out" at me to indicate that I'm using my `InputStream` incorrectly. Can any POI gurus help spot where I'm going awrye? Thanks in advance.

以上就是Apache POI throwing IOException when reading XLSX workbook的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准