-3

使用 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();
4

1 回答 1

0

您的代码使用 XSSFWorkbook 来读取和写入 .xls 文件。XSSFWorkbook 适用于 .xlsx 文件。使用 HSSFWorkbook。

于 2013-10-03T22:42:10.807 回答