java小知识:解析读取docx文档的文本内容

一.pom文件里引入相关jar包

复制代码
	<!-- easy poi -->
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-base</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-web</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-annotation</artifactId>
        <version>4.1.0</version>
    </dependency>

二.文档解析

1.读取docx

复制代码
public static String readDocxUrlContent(String docxUrl) {
    InputStream inputStream = null;
    try {
   		//1.创建URL对象,获得资源
        URL url = new URL(docxUrl);
        //2.打开连接的输入流
        inputStream = url.openStream();
        //3.定义文档对象
        XWPFDocument document = new XWPFDocument(inputStream);
        //4.获取文档内所有的内容段落
        List<XWPFParagraph> paragraphs = document.getParagraphs();
        StringBuilder paragraphText = new StringBuilder();
        if (ObjectUtils.isEmpty(paragraphs)) {
        	//5.不能获取到内容段落,则读取文档内的表格内容
            return WordUtils.getTableCellsText(document);
        }
        //6.读取段落内容文本
        for (XWPFParagraph paragraph : paragraphs) {
            paragraphText.append(paragraph.getText());
        }
        return paragraphText.toString();
    } catch (Exception e) {
        log.error("FileUtil.readDocxUrlContent:读取文件内容失败", e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                log.error("FileUtil.readDocxUrlContent:关闭inputStream失败", e);
            }
        }
    }
    return null;
}

2.获取所有的表格内的文本内容

复制代码
/**
 * 获取文档表内,所有的文本内容
 */
public static String getTableCellsText(XWPFDocument document) {
    StringBuilder stringBuilder = new StringBuilder();
    //1. 读取文档内所有表
    List<XWPFTable> tables = document.getTables();
    for (XWPFTable table : tables) {
        //2. 读取表里所有行
        List<XWPFTableRow> rows = table.getRows();
        for (int i = NumberUtils.INTEGER_ZERO; i < rows.size(); i++) {
            XWPFTableRow row = rows.get(i);
            //3. 读取每行的所有单元格
            List<XWPFTableCell> tableCells = row.getTableCells();
            for (int j = NumberUtils.INTEGER_ZERO; j < tableCells.size(); j++) {
                XWPFTableCell cell = tableCells.get(j);
                stringBuilder.append(cell.getText());
            }
        }
    }
    return stringBuilder.toString();
}
相关推荐
llwszx1 小时前
深入理解Java锁原理(一):偏向锁的设计原理与性能优化
java·spring··偏向锁
云泽野1 小时前
【Java|集合类】list遍历的6种方式
java·python·list
二进制person2 小时前
Java SE--方法的使用
java·开发语言·算法
小阳拱白菜3 小时前
java异常学习
java
FrankYoou4 小时前
Jenkins 与 GitLab CI/CD 的核心对比
java·docker
麦兜*4 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
KK溜了溜了5 小时前
JAVA-springboot 整合Redis
java·spring boot·redis
天河归来5 小时前
使用idea创建springboot单体项目
java·spring boot·intellij-idea
weixin_478689765 小时前
十大排序算法汇总
java·算法·排序算法
码荼5 小时前
学习开发之hashmap
java·python·学习·哈希算法·个人开发·小白学开发·不花钱不花时间crud