1.生成效果
word模板
核心代码
Map<String, Object> dateMap = new HashMap<>();
TableRenderData tableRenderData = Tables.ofA4Width().cellMargin(0.19f, 0.19f, 0.19f, 0.19f).center().create();
{
CellRenderData cell1 = new CellRenderData();
CellRenderData cell2 = new CellRenderData();
{
String image = "图片1.jpg";
byte[] imageByte = null;
PictureRenderData pic1 = new PictureRenderData(260, 200, getType(image), imageByte);
ParagraphRenderData graph1 = new ParagraphRenderData();
graph1.addPicture(pic1);
cell1.addParagraph(graph1);
}
{
String image = "图片2.jpg";
byte[] imageByte = null;
PictureRenderData pic1 = new PictureRenderData(260, 200, getType(image), imageByte);
ParagraphRenderData graph1 = new ParagraphRenderData();
graph1.addPicture(pic2);
cell2.addParagraph(graph1);
}
tableRenderData.addRow(Rows.create(cell1, cell2));
}
{
String cell1 = "图片1";
String cell2 = "图片2";
tableRenderData.addRow(Rows.of(cell1, cell2).center().textFontSize(9).rowAtleastHeight(0.6).create());
}
dateMap.put("imageTables", tableRenderData);
PictureType getType(String path) {
PictureType format;
if (path.endsWith(".emf")) {
format = PictureType.EMF;
} else if (path.endsWith(".wmf")) {
format = PictureType.WMF;
} else if (path.endsWith(".pict")) {
format = PictureType.PICT;
} else if (path.endsWith(".jpeg") || path.endsWith(".jpg")) {
format = PictureType.JPEG;
} else if (path.endsWith(".png")) {
format = PictureType.PNG;
} else if (path.endsWith(".dib")) {
format = PictureType.DIB;
} else if (path.endsWith(".gif")) {
format = PictureType.GIF;
} else if (path.endsWith(".tiff")) {
format = PictureType.TIFF;
} else if (path.endsWith(".eps")) {
format = PictureType.EPS;
} else if (path.endsWith(".bmp")) {
format = PictureType.BMP;
} else if (path.endsWith(".wpg")) {
format = PictureType.WPG;
} else {
throw new BizException("不支持的图片类型Unsupported picture: " + path + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
}
return format;
}