JXLS系列-02-模板数据导出图片
1-pom文件
见第一篇文章:https://blog.csdn.net/ycmy2017/article/details/128644340?spm=1001.2014.3001.5501
2-模板准备
template2.xlsx
![](https://img-blog.csdnimg.cn/img_convert/7cc84f9e0da255676b028919358b168c.jpeg)
jx:image(lastCell="A4" src="headMap.image" imageType="JPEG" scaleX=1.0 scaleY=1.0)
标签字段描述部分源码如下:
![](https://img-blog.csdnimg.cn/img_convert/a0ac063a69a34acfb73d1f98a9f8dd19.jpeg)
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();
}
}