Location>code7788 >text

Solve the problem of garbled Excel files in Maven packaging projects

Popularity:154 ℃/2025-04-05 08:32:34

During Java project development, we often use Maven to manage project dependencies and package projects. When it comes to using Freemarker to export Excel files, many developers may encounter a headache - Excel files appear garbled after Maven is packaged. Today, we will explore this problem and its solutions in depth.

Problem phenomenon

After using the Freemarker template to generate an Excel file and package the project through Maven, when the generated Excel file is opened, you will find that the text content in the file is garbled. This seriously affects the readability and data display effect of Excel files, and is an urgent problem for scenarios that rely on these Excel files for business processing.

Analysis of the cause of the problem

When Maven compiles a project, the resource file will be filtered by default. During this process, if not configured correctly, some inappropriate conversion operations will be performed for binary format files such as Excel (such as .xls files), resulting in garbled file content. Simply put, Maven treats Excel files as normal text files with some character encoding related processing, which is not the way Excel files expect.

Solution

To solve this problem, we need to adjust the configuration of Maven to ensure that the Excel file will not be processed incorrectly during the compilation process. The specific solutions are as follows:

Add maven - resources - plugin plugin configuration

In the project file, add the following maven - resources - plugin plugin configuration:
<plugin>
<groupId></groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<nonFilteredFileExtensions>
<!-- No transcoding of rar and xls -->
<nonFilteredFileExtension>rar</nonFilteredFileExtension>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
The purpose of this configuration is to tell Maven not to perform any transcoding operations on .rar and .xls format files during resource filtering, keeping their original binary format.

Adjust resources configuration

At the same time, we also need to optimize the resource configuration of the project to ensure that the Excel file can be correctly included in the packaging process. Add the following configuration in the resources tag:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.json</include>
<include>**/*.ftl</include>
<include>**/*.xls</include>
</includes>
</resource>
</resources>
The configuration here includes the .xml, .json, .ftl and .xls files in the src/main/java directory into the resource packaging scope. Among them, the resources in the src/main/resources directory are processed according to the default filtering rules (filtering is true), and for files of the specified type in the src/main/java directory, they will be packaged according to our configuration.
Through the above two steps of configuration, we not only avoid Maven's incorrect transcoding of Excel files, but also ensure that Excel files can be correctly included in the project packaging results, thus effectively solving the problem of garbled code of Excel files after Maven is packaged.
I hope that through the introduction of this article, we can help you successfully solve the problem of garbled Maven packaging Excel files encountered in project development, making the project development process smoother. In actual development, when encountering similar problems, in-depth analysis of the causes of the problems and rationally adjust the configuration, so that solutions can often be quickly found.