首页 > 其他 > 详细

POI判断excel文件版本

时间:2020-01-11 17:52:59      阅读:112      评论:0      收藏:0      [点我收藏+]

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及以上");
        }
    }

 

POI判断excel文件版本

原文:https://www.cnblogs.com/ixixi/p/12180376.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!