NPO兼容xlsx 和xls 微软和金山都可用

 在导入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");
    }

 其他操作一致  自行百度

posted @ 2021-02-08 09:47  陌陌逸帆  阅读(110)  评论(0编辑  收藏  举报