Java将word转换为文本

复制代码
在Java中,你可以使用Apache POI库来读取Word文档并提取文本内容。你可以在 Maven 项目中添加以下依赖:
复制代码
<!-- Apache POI for Word 2007+ (.docx) -->
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi</artifactId>
	<version>4.1.2</version>
</dependency>
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>4.1.2</version>
</dependency>

<!-- Apache POI for Word 97-2003 (.doc) -->
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-scratchpad</artifactId>
	<version>4.1.2</version>
</dependency>
复制代码
以下是一个简单的示例代码,展示如何使用Apache POI将Word文档转换为文本:
复制代码
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
public class WordUtils {

    /**
     * Word转换为文本
     *
     * @param file Word文件
     * @return 提取的文本
     */
    public static String wordToTextConverter(MultipartFile file) {
        try {
            if (file.getOriginalFilename().endsWith(".doc")) {
                // 处理Word 97-2003文档
                return processOldWordDocument(file);
            } else if (file.getOriginalFilename().endsWith(".docx")) {
                // 处理Word 2007及以上文档
                return processNewWordDocument(file);
            } else {
                throw new RuntimeException("不支持的Word文档格式");
            }
        } catch (IOException e) {
            throw new RuntimeException("无法读取Word文档", e);
        }
    }

    private static String processOldWordDocument(MultipartFile file) throws IOException {
        try (HWPFDocument document = new HWPFDocument(file.getInputStream())) {
            WordExtractor extractor = new WordExtractor(document);
            String text = extractor.getText();
            log.info("Word文档内容: {}", text);
            return text;
        }
    }

    private static String processNewWordDocument(MultipartFile file) throws IOException {
        try (XWPFDocument document = new XWPFDocument(file.getInputStream())) {
            if (document.getParagraphs().isEmpty()) {
                throw new RuntimeException("Word文档为空");
            }
            XWPFWordExtractor extractor = new XWPFWordExtractor(document);
            String text = extractor.getText();
            log.info("Word文档内容: {}", text);
            return text;
        }
    }
}
相关推荐
NE_STOP9 小时前
MyBatis-配置文件解读及MyBatis为何不用编写Mapper接口的实现类
java
后端AI实验室14 小时前
用AI写代码,我差点把漏洞发上线:血泪总结的10个教训
java·ai
程序员清风16 小时前
小红书二面:Spring Boot的单例模式是如何实现的?
java·后端·面试
belhomme16 小时前
(面试题)Redis实现 IP 维度滑动窗口限流实践
java·面试
Be_Better16 小时前
学会与虚拟机对话---ASM
java
开源之眼18 小时前
《github star 加星 Taimili.com 艾米莉 》为什么Java里面,Service 层不直接返回 Result 对象?
java·后端·github
Maori31619 小时前
放弃 SDKMAN!在 Garuda Linux + Fish 环境下的优雅 Java 管理指南
java
用户9083246027320 小时前
Spring AI 1.1.2 + Neo4j:用知识图谱增强 RAG 检索(上篇:图谱构建)
java·spring boot
小王和八蛋20 小时前
DecimalFormat 与 BigDecimal
java·后端
beata20 小时前
Java基础-16:Java内置锁的四种状态及其转换机制详解-从无锁到重量级锁的进化与优化指南
java·后端