600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Java 调用 Apache POI 往 Excel 插入图片

Java 调用 Apache POI 往 Excel 插入图片

时间:2024-06-21 15:25:58

相关推荐

Java 调用 Apache POI 往 Excel 插入图片

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 讨论

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。