利用POI中的HSSF向excel中写图片的问题

sundream sundream 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我写了一个利用POI中的HSSF向excel中写图片的方法,但是.jpg文件始终无法写入,而且会导致生成的excel文件有问题。请高手帮忙找出原因。多谢。

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

HSSFClientAnchor a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 0, (short) 1, 0 );
HSSFShapeGroup group = patriarch.createGroup( a );
group.setCoordinates( 0, 0, 80 * 4 , 12 * 23 );
float verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet) / (float)Math.abs(group.getY2() - group.getY1());
EscherGraphics g = new EscherGraphics( group, wb, null, verticalPointsPerPixel );
g.drawOval(0,0,1023,255);
g.drawLine(0,0,1023,255);

File _file = new File("c:/サンプル.jpg"); //读入文件
Image src = javax.imageio.ImageIO.read(_file); //构造Image对象
int wideth=src.getWidth(null); //得到源图宽
int height=src.getHeight(null); //得到源图长
g.drawImage(src,0,0,wideth,height,null); //绘制缩小后的图
FileOutputStream fileOut = new FileOutputStream("ShapesandGraphics2d9.xls");
wb.write(fileOut);
fileOut.close();
g.dispose();
...全文
评论 3
收藏 已收藏
分享
返回顶部
sundream
Java SE 62,620 成员 307,251 内容