POI判断excel文件版本

1 通过POIFSFileSystem.hasPOIFSHeader(InputStream is);判断Excel 2003及以下

2通过POIXMLDocument.hasOOXMLHeader(InputStream is);判断Excel 2007及以上

这种判断,即使将excel文件后缀变换,也会正确识别,比如将.xlsx人为换成xls导入,还是能识别出为2007以上版本。

注意:传入的InputStream用BufferedInputStream装饰一层,如果直接传入InputStream,如果流不支持mark/reset机制,会抛出java.io.IOException: mark/reset not supported

最后附上示例:

public static void main(String[] args)throws Exception {
        File file = new File("D:\\docs\\work\\需求排期安排.xlsx");
        InputStream is = new FileInputStream(file);
        //这里用BufferedInputStream再包装一层,可解决:mark/reset not supported问题
        BufferedInputStream bis = new BufferedInputStream(is);
        if(POIFSFileSystem.hasPOIFSHeader(bis)) {
            System.out.println("2003及以下");
        }
        if(POIXMLDocument.hasOOXMLHeader(bis)) {
            System.out.println("2007及以上");
        }
    }

猜你喜欢

转载自www.cnblogs.com/ixixi/p/12180376.html
今日推荐