import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.util.Units; import org.apache.poi.xssf.usermodel.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /** * @author loomis * @Date 2021/11/24 11:10 * @Version 1.0 */ public class test { public static void main(String[] args) { FileOutputStream fileOut = null; BufferedImage bufferImg = null;//logo ByteArrayOutputStream byteArrayOut = null; //读进图片 XSSFWorkbook wb = new XSSFWorkbook(); try { // 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray byteArrayOut = new ByteArrayOutputStream(); //将图片读到BufferedImage bufferImg = ImageIO.read(new File("C:\\Users\\Administrator\\Pictures\\20211009144818.png")); // 将图片写入流中 ImageIO.write(bufferImg, "png", byteArrayOut); XSSFSheet sheet = wb.createSheet("123"); Row row = sheet.createRow(1); Cell cell = row.createCell(1); // 设置列宽 sheet.setColumnWidth(1, 256 * 38); // 设置行高 row.setHeightInPoints((short) 20); // 指定我想要的长宽 int standardWidth = 50; int standardHeight = 25; // 计算单元格的长宽 double cellWidth = sheet.getColumnWidthInPixels(cell.getColumnIndex()); double cellHeight = cell.getRow().getHeightInPoints() / 72 * 96; // 计算需要的长宽比例的系数 double a = standardWidth / cellWidth; double b = standardHeight / cellHeight; /**anchor主要用于设置图片的属性 * 该构造函数有8个参数 * 前四个参数是控制图片在单元格的位置,分别是图片距离单元格left,top,right,bottom的像素距离 * 后四个参数,前两个表示图片左上角所在的cellNum和 rowNum,后两个参数对应的表示图片右下角所在的cellNum和 rowNum, * excel中的cellNum和rowNum的index都是从0开始的 * */ XSSFDrawing patriarch = sheet.createDrawingPatriarch(); CreationHelper helper = wb.getCreationHelper(); ClientAnchor anchor = helper.createClientAnchor(); anchor.setRow1(1); anchor.setCol1(1); anchor.setDx1(0); anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_DONT_RESIZE);//设置图片随单元移动调整大小 XSSFPicture picture = patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG)); picture.resize(a,b); ClientAnchor anchor2 = helper.createClientAnchor(); anchor2.setRow1(1); anchor2.setCol1(1); anchor2.setDx1((standardWidth-10)* Units.EMU_PER_POINT); anchor2.setDx2((standardWidth-10)* Units.EMU_PER_POINT); XSSFPicture picture2 = patriarch.createPicture(anchor2, wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG)); // 计算需要的长宽比例的系数 picture2.resize(a,b); ClientAnchor anchor3 = helper.createClientAnchor(); anchor3.setRow1(1); anchor3.setCol1(1); anchor3.setDx1((standardWidth-10)*2* Units.EMU_PER_POINT); anchor3.setDx2((standardWidth-10)*2* Units.EMU_PER_POINT); XSSFPicture picture3 = patriarch.createPicture(anchor3, wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG)); picture3.resize(a,b); ClientAnchor anchor4 = helper.createClientAnchor(); anchor4.setRow1(1); anchor4.setCol1(1); anchor4.setDx1((standardWidth-10)*3* Units.EMU_PER_POINT); anchor4.setDx2((standardWidth-10)*3* Units.EMU_PER_POINT); XSSFPicture picture4 = patriarch.createPicture(anchor4, wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG)); picture4.resize(a,b); ClientAnchor anchor5 = helper.createClientAnchor(); anchor5.setRow1(1); anchor5.setCol1(1); anchor5.setDx1((standardWidth-10)*4* Units.EMU_PER_POINT); anchor5.setDx2((standardWidth-10)*4* Units.EMU_PER_POINT); XSSFPicture picture5 = patriarch.createPicture(anchor5, wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG)); picture5.resize(a,b); //生成的excel文件地址 fileOut = new FileOutputStream("D:\\123.xlsx"); // 写入excel文件 wb.write(fileOut); } catch (Exception io) { io.printStackTrace(); System.out.println("io erorr : " + io.getMessage()); } finally { if (fileOut != null) { try { fileOut.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
POI XSSF 多张图片导出到一个单元格实现
最新推荐文章于 2024-11-15 14:22:53 发布
此代码示例演示了如何使用Apache POI库在Excel工作表中创建多个单元格,并在每个单元格中插入同一张图片,然后根据预设的长宽比例调整图片大小。通过ClientAnchor设置图片位置,resize方法调整图片尺寸,最终保存为xlsx文件。
摘要由CSDN通过智能技术生成