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;
        }
    }
}
相关推荐
bing_1589 分钟前
Spring Data MongoDB 提供了哪些核心组件?
java·mongodb·spring
知秋丶14 分钟前
Spring-rabbit重试消费源码分析
java·后端·spring
hello早上好17 分钟前
Spring Bean后处理器
java·架构
沉豆25 分钟前
Jmeter调用jar包中的方法,并使用返回值当请求参数
java·jmeter·jar
天天摸鱼的java工程师37 分钟前
synchronized 与 ReentrantLock 区别?公平锁、非公平锁、可重入锁、自旋锁的原理与应用?
java·后端
一只白鸭子42 分钟前
从实践到思考:Spring Boot + MyBatis关系查询小分享
java
byte轻骑兵1 小时前
【C++特殊工具与技术】优化内存分配(五):显式析构函数的调用
开发语言·c++
天天摸鱼的java工程师1 小时前
详解 Spring Boot 的 RedisAutoConfiguration 配置:从自动装配到自定义扩展
java·后端
Dcs1 小时前
Java 新手指南:类和对象到底是啥?
java
东方佑1 小时前
使用 FastMCP 实现 Word 文档与 JSON 数据互转的 Python 服务
python·json·word