1、添加依赖关系

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.8</version>
        </dependency>

2、读取word内容代码

 String buffer = "";
        try {
            if (path.endsWith(".doc")) {
                FileInputStream is = new FileInputStream(path);
                WordExtractor ex = new WordExtractor(is);
                buffer = ex.getText();
                is.close();
            } else if (path.endsWith("docx")) {
                OPCPackage opcPackage = POIXMLDocument.openPackage(path);
                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                buffer = extractor.getText();
                opcPackage.close();
            } else {
                return AjaxResult.error("文件不是word文件");
            }
        } catch (Exception e) {
            //e.printStackTrace();
            return AjaxResult.error("读取word文件失败"+e.getMessage());
        }

 

相关文章:

  • 2021-12-28
  • 2021-08-13
  • 2021-12-09
  • 2021-05-08
  • 2021-06-20
  • 2022-12-23
  • 2021-04-19
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-12
  • 2022-02-19
  • 2021-11-30
  • 2022-12-23
  • 2021-12-09
  • 2021-12-09
相关资源
相似解决方案