FileOutputStream fileOut = null;
BufferedImage bufferImg = null;
//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
try {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
File path1 = new File(ResourceUtils.getURL("classpath:").getPath());
String outPutFilePath = path1.getAbsolutePath()+"/static/custom/img/xxx.png";
bufferImg = ImageIO.read(new File(outPutFilePath));
ImageIO.write(bufferImg, "png", byteArrayOut);
//画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
//anchor主要用于设置图片的属性
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 0, 0, (short) 2, 1);
anchor.setAnchorType(3);
//插入图片
patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
} catch (Exception e) {
e.printStackTrace();
}finally {
if (fileOut != null) {
try {
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java导出excle导出图片
最新推荐文章于 2022-08-19 17:20:42 发布