使用 Apache POI,我可以将图像添加到没有图表的工作表中,但它不能添加到有图表的工作表中
问题:不可能用excel打开输出文件!
为什么不?有解决办法吗?
InputStream inputStreamXls = new FileInputStream("c:\\temp\\template.xls");
XSSFWorkbook workbook = new XSSFWorkbook(inputStreamXls);
XSSFSheet sheet = workbook.getSheetAt(0);
inputStreamXls.close();
InputStream inputStreamImg = new FileInputStream("c:\\temp\\logo.png");
byte[] bytes = IOUtils.toByteArray(inputStreamImg);
int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
inputStreamImg.close();
CreationHelper helper = workbook.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(x);
anchor.setRow1(y);
Picture pict = drawing.createPicture(anchor, pictureIdx);
// save workbook
String file = "c:\\temp\\output_file.xls";
FileOutputStream fileOut = new FileOutputStream(file);
workbook.write(fileOut);
fileOut.close();