引入依赖包
pom
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>2.14.0</version>
</dependency>
创建Excel模版文件
在模版中第一个单元格设置批注,设置jxls扫描区域,如:jx:area(lastCell="U2")

在循环数据列的第一个单元格批注设置jxls指令进行循环展示数据,如:jx:each(items="items" var="item" lastCell="U9")

其他详细指令可查看官方文档
封装工具类
java
public class JxlsUtils {
public static void exportExcel(InputStream is, OutputStream os, Map<String, Object> model) throws IOException {
Context context = new Context();
if (model != null) {
for (String key : model.keySet()) {
context.putVar(key, model.get(key));
}
}
// 使用 JxlsHelper 自动处理转换
JxlsHelper.getInstance()
// 禁用Jxls在生成Excel文件时对模板中所有公式的自动处理和重新计算
.setProcessFormulas(false)
.processTemplate(is, os, context);
}
}
在pom文件maven打包工具配置不需要过滤的文件扩展名
pom
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<!-- 指定不需要过滤的文件扩展名 -->
<nonFilteredFileExtensions>
<!-- 禁止过滤 Excel 2007+ 格式 -->
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
<!-- 禁止过滤 Excel 97-2003 格式 -->
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
</build>