JXLS系列-02-模板数据导出图片

JXLS系列-02-模板数据导出图片

1-pom文件

见第一篇文章:https://blog.csdn.net/ycmy2017/article/details/128644340?spm=1001.2014.3001.5501

2-模板准备

template2.xlsx

jx:image(lastCell="A4" src="headMap.image" imageType="JPEG" scaleX=1.0 scaleY=1.0)

标签字段描述部分源码如下:

public class ImageCommand extends AbstractCommand {
    public static final String COMMAND_NAME = "image";
    private byte[] imageBytes;
    private ImageType imageType = ImageType.PNG;
    private Area area;
    /** Expression that can be evaluated to image byte array byte[] */
    private String src;
    /**
     * org.apache.poi.ss.usermodel.Picture#resize(double scaleX, double scaleY)
     * <p>
     * Resize the image.
     * <p>
     * Please note, that this method works correctly only for workbooks
     * with the default font size (Arial 10pt for .xls and Calibri 11pt for .xlsx).
     * If the default font is changed the resized image can be streched vertically or horizontally.
     * </p>
     * <p>
     * <code>resize(1.0,1.0)</code> keeps the original size,<br>
     * <code>resize(0.5,0.5)</code> resize to 50% of the original,<br>
     * <code>resize(2.0,2.0)</code> resizes to 200% of the original.<br>
     * <code>resize({@link Double#MAX_VALUE},{@link Double#MAX_VALUE})</code> resizes to the dimension of the embedded image.
     * </p>
     */
    private Double scaleX;
    private Double scaleY;

3-Java代码实现

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
    private String name;
    private String birthDate;
    private BigDecimal payment;
    private byte[] photoImg;

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

import java.io.File;
import java.io.FileInputStream;
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 ExcelTest01 {
    public static void main(String[] args) throws Exception{
        List<Employee> employeeList = new ArrayList<>();
        String img1="D:/001/101.png";
        String img2="D:/001/102.png";
        String img3="D:/001/103.png";
        byte[] bytes1 = Util.toByteArray(new FileInputStream(new File(img1)));
        byte[] bytes2 = Util.toByteArray(new FileInputStream(new File(img2)));
        byte[] bytes3 = Util.toByteArray(new FileInputStream(new File(img3)));
        Employee employee1 = new Employee( "张三", "2010-01-02",new BigDecimal("20100102.12"),bytes1);
        Employee employee2 = new Employee( "李四", "2011-01-03",new BigDecimal("20110103.03"),bytes2);
        Employee employee3 = new Employee( "王五", "2012-01-04",new BigDecimal("20120104.25"),bytes3);
        employeeList.add(employee1);
        employeeList.add(employee2);
        employeeList.add(employee3);
        InputStream is = new FileInputStream("D:/001/template2.xlsx");
        OutputStream os = new FileOutputStream("D:/001/out2.xlsx");
        Context context = new Context();
        context.putVar("employeeList", employeeList);
        JxlsHelper.getInstance().processTemplate(is, os, context);
        os.close();
        is.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值