poi word操作之向单元格内添加图片

该博客详细介绍了如何使用 Apache POI 库在 Word 文档中向单元格内添加图片,并动态调整图片大小以适应文档宽度。主要步骤包括计算图片的EMU单位宽高,根据文档最大宽度限制进行等比缩放,并提供了完整的 Java 代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
	}
}
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值