jxls对模板Excel进行导出

jslx对模板Excel进行导出

jxls是在国外比较受欢迎导出工具,就和国内的easypoi类似,这两者并不冲突,就看个人喜好。我在导出Excel文档时,个人比较喜欢使用jxls,废话不多说,那就直接开干吧

第一步:导入maven坐标

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

第二步:编写后台代码

注意:我目前使用的jxls是2.x版本,因此它简化了很多步骤。1.x版本需要多几个步骤,多一些代码,并且它们的模板使用方式也不同。详情请参阅官网文档

@GetMapping("/exportByJxls")
    public void exportByJxls() throws Exception {
        ArrayList<User> users = new ArrayList<User>();
        User user = new User();
        user.setId("1");
        user.setName("小张");
        user.setAge("18");
        user.setTel("138383838383");
        users.add(user);

        users.add(new User("2","小王","22","384324324"));
        users.add(new User("3","小3","23","3843243224"));
        users.add(new User("4","小4","24","11122224"));
        users.add(new User("5","小5","25","14654324324"));

        File fileIn = new File("C:\\Users\\Administrator\\Desktop\\test\\templateExcel.xlsx");
        File fileOut = new File("C:\\Users\\Administrator\\Desktop\\test\\targetExcel.xlsx");
        OutputStream outputStream = new FileOutputStream(fileOut);


        InputStream inputStream = new FileInputStream(fileIn);


        Context context = new Context();
        context.putVar("data",users);
        JxlsHelper.getInstance().processTemplate(inputStream,outputStream,context);

        outputStream.close();
    }

第三步:制作模板

  • 第一个批注是标识文档扫描的边界,如果你的${}在所写的边界以外,它就不会解析为变量,而是给你当做文本原格式显示
  • 第二个批注是循环,items为传入的数组的key,var是我们自定义的对象名称,用于下面的对象引用

在这里插入图片描述

第四步:运行访问

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值