Spring Boot3整合Jxls工具包实现模版excel导出文件

引入依赖包

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>
相关推荐
Daemon4 小时前
AI Agent系列记录(第二篇)
前端·人工智能·后端
冰心少年4 小时前
ROS2节点:机器人的工作细胞
后端
冰心少年4 小时前
ROS2话题:节点间传递数据的桥梁
后端
不懂的浪漫4 小时前
mqtt-plus 架构解析(一):分层架构与设计哲学
spring boot·分布式·物联网·mqtt·架构
星辰徐哥4 小时前
异步定时任务系统的设计与Rust实战集成
开发语言·后端·rust
海兰5 小时前
【springboot】gradle快速镜像配置
spring boot·笔记·后端
武超杰5 小时前
SpringBoot 整合 Spring Security 实现权限控制
spring boot·后端·spring
XMYX-05 小时前
06 - Go 的切片、字典与遍历:从原理到实战
后端·golang
架构师专栏5 小时前
比 MQ 更轻的异步方案:Spring 内置的这个隐藏功能,很多人还不知道
后端