Apache Poi在excel工作簿中插入大图像

时间:2013-05-31 07:24:52

标签: java image excel apache-poi

我正在使用Java和apache poi将图像添加到excel工作簿。小尺寸图像没有问题。当我尝试添加大尺寸的图像时。该日志显示非法参数异常。我认为这是因为,poi无法为大图像设置锚点。谁知道它失败了什么尺寸或分辨率?我这样做的大小为1600x1200的.png图像。就像我之前提到的,这种方法适用于小图像,适用于100x100像素。以前有人有这个问题吗?如何解决它。我需要插入高分辨率的图像。图像应该跨越大多数excel文档。这是我得到的错误消息:

严重:空 java.lang.IllegalArgumentException:col2必须介于0到255之间     在org.apache.poi.hssf.usermodel.HSSFClientAnchor.checkRange(HSSFClientAnchor.java:245)     在org.apache.poi.hssf.usermodel.HSSFClientAnchor.setCol2(HSSFClientAnchor.java:136)     在org.apache.poi.hssf.usermodel.HSSFPicture.resize(HSSFPicture.java:100)     在org.apache.poi.hssf.usermodel.HSSFPicture.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。谢谢你的回复。

0 个答案:

没有答案