我正在使用 Java 和 apache poi 将图像添加到 Excel 工作簿。小尺寸图像没有问题。当我尝试添加大尺寸的图像时。日志显示非法参数异常。我认为这是因为,poi 无法为大图像放置锚。任何人都知道它在什么尺寸或什么分辨率下失败?我正在使用大小为 1600x1200 的 .png 图像执行此操作。就像我提到的,这种方法适用于 100x100 像素的小图像。以前有人遇到过这个问题吗?如何绕过它。我需要插入高分辨率的图像。图像应跨越大部分 excel 文档。这是我得到的错误消息:
严重:null java.lang.IllegalArgumentException:在 org.apache.poi.hssf.usermodel.HSSFClientAnchor 的 org.apache.poi.hssf.usermodel.HSSFClientAnchor.checkRange(HSSFClientAnchor.java:245) 处的 col2 必须介于 0 和 255 之间。 setCol2(HSSFClientAnchor.java:136) 在 org.apache.poi.hssf.usermodel.HSSFPiture.resize(HSSFPiture.java:100) 在 org.apache.poi.hssf.usermodel.HSSFPiture.resize(HSSFPicture.java:119)
我正在使用以下代码来做到这一点:
private HSSFSheet insertImageWithFilename(Workbook workbook, HSSFSheet sheet, int col, int row,double scaling, String fileName)
{
try
{
StringBuilder builder = new StringBuilder(45);
builder.append(Configuration.getRoot().getInstallation().getDirectory());
builder.append(File.separator).append(Configuration.getRoot().getInstallation().getPortalName());
builder.append(File.separator).append(IMAGE_PATH);
builder.append(File.separator).append(fileName);
InputStream is = new FileInputStream(builder.toString());
byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
CreationHelper helper = workbook.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(col);
anchor.setRow1(row);
anchor.setAnchorType(2);
Picture pict = drawing.createPicture(anchor, pictureIdx);
pict.resize();
pict.resize(scaling);
}
catch (FileNotFoundException fnfe)
{
System.out.println("FileNotFoundException:" + fnfe.getMessage());
}
catch (IOException ioe)
{
System.out.println("IOException:" + ioe.getMessage());
}
return sheet;
}
编辑:缩放有这个值:0.1d。感谢您的回复。