2025.6.17

这个错误表明你的项目中缺少Apache POI的HWPF组件,这是用于处理旧版Word文档(.doc格式)的库。以下是解决方案:

解决方法

1. 添加完整的POI依赖

更新你的pom.xml文件,确保包含所有必要的依赖:

<!-- 基础POI库 -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.2.3</version>
</dependency>

<!-- 处理新版Word文档(.docx) -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.3</version>
</dependency>

<!-- 处理旧版Word文档(.doc) -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>5.2.3</version>
</dependency>

2. 替代方案(如果不想添加HWPF)

如果你只需要处理.docx文件而不用支持旧的.doc格式,可以简化代码:

private ResponseResult<String> WordParser(MultipartFile file) {
    try {
        if (file.isEmpty()) {
            return ResponseResult.error("上传的文件为空");
        }

        String fileName = file.getOriginalFilename();
        String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();

        if (!fileExt.equals("docx")) {
            return ResponseResult.error("仅支持.docx格式的Word文件");
        }

        InputStream inputStream = file.getInputStream();
        XWPFDocument docx = new XWPFDocument(inputStream);
        XWPFWordExtractor extractor = new XWPFWordExtractor(docx);
        String content = extractor.getText();
        
        extractor.close();
        inputStream.close();
        
        return ResponseResult.success(content);
    } catch (Exception e) {
        e.printStackTrace();
        return ResponseResult.error("解析Word文件时出错: " + e.getMessage());
    }
}

3. 如果你确实需要处理.doc文件

确保:

  1. 已添加poi-scratchpad依赖
  2. 正确导入了类:
    import org.apache.poi.hwpf.HWPFDocument;
    import org.apache.poi.hwpf.extractor.WordExtractor;
    

4. 清理并重新构建项目

添加依赖后:

  1. 执行Maven的CleanInstall
  2. 刷新IDE的项目依赖(在IntelliJ中是右键项目 > Maven > Reimport)

这样应该能解决"Cannot resolve symbol 'hwpf'"的问题。

posted @ 2025-06-17 23:04  258333  阅读(54)  评论(0)    收藏  举报