java poi解析word的方法
之前做过用java读取word文档,获取word文本内容。
但发现docx的支持,doc就异常了。
后来找了很多资料发现是解析方法不一样。
首先要导入poi相关的jar包
我用的是maven,pom.xml引入如下:
org.apache.poi poi-ooxml 3.8 org.apache.poi poi-scratchpad 3.8
java获取word文本内容如下:
publicBaseRespgetParsedTxt(MultipartFilefile)throwsException{
BaseRespbr=newBaseResp("200","");
StringtextType=file.getContentType();
Stringtxt="";
if(textType.equals(TXT_TYPE)){
Stringcode=getCharset(file);
txt=newString(file.getBytes(),code);
}elseif(textType.equals(DOC_TYPE)){
HWPFDocumentdoc=newHWPFDocument(file.getInputStream());
Rangerang=doc.getRange();
txt=rang.text();
System.out.println(txt);
}elseif(textType.equals(DOCX_TYPE)){
FileuFile=newFile("tempFile.docx");
if(!uFile.exists()){
uFile.createNewFile();
}
FileCopyUtils.copy(file.getBytes(),uFile);
OPCPackageopcPackage=POIXMLDocument.openPackage("tempFile.docx");
POIXMLTextExtractorextractor=newXWPFWordExtractor(opcPackage);
txt=extractor.getText();
uFile.delete();
}else{
br=newBaseResp("300","上传文件格式错误,请上传.txt或者.docx");
returnbr;
}
br.setDatas(txt);
returnbr;
}
功能实现了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短