解析docx报错The supplied data appears to be in the OLE2 Format. You are calling the part

在解析docx格式的word文档的时候突然解析报错The supplied data appears to be in the OLE2 Format. You are calling the part

网上怎么搜索查询,查到的都是Excel相关的,根本和我遇到的情况不搭边

一:解析说明

doc格式和docx格式的解析方式是不一样的

doc格式解析

WordExtractor ex = new WordExtractor(file.getInputStream());
String text = ex.getText();
ex.close();

docx格式解析

XWPFDocument document = new XWPFDocument(file.getInputStream());
XWPFWordExtractor extractor = new XWPFWordExtractor(document);
String text = extractor.getText();

二:报错原因

解析到的文件后缀确实是docx,但是该文件因为版本太低或者由doc强行修改过后缀名为docx,此时使用docx的解析就会出现报错

三:解决

目前没发现什么好解决方式,暂时try-cache了异常,如果异常是该异常,重新使用doc解析方式进行解析,如果有好的解决方案可以推荐一下

try{
    
    
//解析docx
} catch (Exception e){
    
    
	e.printStackTrace();
	if (e instanceof OLE2NotOfficeXmlFileException){
    
    
    	return readWord(file,".doc");
    }
}

猜你喜欢

转载自blog.csdn.net/Ellis_li/article/details/131222006
今日推荐