在导入Excel的时候,尽量能判断导入Excel的版本,调用不同的方法。
HSSFWorkbook 对应xls 版本
XSSFWorkbook 对应xlsx 版本
Workbook wb = WorkbookFactory.create(is);//自动兼容版本
/** * Creates the appropriate HSSFWorkbook / XSSFWorkbook from * the given InputStream. * Your input stream MUST either support mark/reset, or * be wrapped as a {@link PushbackInputStream}! */ public static Workbook create(InputStream inp) throws IOException, InvalidFormatException { // If clearly doesn't do mark/reset, wrap up if(! inp.markSupported()) { inp = new PushbackInputStream(inp, 8); } if(POIFSFileSystem.hasPOIFSHeader(inp)) { return new HSSFWorkbook(inp); } if(POIXMLDocument.hasOOXMLHeader(inp)) { return new XSSFWorkbook(OPCPackage.open(inp)); } throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream"); }
其他操作一致 自行百度