之前做過用java讀取word文檔,獲取word文本內(nèi)容。

從策劃到設(shè)計制作,每一步都追求做到細(xì)膩,制作可持續(xù)發(fā)展的企業(yè)網(wǎng)站。為客戶提供成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、網(wǎng)站策劃、網(wǎng)頁設(shè)計、域名與空間、網(wǎng)頁空間、網(wǎng)絡(luò)營銷、VI設(shè)計、 網(wǎng)站改版、漏洞修補等服務(wù)。為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,以客戶的口碑塑造優(yōu)易品牌,攜手廣大客戶,共同發(fā)展進(jìn)步。
但發(fā)現(xiàn)docx的支持,doc就異常了。
后來找了很多資料發(fā)現(xiàn)是解析方法不一樣。
首先要導(dǎo)入poi相關(guān)的jar包
我用的是maven,pom.xml引入如下:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.8</version>
</dependency>
java獲取word文本內(nèi)容如下:
public BaseResp getParsedTxt(MultipartFile file) throws Exception {
BaseResp br=new BaseResp("200","") ;
String textType = file.getContentType();
String txt = "";
if(textType.equals(TXT_TYPE)){
String code = getCharset(file);
txt = new String(file.getBytes(),code);
}else if(textType.equals(DOC_TYPE)){
HWPFDocument doc = new HWPFDocument(file.getInputStream());
Range rang = doc.getRange();
txt = rang.text();
System.out.println(txt);
}else if(textType.equals(DOCX_TYPE)){
File uFile = new File("tempFile.docx");
if(!uFile.exists()){
uFile.createNewFile();
}
FileCopyUtils.copy(file.getBytes(), uFile);
OPCPackage opcPackage = POIXMLDocument.openPackage("tempFile.docx");
POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
txt= extractor.getText();
uFile.delete();
}else{
br = new BaseResp("300","上傳文件格式錯誤,請上傳.txt或者.docx");
return br;
}
br.setDatas(txt);
return br;
}
功能實現(xiàn)了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
分享標(biāo)題:javapoi解析word的方法
文章分享:http://chinadenli.net/article46/jhjdeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)頁設(shè)計公司、靜態(tài)網(wǎng)站、動態(tài)網(wǎng)站、小程序開發(fā)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)