如何解决“导入org.apache.poi无法解析”

时间:2019-01-29 14:55:11

标签: java maven visual-studio-code apache-poi

我想在代码中使用Apache-POI,但是在导入语句中收到错误消息The import org.apache.poi cannot be resolved import org.apache.poi.xssf.usermodel.XSSFWorkbook;

我是一个初学者,具有全新设置的Visual Studio Code v1.30.2,Maven 3.6.0,Java JRE 1.8.0_201。

我激活了以下扩展名: Java调试器,0.16.0 Java依赖项查看器0.3.0 Java扩展包0.5.0 Java测试运行程序0.14.0 Java(TM)0.37.0的语言支持 适用于Java的Maven,0.14.0

我在依赖项部分的pom.xml中输入了以下语句:

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.1</version>
</dependency>

我也尝试了其他POI版本,例如3.10-决赛。

我对Java,Maven和VSCode还是太陌生,以确保我已包括所有必要的信息以指向解决方案。请帮助我:)

我已经运行mvn compile,结果如下:

C:\Users\MYUSERNAME\Documents\Java\project2>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< MYNAME.project2:project2 >---------------------
[INFO] Building project2 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project2 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\MYUSERNAME\Documents\Java\project2\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ project2 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\MYUSERNAME\Documents\Java\project2\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[12,1] package org.apache.poi.ss.usermodel does not exist
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[13,37] package org.apache.poi.xssf.usermodel does not exist
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[14,37] package org.apache.poi.xssf.usermodel does not exist
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[46,9] cannot find symbol
  symbol:   class XSSFWorkbook
  location: class MYNAME.project2.App
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[46,45] cannot find symbol
  symbol:   class XSSFWorkbook
  location: class MYNAME.project2.App
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[48,9] cannot find symbol
  symbol:   class XSSFSheet
  location: class MYNAME.project2.App
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[55,13] cannot find symbol
  symbol:   method setCellValue(java.lang.String)
  location: variable cell of type com.google.common.collect.Table.Cell
[INFO] 7 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.549 s
[INFO] Finished at: 2019-01-30T09:41:45+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project project2: Compilation failure: Compilation failure:
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[12,1] package org.apache.poi.ss.usermodel does not exist
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[13,37] package org.apache.poi.xssf.usermodel does not exist
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[14,37] package org.apache.poi.xssf.usermodel does not exist
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[46,9] cannot find symbol
[ERROR]   symbol:   class XSSFWorkbook
[ERROR]   location: class MYNAME.project2.App
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[46,45] cannot find symbol
[ERROR]   symbol:   class XSSFWorkbook
[ERROR]   location: class MYNAME.project2.App
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[48,9] cannot find symbol
[ERROR]   symbol:   class XSSFSheet
[ERROR]   location: class MYNAME.project2.App
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[55,13] cannot find symbol
[ERROR]   symbol:   method setCellValue(java.lang.String)
[ERROR]   location: variable cell of type com.google.common.collect.Table.Cell
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/

我还粘贴了mvn -X compile here

的完整输出

我已粘贴完整的POM.XML here

在我的本地文件夹C:\ Users \ MYUSERNAME.m2 \ repository \ org \ apache中,没有没有子文件夹 poi。

3 个答案:

答案 0 :(得分:2)

感谢所有贡献者,尤其是@ayZagen(他将我指向pom.xml)。

原来,我已经将poi的依赖项插入到

的依赖项部分
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-checkstyle-plugin</artifactId>

而不是进入正确的部分<project> <dependencies>

我将依存关系移至正确的部分时,maven下载POI,然后编译该部分代码。

很抱歉花了点时间解决小错误。感谢您帮助我找出问题并提出正确的问题!这是一次非常积极的经历!

答案 1 :(得分:0)

编辑 尝试将poi依赖项移至根dependencies标记。

运行mvn compile

  

第一次执行此(或任何其他)命令时,Maven将执行   需要下载所需的所有插件和相关依赖项   完成命令。通过全新安装的Maven,这可能需要   相当长的时间(在上面的输出中,花费了将近4分钟)。如果你   再次执行命令,Maven现在将拥有所需的内容,因此它   无需下载任何新内容,就可以执行   命令要快得多。

Maven: Getting Started

答案 2 :(得分:0)

根据错误日志,有2种可能性

  1. 网络问题 。 Maven无法从其存储库下载jar文件,因此 .m2 文件夹中没有 POI
    解决方案:检查网络/ Internet。
  2. 文件已损坏 。如果文件损坏,就会发生这种情况。
    解决方案:删除 .m2 文件夹内的文件夹org.apache.poi。右键单击项目(来自Eclipse)-> Maven-> Update project
    然后创建cleanrefresh项目,然后运行命令 Maven clean install