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();
}
相关推荐
摇滚侠9 小时前
Java 零基础全套教程,集合框架,笔记 153-163
java·开发语言·笔记
nannan12329 小时前
后端技术栈梳理
java
L、21810 小时前
CANN算子开发调试实战:从“Segmentation Fault“到定位根因的完整流程
java·开发语言
索木木11 小时前
NCCL SHARP 和 TREE算法
java·服务器·算法
NiceCloud喜云11 小时前
Claude Files API 深入:从上传、复用到配额管理的工程化指南
android·java·数据库·人工智能·python·json·飞书
超梦dasgg12 小时前
Java 生产环境 MQ 技术选型全解析
java·开发语言·java-rocketmq·java-rabbitmq
霸道流氓气质12 小时前
Spring AI 多工具链式调用(Tool Chain)极简实战
java·人工智能·spring
罗超驿12 小时前
22.深入剖析JDBC架构:从原生API到企业级数据交互核心
java·数据库·mysql·面试
一直有一个ac的梦想12 小时前
cmu15445 2025fall lec 18 transactions with two-phase lock
java·开发语言·数据库
九皇叔叔12 小时前
Spring-Ai-Alibaba [04] 04-llm-platform-custom-demo
java·人工智能·spring