java实现将数据保存到excel模板

1.导入依赖

 <!--对象 get set插件 便于简写对象属性get set 方法  非必须-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
        </dependency>
        <!--必须-->
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls-poi</artifactId>
            <version>1.1.0</version>
        </dependency>

        <!--      日志包  非必须  避免控制台提示日志相关警告信息-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
            <scope>compile</scope>
        </dependency>

2. 涉及模板文件(模板需要右键设置批注的看下图)

 2.1.首行设置整体区域(   Administrator: jx:area(lastCell="AJ48")   Administrator是右键插入批注直接就有的不要动每个人的可能不一样只添加jx:这一串   AJ48是因为我的合同模板最后一行是48最后一列是AJ)

 2.2 需要循环的模板如下(list是java传的我的是list如下面代码 AJ8的意思是从AJ8当前行开始循环往下) 批注内容:   Administrator: jx:each(items="list" var="item" lastCell="AJ8")       eg: 赋值${item.java程序设置的值} 具体看模板及我的代码示例

 3.模板赋值

package com.laomao.printing;

import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import org.jxls.util.Util;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class Print {
    /**
     * 打印方法  不插入图片
     * @throws Exception
     */
    public static void print() throws Exception {
        //准备数据
        List<OrderContract> list = new ArrayList<OrderContract>();
        for (int i = 0; i < 20; i++) {
            //自己要赋值的list跟excel文件上的批注一致
            OrderContract stu = new OrderContract();
            stu.setXh(1);
            stu.setBrandName("品牌");
            stu.setBuyNo("订货号");
            stu.setDay(10);
            stu.setMeasure("个");
            stu.setModel("型号");
            stu.setNum(10);
            stu.setTitle("花姑娘滴");
            stu.setUnitPrice(new BigDecimal(10));
            stu.setTotalPrice(new BigDecimal(100));
            list.add(stu);
        }
        //获取模板文件,你自己当前模板的位置  我这里为当前项目下
        //如果为系统路径 为: InputStream is = new FileInputStream("F://student.xlsx");
//        InputStream is = Print.class.getClassLoader().getResourceAsStream("student.xlsx");
        InputStream is = Print.class.getClassLoader().getResourceAsStream("order_contract.xls");
        //根据模板生成的文件保存路径  我这里保存在本地D盘
        OutputStream os = new FileOutputStream("D://order_contract.xls");
        //绑定数据
        Context context = new Context();
        //涉及太多只写了一个单独的
        context.putVar("orderNo", "123456");
//        context.putVar("orderTime", new Date());
        context.putVar("list", list);
        //生成
        JxlsHelper.getInstance().processTemplate(is, os, context);
    }


    /**
     * 打印方法  不插入图片
     *
     * 模板文件为student1.xlsx
     * 注意事项:模板文件新加入了一行,所以整体范围也就有所改变,大家注意一下批注中的结束范围
     * 图片所占用的范围为B2到C2
     *
     *
     * @throws Exception
     */
    public static void print1() throws Exception {
        //准备数据
        List<Student> list = new ArrayList<Student>();
        for (int i = 0; i < 20; i++) {
            Student stu = new Student();
            stu.setAge(i);
            stu.setClassName("一年级");
            stu.setName("张三" + i);
            stu.setSex("男");
            list.add(stu);
        }
        //获取模板文件,你自己当前模板的位置  我这里为当前项目下
        //如果为系统路径 为: InputStream is = new FileInputStream("F://student.xlsx");
        InputStream is = Print.class.getClassLoader().getResourceAsStream("student1.xlsx");
        //根据模板生成的文件保存路径  我这里保存在本地D盘
        OutputStream os = new FileOutputStream("D://student1.xlsx");

        //图片路径
        InputStream imageInputStream =Print.class.getClassLoader().getResourceAsStream("a.png");
        //转为字节码,插入的图片只能是字节码格式
        byte[] imageBytes = Util.toByteArray(imageInputStream);
        //绑定数据
        Context context = new Context();
        context.putVar("imageBytes", imageBytes);
        context.putVar("title", "学上统计表");
        context.putVar("list", list);
        //生成
        JxlsHelper.getInstance().processTemplate(is, os, context);
    }

    public static void main(String[] args) throws Exception {
        print();
//        print1();
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值