我在使用 Apache POI(版本 3.7)将图片插入 Excel 工作簿时遇到问题。这是我的代码:
private static void createAndFillWorkbook() {
FileOutputStream out = null;
FileInputStream in = null;
try {
HSSFWorkbook workbook = new HSSFWorkbook();
File picture = new File("D:\\pngPict.png");
byte[] buf = new byte[(int) picture.length()];
in = new FileInputStream(picture);
in.read(buf);
workbook.addPicture(buf, Workbook.PICTURE_TYPE_PNG);
out = new FileOutputStream("D:\\Book3.xls");
workbook.write(out);
} catch (Exception e) {
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
它不起作用(我使用的是 .xls 文件格式),我不知道为什么。