使用时间:4小时

使用poi方法将word中的内容提取出来,并输出到控制台或者存储到数据库
poi.jar下载地址:https://www.apache.org/dyn/closer.lua/poi/release/bin/poi-bin-3.17-20170915.tar.gz
需要导入的包

根据标题和内容字体大小的不同,寻找所需要的段落和标题,并判断是标题还是内容。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class poi_word { public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream(new File("path")); //读取文件
POIFSFileSystem fs = new POIFSFileSystem(is);
@SuppressWarnings("resource")
HWPFDocument document = new HWPFDocument(fs);
Range range = document.getRange(); //存储word内容到document中 for (int i = 0; i < range.numParagraphs()-2; i++) { //numparagraphs代表段落总数
int setparagraph=i; //记录当前段落
Paragraph para_1 = range.getParagraph(i);// 获取第i段
Paragraph para_2 = range.getParagraph(i+1);// 获取第i+1段
Paragraph para_3 = range.getParagraph(i+2);// 获取第i+2段 String paratext1 = para_1.text().trim().replaceAll("\r\n", ""); //当前段落的内容并去除换行
String paratext2 = para_2.text().trim().replaceAll("\r\n", ""); //当前段落的内容并去除换行
CharacterRun run1=para_1.getCharacterRun(0);
CharacterRun run2=para_2.getCharacterRun(0);
CharacterRun run3=para_3.getCharacterRun(0); //段落属性
if (paratext1.length() > 0&&paratext2.length() > 0) { if(run1.getFontSize()>run2.getFontSize()&&run2.getFontSize()>run3.getFontSize()) continue; // 当连续三个及以上的字体大小不同的段落存在时则跳过当前循环,直到两个段落存在(找到小标题和内容)
String content="";
if(run1.getFontSize()>=run2.getFontSize()) { //当两段内容字体大小为大于时 则为标题和内容 相等时则同为内容
content +=paratext2; //第i+1段为内容
run1=run2;
run2=run3; //顺序重新定位段落
setparagraph++;
}
System.out.println(paratext1+"\t"+content);
i=setparagraph;
}
}
}
}

参考来源:https://www.cnblogs.com/wys-373/p/10568322.html

最新文章

  1. html5 canvas常用api总结(三)--图像变换API
  2. POMDP
  3. 【C#进阶系列】16 数组
  4. 在MVC3中修改KindEditor实现图片删除
  5. saiku 展示优化
  6. Oracle private dblink和pubic dblink
  7. (总结)CentOS Linux下VNC Server远程桌面配置详解
  8. SQL Server2008不允许修改表结构解决办法
  9. Python入门教程 超详细1小时学会Python(转)
  10. PHP对象注入 PHP Object Injection
  11. Android系统架构与系统源码目录
  12. IDEA tomcat8 控制台日志乱码
  13. vue 父子组件互相传值容易出现的报错
  14. 设计模式之Interpreter(解释器)(转)
  15. Java基础——网络编程(一)
  16. python利用beautifulSoup写爬虫
  17. 浅谈Linux系统中如何查看进程
  18. CentOS 7中firewall防火墙详解和配置以及切换为iptables防火墙
  19. as, idea 出现 Gradle&#39;s dependency cache may be corrupt 错误分析
  20. CentOS 6.5 下安装 Redis

热门文章

  1. Redis 常用命令 大全
  2. Web01_HTML
  3. Sqlserver实现故障转移 — 域控(1)
  4. Cocos2d-X多线程(4) 在子线程中进行网络请求
  5. 【推荐算法工程师技术栈系列】程序语言--Java
  6. v-if 和v-show的区别
  7. map member functions
  8. “laravel.log” could not be opened: failed to open stream
  9. RabbitMQ入门教程(十五):普通集群和镜像集群
  10. Property &#39;showModal&#39; does no t exist on type &#39;JQuery&lt;HTMLElement&gt;&#39;