我用Java操作Excel导入图片出现以下问题:
首先是用HSSF操作的
部分代码如下:
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(new File(excelPath)));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet1 = wb.getSheet(0);
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("d:/PieChart.jpg"));
ImageIO.write(bufferImg,"jpg",byteArrayOut);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 1,1,(short)10,20);
anchor.setAnchorType(2);
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut = new FileOutputStream("d:/workbook.xls");
wb.write(fileOut);
fileOut.close();
运行后没有异常,再次打开Excel时弹出对话框,“部分数据丢失”,发现里面原有的按钮等控件都没有了,宏命令还都在。。请问这是什么问题。。
随后参考网上大神的帖子用JXL来操作图片导入
部分代码如下:
XXXX省略部分XXX
WritableSheet ws = wwb.getSheet(0);
File image = new File("d:/aaaa/WS001.png");
BufferedImage bi7 = ImageIO.read(image);
WritableImage wimage = new WritableImage(0,0,10,10,image);
ws.addImage(wimage);
workbook.write();
workbook.close();
这个代码运行时,在ws.addImage(wimage)会出common.Assert.verify的异常
因为公司电脑代码不能复制,上述代码如有字母符号错误就不要指出了,谢谢。
求大神帮忙解决~~
------解决方案--------------------
改为fileOut = new FileOutputStream("d:/workbook.xls",true);