【问题标题】:Loading an excel(.xlsx/.xls) file from maven project resources folder (src/main/resources)从 maven 项目资源文件夹 (src/main/resources) 加载 excel(.xlsx/.xls) 文件
【发布时间】:2016-06-06 06:55:26
【问题描述】:

我正在尝试从 maven 项目的资源文件夹(src/main/resources)加载一个 excel 文件。

文件夹结构

MyWebApp
   |______src/main/java
   |           |____Test.java
   |
   |______src/main/resources
   |            |______test
   |                    |___hello.properties
   |                    |___template.xlsx
   |______target
            |___MyWebApp
                  |____WEB_INF
                         |___classes
                                |__test
                                     |__hello.properties
                                     |__template.xlsx

我的方法

//loading excel file
String resource = "/test/template.xlsx";
System.out.println(this.getClass().getResource(resource) == null); // prints true

//loading properties file
String resource = "/test/hello.properties";
System.out.println(this.getClass().getResource(resource) == null); //prints false

//I have also tried below methods
this.getClass().getClassLoader().getResourceAsStream(resource); //null
new ClassPathResource(resource).getInputStream(); //null

做了一些谷歌搜索后,我知道了,maven filters binary contents。为了克服我修改了我的pom.xml 以允许.xlsx,.xls 文件扩展名不被这个help 过滤。

pom.xml

<configuration>                
    <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
        <resources>
            <resource>
                 <directory>${basedir}/src/main/resources</directory>
                     <includes>
                         <include>**/*.xlsx</include>
                         <include>**/*.xls</include>
                     </includes>
             </resource>
         </resources>
</configuration>

我可以加载属性文件,但我无法使用上述方法加载 excel 文件。在我这边,我提到了以下两个链接(Rererence-1Reference-2) 但没有成功。如果您对此问题有一些想法/想法,请帮助我。

【问题讨论】:

    标签: java excel maven apache-poi classpath


    【解决方案1】:

    在您链接的the maven documentation page 中说:

    如果您同时拥有文本文件和二进制文件作为资源 建议有两个分开的文件夹。一个文件夹 src/main/resources (默认) 用于未过滤的资源 和另一个文件夹 src/main/resources-filtered 的资源 被过滤。

    因此,您应该将属性和 xlsx 文件保存在不同的目录中。

    还有一个information about excluding binary files from filtering

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          ...
          <nonFilteredFileExtensions>
            <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
            <nonFilteredFileExtension>swf</nonFilteredFileExtension>
          </nonFilteredFileExtensions>
          ...
        </configuration>
     </plugin>
    

    【讨论】:

    • 感谢 Ulrich 的回答,我没有使用 2 个不同的文件夹,而是使用 &lt;include&gt;**/*.xlsx&lt;/include&gt; 包含我的 excel 文件。我们可以有 2 个文件夹,或者我们可以使用“包含”。以及我已经尝试过的排除二进制文件的选项,仍然是同样的问题。
    • 您是否检查过template.xlsx 是否包含在mvn package 期间生成的jar 文件中?
    • 是的,jar文件包含excel文件
    • 我创建了一个chat room
    猜你喜欢
    • 2017-02-05
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 2016-10-12
    • 2012-05-06
    • 2013-05-11
    相关资源
    最近更新 更多