打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
POI 使用 HWPFDocument 和 XWPFDocument 分割 word2003 和 word2010示例
[摘要:本身正在做项目过程当中用到懂得析支解word2003战2007,2010等文件内容,以下是代码: 上面是局部代码,若是念要全体代码能够收我邮箱,yongqian.liu@peraglobal.com, 接心类 :PoiExtractContent.java]
自己在做项目过程中用到了解析分割word2003和2007,2010等文件内容,以下是代码:
下面是部分代码,如果想要全部代码可以发我邮箱,yongqian.liu@peraglobal.com,
接口类 :PoiExtractContent.java
package com.peraglobal.extract.poi;
import java.util.Map;
/**
* 使用 POI 解析 WORD 文件的内容信息
* @author yongqian.liu
* 2015-2-9
*/
public interface PoiExtractContent<T> {
/**
* 根据文件路径获得 Document 对象
* @param docPath 路径
* @return Document
*/
public T getDocument(String docPath);
/**
* 解析 word 文档的标题
* @param doc Document 对象
* @return word 文档中标题
*/
public String getTilte(T doc);
/**
* 获取 word 文档里所有文字内容(不包括图片、表格等格式的内容)
* @param doc Document 对象
* @return word 文档中文字部分全部内容
*/
public String getContent(T doc);
/**
* 获取 word 文档里所有文字内容(不包括图片、表格等格式的内容)
* @param docPath doc 对象路径
* @return word 文档中文字部分全部内容
*/
public String getContent(String docPath);
/**
* 获取 word 文档里面所有图片并另存到指定目录下
* @param doc Document 对象
* @param picPath 保存图片路径
* @param suffix 后缀名
*/
public void getPictures(T doc, String picPath, String suffix);
/**
* 获取word 文档里面所有表格
* @param doc Document 对象
*/
public void getTables(T doc);
/**
* 获取word 文档中最大的字体
* @param doc Document 对象
* @return 最大字体
*/
public int getMaxFontSize(T doc);
/**
* 获取 word 文档的创建信息
* @param docPath doc路径
* @return 创建文档的信息
*/
public Map<String, String> getInfo(String docPath);
}
2003实现:PoiHwpfExtractContentImpl.java
package com.peraglobal.extract.poi.impl;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.model.PicturesTable;
import org.apache.poi.hwpf.model.StyleDescription;
import org.apache.poi.hwpf.model.StyleSheet;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
import com.peraglobal.extract.poi.PoiExtractContent;
import com.peraglobal.extract.util.Const;
import com.peraglobal.extract.util.FormatTextUtil;
/**
* 使用 POI 解析 DOC2003 文件的内容信息
* @author yongqian.liu
* 2015-2-9
*/
public class PoiHwpfExtractContentImpl implements PoiExtractContent<HWPFDocument> {
/**
* 根据文件路径获得 Document 对象
* @param docPath 路径
* @return Document
*/
public HWPFDocument getDocument(String docPath) {
// hwpfDocument 是专门处理 word 的,在 poi 中还有处理其他 office 文档的类
HWPFDocument doc = null;
try {
doc = new HWPFDocument(new FileInputStream(docPath));
} catch (Exception e) {
e.printStackTrace();
}
return doc;
}
/**
* 解析 word 文档的标题
* @param doc Document 对象
* @return 标题
*/
public String getTilte(HWPFDocument doc) {
String title = "\";
Range range = doc.getRange();
Paragraph p = null;
for (int i = 0; i < range.numParagraphs(); i++) {
p = range.getParagraph(i);
if(p.text() != null && !p.text().equals("") && !p.text().equals("r")){
title = p.text().trim();
break;
}
}
return title;
}
/**
* 获取 word 文档里所有文字内容(不包括图片、表格等格式的内容)
* @param doc Document 对象
* @return word 文档中文字部分全部内容
*/
public String getContent(HWPFDocument doc){
String content = "";
try {
content = doc.getText().toString().trim();
} catch (Exception e) {
e.printStackTrace();
}
return content.replaceAll("", "");
}
/**
* 获取 word 文档里所有文字内容(不包括图片、表格等格式的内容)
* @param docPath doc 对象路径
* @return word 文档中文字部分全部内容
*/
public String getContent(String docPath) {
StringBuffer strBuff = new StringBuffer("");
try {
WordExtractor extractor = new WordExtractor(new FileInputStream(docPath));
//extractor.getTextFromPieces();
String [] strArray = extractor.getParagraphText();
for(int i = 0; i < strArray.length; ++i) {
strBuff.append(strArray[i].trim());
}
} catch (Exception e) {
e.printStackTrace();
}
return strBuff.toString().replaceAll("", "");
}
/**
* 获取 word 文档里面所有图片并另存到指定目录下
* @param doc Document 对象
* @param picPath 保存图片路径
* @param suffix 后缀名
*/
public void getPictures(HWPFDocument doc, String picPath, String suffix) {
Range range = doc.getRange();
byte[] dataStream = doc.getDataStream();
int numChar = range.numCharacterRuns();
PicturesTable pTable = new PicturesTable(doc, dataStream, dataStream);
for (int i = 0; i < numChar; ++i) {
CharacterRun cuRun = range.getCharacterRun(i);
boolean hasPic = pTable.hasPicture(cuRun);
if (hasPic) {
Picture picture = pTable.extractPicture(cuRun, true);
try {
picture.writeImageContent(new FileOutputStream(picPath + i + suffix));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 获取word 文档里面所有表格
* @param doc Document 对象
*/
public void getTables(HWPFDocument doc){
Range range = doc.getRange();
TableIterator tableIt = new TableIterator(range);
while (tableIt.hasNext()) {
Table table = (Table)tableIt.next();
for(int j=0;j<table.numRows();j++){
TableRow tr = table.getRow(j);
String content = "";
for(int i=0;i<tr.numCells();i++){
TableCell cell = tr.getCell(i);
for(int m=0;m<cell.numParagraphs();m++){ //获取单元格内容
Paragraph para = cell.getParagraph(m);
content += para.text().trim() + ";";
}
}
System.out.println(content);
}
}
}
/**
* 获取文章中所有标题集合
* @param doc Document
* @return
*/
public List<String> getTitleList(HWPFDocument doc){
Range range = doc.getRange();
byte[] dataStream = doc.getDataStream();
int numP = range.numParagraphs();
List<String> titleList = new ArrayList<String>();
PicturesTable pTable = new PicturesTable(doc, dataStream, dataStream);
for(int i=0;i<numP;i++){
Range curRange = range.getParagraph(i);
Paragraph paragraph = range.getParagraph(i);
CharacterRun cr = curRange.getCharacterRun(0);
if(pTable.hasPicture(cr)){ //图片
continue;
}else{
char currentChar = 0;
for(int k=0;k<cr.text().length();k++){
currentChar = cr.text().charAt(k);
if(currentChar != Const.SPACE_ASCII){
break;
}
}
if(currentChar == Const.ENTER_ASCII){ //回车符
continue;
}else if(currentChar == Const.SPACE_ASCII){ //空格符
continue;
}else if(currentChar == Const.TABULATION_ASCII){ //水平制表符
continue;
}
}
int numStyles = doc.getStyleSheet().numStyles();
int styleIndex = paragraph.getStyleIndex();
if (numStyles > styleIndex) {
StyleSheet style_sheet = doc.getStyleSheet();
StyleDescription style = style_sheet.getStyleDescription(styleIndex);
String styleName = style.getName();
if(styleName!=null&&styleName.contains("标题")){
titleList.add(paragraph.text().trim());
System.out.println(paragraph.text().trim());
}
}
}
return titleList;
}
/**
* 获取整篇文章中所有标题样式名称
* @param doc Document
* @return
*/
public Set<String> getTitleStyleNameSet(HWPFDocument doc){
Range range = doc.getRange();
byte[] dataStream = doc.getDataStream();
int numP = range.numParagraphs();
Set<String> titNameSet = new HashSet<String>();
PicturesTable pTable = new PicturesTable(doc, dataStream, dataStream);
for(int i=0;i<numP;i++){
Range curRange = range.getParagraph(i);
Paragraph paragraph = range.getParagraph(i);
CharacterRun cr = curRange.getCharacterRun(0);
if(pTable.hasPicture(cr)){ //图片
continue;
}else{
char currentChar = 0;
for(int k=0;k<cr.text().length();k++){
currentChar = cr.text().charAt(k);
if(currentChar != Const.SPACE_ASCII){
break;
}
}
if(currentChar == Const.ENTER_ASCII){ //回车符
continue;
}else if(currentChar == Const.SPACE_ASCII){ //空格符
continue;
}else if(currentChar == Const.TABULATION_ASCII){ //水平制表符
continue;
}
}
int numStyles = doc.getStyleSheet().numStyles();
int styleIndex = paragraph.getStyleIndex();
if (numStyles > styleIndex) {
StyleSheet style_sheet = doc.getStyleSheet();
StyleDescription style = style_sheet.getStyleDescription(styleIndex);
String styleName = style.getName();
if(styleName!=null&&styleName.contains("标题")){
if(styleName.contains(",")){
styleName = getFirstStyleName(styleName);
}
titNameSet.add(styleName);
}
}
}
return titNameSet;
}
/**
* 处理标题样式名称的特殊格式,如:“标题 3,标题 3 Char,标题 3 Char Char” ,只获取“标题 3”
* @param styleName 需进行处理的标题样式 ,如"标题 3,标题 3 Char,标题 3 Char Char”
* @return
*/
private String getFirstStyleName(String styleName){
if ((styleName != null) && (styleName.length() > 0)) {
int styleLeng = styleName.split(",").length;
if(styleLeng>1){
int comma = styleName.indexOf(",");
if(comma>-1&&(comma<styleName.length())){
return styleName.substring(0,comma);
}
}
}
return styleName;
}
/**
* 获取当前文章中最大标题样式名称,如“标题1”
* @param doc Document
* @return
*/
public String getMaxTitleStyleName(HWPFDocument doc){
Set<String> titNameSet = getTitleStyleNameSet(doc);
Iterator<String> it = titNameSet.iterator();
List<Integer> tempLst = new ArrayList<Integer>();
while(it.hasNext()){
String titName = it.next(); //得到“标题 1”、“标题 2”
try {
int curStyleName = Integer.parseInt(titName.substring(2).trim());
tempLst.add(curStyleName);
} catch (NumberFormatException e) {
continue;
}
}
int max = (tempLst.size()==0?0:tempLst.get(0));
for(int i=0;i<tempLst.size();i++){
int curSize = tempLst.get(i);
if(curSize<max){
max = curSize;
}
}
if(max==0){
return ""; //文章中不包含任何标题
}
return "标题 "+max;
}
/**
* 获取word 文档中最大的字体
* @param doc Document 对象
*/
public int getMaxFontSize(HWPFDocument doc) {
int fontSize = 0;
try {
Range range = doc.getRange();
for (int i = 0; i < range.numParagraphs(); i++) {
Paragraph poiPara = range.getParagraph(i);
int j = 0;
while (true) {
CharacterRun run = poiPara.getCharacterRun(j++);
if(fontSize < run.getFontSize()) {
fontSize = run.getFontSize();
}//字体大小
if (run.getEndOffset() == poiPara.getEndOffset()) {
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return fontSize;
}
/**
* 获取 word 文档的创建信息
* @param docPath doc路径
* @return 创建文档的信息
*/
public Map<String, String> getInfo(String docPath) {
try {
InputStream is = new FileInputStream(docPath);
WordExtractor extractor = new WordExtractor(is);
SummaryInformation info = extractor.getSummaryInformation();
Map<String, String> mapInfo = new HashMap<String, String>();
mapInfo.put("author", info.getAuthor()); // 作者
mapInfo.put("title", info.getTitle()); // 标题
mapInfo.put("subject", info.getSubject()); // 主题
mapInfo.put("keyword", info.getKeywords()); // 关键词
mapInfo.put("createdate", FormatTextUtil.dateFormat(info.getCreateDateTime())); // 创建时间
mapInfo.put("updatedate", FormatTextUtil.dateFormat(info.getLastSaveDateTime())); // 修改时间
} catch (Exception e) {
}
return null;
}
}
2007实现类:PoiXwpfExtractContentImpl.java
package com.peraglobal.extract.poi.impl;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.POIXMLProperties.CoreProperties;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import com.peraglobal.extract.poi.PoiExtractContent;
import com.peraglobal.extract.util.FormatTextUtil;
/**
* 使用 POI 解析 DOCX2007 文件的内容信息
* @author yongqian.liu
* 2015-2-9
*/
public class PoiXwpfExtractContentImpl implements PoiExtractContent<XWPFDocument> {
/**
* 根据文件路径获得 Document 对象
* @param docxPath 路径
* @return Document
*/
public XWPFDocument getDocument(String docxPath) {
//xwpfDocument是专门处理word的,在poi中还有处理其他office文档的类
XWPFDocument docx = null;
try {
OPCPackage pack = POIXMLDocument.openPackage(docxPath);
docx = new XWPFDocument(pack) ;
} catch (Exception e) {
e.printStackTrace();
}
return docx;
}
/**
* 解析 word 文档的标题
* @param docx Document 对象
* @return word 文档中标题
*/
public String getTilte(XWPFDocument docx) {
String title = "\";
List<XWPFParagraph> paras = docx.getParagraphs();
XWPFParagraph p = null;
for (int i = 0; i < paras.size(); i++) {
if(p.getText() != null && !p.getText().equals("") && !p.getText().equals("r")){
title = p.getText().trim();
break;
}
}
return title;
}
/**
* 获取 word 文档里所有文字内容(不包括图片、表格等格式的内容)
* @param docx Document 对象
* @return word 文档中文字部分全部内容
*/
public String getContent(XWPFDocument docx) {
String content = "";
try {
List<XWPFParagraph> paras = docx.getParagraphs();
for (XWPFParagraph para : paras) {
content += para.getText().trim();
}
} catch (Exception e) {
e.printStackTrace();
}
return content.replaceAll("", "");
}
/**
* 获取 word 文档里所有文字内容(不包括图片、表格等格式的内容)
* @param docxPath docx 对象路径
* @return word 文档中文字部分全部内容
*/
public String getContent(String docxPath) {
String content = "";
try {
OPCPackage opcPackage = POIXMLDocument.openPackage(docxPath);
POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
content += extractor.getText().trim();
} catch (Exception e) {
e.printStackTrace();
}
return content.replaceAll("", "");
}
/**
* 获取 word 文档里面所有图片并另存到指定目录下
* @param docx Document 对象
* @param picPath 保存图片路径
* @param suffix 后缀名
*/
public void getPictures(XWPFDocument docx, String picPath, String suffix){
List<XWPFPictureData> wpdList = docx.getAllPictures();
if(wpdList != null && wpdList.size() > 0){
for (int i = 0; i < wpdList.size(); i++) {
byte[] picByte = wpdList.get(i).getData(); //获取图片数据流
FileOutputStream fos = null;
try {
fos = new FileOutputStream(picPath + i + suffix);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
try {
fos.write(picByte);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 获取word 文档里面所有表格
* @param doc Document 对象
*/
public void getTables(XWPFDocument docx){
Iterator<XWPFTable> tableIt = docx.getTablesIterator();
while (tableIt.hasNext()) {
XWPFTable table = tableIt.next();
String rowInfo = "";
for(int j = 0; j < table.getRows().size(); j ++){
List<XWPFTableCell> cells = table.getRow(j).getTableCells(); // 获得所有列
for (int k = 0; k < cells.size(); k++) {
rowInfo += cells.get(k).getText().trim() + ";";
}
}
System.out.println(rowInfo);
}
}
/**
* 获取word 文档中最大的字体
* @param doc Document 对象
*/
public int getMaxFontSize(XWPFDocument docx) {
int fontSize = 0;
/* List<XWPFParagraph> paraGraph = docx.getParagraphs();
for(XWPFParagraph para :paraGraph ){
List<XWPFRun> run = para.getRuns();
for(XWPFRun r : run){
int i = 0;
System.out.println("字体颜色:"+r.getColor());
System.out.println("字体名称:"+r.getFontFamily());
System.out.println("字体大小:"+r.getFontSize());
System.out.println("Text:"+r.getText(i++));
System.out.println("粗体?:"+r.isBold());
System.out.println("斜体?:"+r.isItalic());
if(fontSize < r.getFontSize()){
fontSize = r.getFontSize();
}
}
}*/
return fontSize;
}
/**
* 获取 word 文档的创建信息
* @param docPath docx路径
* @return 创建文档的信息
*/
public Map<String, String> getInfo(String docxPath) {
try {
InputStream is = new FileInputStream(docxPath);
XWPFDocument docx = new XWPFDocument(is);
XWPFWordExtractor extractor = new XWPFWordExtractor(docx);
CoreProperties coreProps = extractor.getCoreProperties();
Map<String, String> mapInfo = new HashMap<String, String>();
//mapInfo.put("category", coreProps.getCategory()); //分类
mapInfo.put("author", coreProps.getCreator()); //创建者
mapInfo.put("title", coreProps.getTitle()); //标题
mapInfo.put("subject", coreProps.getSubject()); // 主题
mapInfo.put("keyword", coreProps.getKeywords()); // 关键词
mapInfo.put("createdate", FormatTextUtil.dateFormat(coreProps.getCreated())); //创建时间
mapInfo.put("updatedate", FormatTextUtil.dateFormat(coreProps.getLastPrinted())); // 修改时间
} catch (Exception e) {
}
return null;
}
}
相关推荐
感谢关注 Ithao123精品文库频道,ithao123.cn是专门为互联网人打造的学习交流平台,全面满足互联网人工作与学习需求,更多互联网资讯尽在 IThao123!
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java poi操作word转pdf
Java 读取Word中表格,支持doc、docx
Java实现poi方式读取word文件内容(不带格式)
POI抽取word文档图片并保存
使用POI操作Excel和Word
利用poi操作word文档
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服