SpringBoot集成jxls2实现复杂(多表格)excel导出

核心依赖

需求

导出多个表格,包含图片,类似商品标签

1.配置模板

创建一个xlsx的模板文件,配置如下

  • 该模板进行遍历了两次,因为我想要导出的数据分为两列展示,左右布局,一个循环实现不了,所以采用两个循环奇偶遍历展示

在这里插入图片描述

2.引入依赖

<dependency>
    <groupId>org.jxls</groupId>
    <artifactId>jxls</artifactId>
    <version>2.12.0</version>
</dependency>
<dependency>
    <groupId>org.jxls</groupId>
    <artifactId>jxls-poi</artifactId>
    <version>2.12.0</version>
</dependency>

3.创建测试类

package com.example;

import org.example.SpringBootWsDemoApplication;
import org.example.domain.vo.ProductVO;
import org.junit.jupiter.api.Test;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.test.context.TestPropertySource;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

@SpringBootTest(classes = SpringBootWsDemoApplication.class)
@TestPropertySource(properties = {"app.template.path=classpath:/templates/jxl_temp_img2.xlsx"})
public class ExportImg2Test {

    @Value("${app.template.path}")
    private Resource templateResource;

    @Test
    public void test() throws IOException {
        List<ProductVO> batch = ProductVO.batchToJxls();

        // 创建上下文并设置数据
        Context context = new Context();
        context.putVar("products", batch);

        // 加载模板
        OutputStream os = new FileOutputStream("output_img.xlsx");
        // 获取模板文件输入流
        // InputStream templateStream = ExportTest.class.getResourceAsStream("templates/jxl_temp.xlsx");
        // JxlsHelper.getInstance().processTemplate(templateStream, os, context);
        // imageType图片类型默认是PNG,支持:PNG, JPEG, EMF, WMF, PICT, DIB,模板中src是model中传入的字节数组byte[]
        JxlsHelper instance = JxlsHelper.getInstance();
        instance.processTemplate(templateResource.getInputStream(), os, context);
    }

}

4.效果图

在这里插入图片描述

注意事项

  • 模板文件放到src/main/resources下本地开发工具直接运行可以访问,打包jar后也许访问不到,这种情况如果出现了的话,要么就把模板文件放到服务器本地磁盘,通过文件路径读取,而不是从demo.jar!/BOOT-INF/classes!/templates/jxls_temp.xlsx

  • 导出需要的是一个图片文件的byte数组

// InputStream imageInputStream = new FileInputStream("D:/qr.jpg");File file = ResourceUtils.getFile("classpath:static/img/qr.jpg");
// 使用工具方法把流转成byte数组
byte[] imageBytes = Util.toByteArray(Files.newInputStream(file.toPath()));
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
jxls是一个开源的Java工具,可以根据Excel模板文件生成Excel文件。jxls支持复杂Excel模板,可以在模板中包含多个工作表、多个单元格样式、公式等。 下面是使用jxls导出Excel的步骤: 1. 创建Excel模板文件,可以使用Excel或者其他电子表格软件创建,也可以使用jxls提供的Excel模板文件样例。 2. 在Java代码中使用jxls API读取Excel模板文件,并将要填充到Excel文件中的数据传递给jxls。 3. 在Excel模板文件中,使用jxls提供的标记语言标记待填充的单元格或区域。 4. 使用jxls API将填充好数据的Excel文件输出到指定位置。 下面是一个简单的示例: 1. 创建Excel模板文件,假设文件名为template.xlsx,包含两个工作表Sheet1和Sheet2,每个工作表中包含一个表格表格中包含两个单元格A1和B1,A1单元格中填充姓名,B1单元格中填充年龄。 2. 在Java代码中,使用jxls API读取Excel模板文件,准备要填充到Excel文件中的数据: ```java InputStream is = new FileInputStream(new File("template.xlsx")); OutputStream os = new FileOutputStream(new File("output.xlsx")); Map<String, Object> model = new HashMap<String, Object>(); List<Person> persons = new ArrayList<Person>(); persons.add(new Person("Alice", 25)); persons.add(new Person("Bob", 30)); model.put("persons", persons); ``` 3. 在Excel模板文件中,使用jxls提供的标记语言标记待填充的单元格或区域。在A1单元格中插入${person.name},在B1单元格中插入${person.age},表示在Excel文件中填充persons集合中的每个Person对象的name和age属性。 4. 使用jxls API将填充好数据的Excel文件输出到指定位置: ```java XLSTransformer transformer = new XLSTransformer(); Workbook workbook = transformer.transformXLS(is, model); workbook.write(os); os.flush(); os.close(); is.close(); ``` 这样,就可以根据复杂模板导出Excel文件了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值