Java 中,调用 Apache 的 POI 操作 Excel,往 Excel 中插入一张图片。
//create a new workbookWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();//add picture data to this workbook.// 打开图片InputStream is = new FileInputStream("image1.jpeg");byte[] bytes = IOUtils.toByteArray(is);// 增加图片到 Workbookint pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);is.close();CreationHelper helper = wb.getCreationHelper();//create sheetSheet sheet = wb.createSheet();// Create the drawing patriarch. This is the top level container for all shapes.Drawing drawing = sheet.createDrawingPatriarch();//add a picture shapeClientAnchor anchor = helper.createClientAnchor();//set top-left corner of the picture,//subsequent call of Picture#resize() will operate relative to it// 设置图片位置anchor.setCol1(3);anchor.setRow1(2);Picture pict = drawing.createPicture(anchor, pictureIdx);//auto-size picture relative to its top-left cornerpict.resize();//save workbookString file = "picture.xls";if(wb instanceof XSSFWorkbook) file += "x";// 输出文件FileOutputStream fileOut = new FileOutputStream(file);wb.write(fileOut);fileOut.close();
另一种坐标方法可以参考/joyous/article/details/9664739
本文链接:/joyous/article/details/8780112
Q群 23601 讨论