poi word操作之向单元格内添加图片
1,主要难点:
poi word操作之向单元格内添加图片考虑的点:
1)考虑图片在单元格内的占比(动态的调整图片在文档中的宽高)
2,解决步骤
1)首先需要获取图片文件的宽高转化为EMU单位数值的宽高,因为通过poi设置图片时的宽高单位是EMU单位:
File image = new File(path);
BufferedImage read = ImageIO.read(image);
int width = Units.toEMU(read.getWidth());
int height = Units.toEMU(read.getHeight());
2)动态的调整适合文档宽度的EMU数值:
首先需要看你的图片在文档中最大宽度,根据个人需求调整,我的如下:
我的图片最大宽度占比为16cm,因此可以根据公式计算得出适合我的文档宽度的EMU数值。注意,EMU数值只能是整数:
公式:1 EMU = 1/914400英寸= 1/360000 cm
如果图片最大EMU值超出的我的文档的最大值就等比缩放图片的EMU数值:
File image = new File(path);
BufferedImage read = ImageIO.read(image);
int width = Units.toEMU(read.getWidth());
int height = Units.toEMU(read.getHeight());
//1 EMU = 1/914400英寸= 1/36000 mm,16是word文档中图片能设置的最大宽度cm
if(width/360000>16){
//向上取整,不要小数
NumberFormat f = NumberFormat.getNumberInstance();
f.setMaximumFractionDigits(0);
f.setRoundingMode(RoundingMode.UP);
//获取图片的EMU和图片在文档中最大EMU的比例值
Double d=width/360000/16d;
//根据比例值计算得出适合文档的最大EMU数值
width = Integer.valueOf(f.format(width/d).replace(",",""));
height = Integer.valueOf(f.format(height/d).replace(",",""));
}
3,完整代码方法示例:
/**
*批量插入多张图片,一张一行
*urls是以“,”为分隔的多张图片拼接的字符串
*/
public void setCellImg(XWPFTableCell cell, String urls) throws Exception {
try{
String[] url = urls.split(",");
//获取单元格的段落
XWPFParagraph paragraphs = cell.getParagraphs().get(0);
XWPFRun run = paragraphs.getRuns().isEmpty() ? paragraphs.createRun() : paragraphs.getRuns().get(0);
int index=0;
for(String path:url){
File image = new File(path);
//判断图片是否存在
if(!image.exists()){
continue;
}
//判断图片的格式
int format=0;
if (path.endsWith(".emf")) {
format = XWPFDocument.PICTURE_TYPE_EMF;
} else if (path.endsWith(".wmf")) {
format = XWPFDocument.PICTURE_TYPE_WMF;
} else if (path.endsWith(".pict")) {
format = XWPFDocument.PICTURE_TYPE_PICT;
} else if (path.endsWith(".jpeg") || path.endsWith(".jpg")) {
format = XWPFDocument.PICTURE_TYPE_JPEG;
} else if (path.endsWith(".png")) {
format = XWPFDocument.PICTURE_TYPE_PNG;
} else if (path.endsWith(".dib")) {
format = XWPFDocument.PICTURE_TYPE_DIB;
} else if (path.endsWith(".gif")) {
format = XWPFDocument.PICTURE_TYPE_GIF;
} else if (path.endsWith(".tiff")) {
format = XWPFDocument.PICTURE_TYPE_TIFF;
} else if (path.endsWith(".eps")) {
format = XWPFDocument.PICTURE_TYPE_EPS;
} else if (path.endsWith(".bmp")) {
format = XWPFDocument.PICTURE_TYPE_BMP;
} else if (path.endsWith(".wpg")) {
format = XWPFDocument.PICTURE_TYPE_WPG;
} else {
log.error("Unsupported picture: " + url +
". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
continue;
}
//获取图片文件流
FileInputStream is = new FileInputStream(path);
//计算适合文档宽高的图片EMU数值
BufferedImage read = ImageIO.read(image);
int width = Units.toEMU(read.getWidth());
int height = Units.toEMU(read.getHeight());
//1 EMU = 1/914400英寸= 1/36000 mm,15是word文档中图片能设置的最大宽度cm
if(width/360000>15){
NumberFormat f = NumberFormat.getNumberInstance();
f.setMaximumFractionDigits(0);
f.setRoundingMode(RoundingMode.UP);
Double d=width/360000/15d;
width = Integer.valueOf(f.format(width/d).replace(",",""));
height = Integer.valueOf(f.format(height/d).replace(",",""));
}
run.addPicture(is, format, image.getName(), width, height);
is.close();
if(index!=url.length-1){
run.addBreak();//换行
}
index++;
}
}catch (Exception e){
throw e;
}
}