我写了一个利用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();
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();
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货