POI导出图片到Excel不生效 (2007以上版本)

现在网上有很多的POI导出方法,但是在导出时一直不能导出成功,所以考虑了下有可能是因为版本原因导致的。

大部分教程使用的都是 HSSFWorkbook,这个方法只能操作2003以下的版本,操作2007以上版本需要使用XSSFWorkbook

更改了方法之后,导出图片成功,以下是一些核心代码

        Workbook wb = new XSSFWorkbook();
        CreationHelper helper = wb.getCreationHelper();
        Sheet sheet = wb.createSheet();
        Drawing drawing = sheet.createDrawingPatriarch();


        for (int i = 0; i <productToolList.size() ; i++) {//需要导出的数据列表

            String productImage = productToolList.get(i).getProductImage();
            
            InputStream is = new FileInputStream("图片路径");
            byte[] bytes = IOUtils.toByteArray(is);
            int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
            is.close();


            ClientAnchor anchor = helper.createClientAnchor();
            
            anchor.setDx1(0);
            anchor.setDy1(0);
            anchor.setDx2(0);
            anchor.setDy2(0);
            anchor.setCol1(0);
            anchor.setRow1(i);
            anchor.setCol2(1);
            anchor.setRow2(i+1);

            Picture pict = drawing.createPicture(anchor, pictureIdx);


            Row row =  sheet.createRow(i);

            row.createCell(1).setCellValue(productToolList.get(i).getProductSku());
            row.createCell(2).setCellValue(productToolList.get(i).getProductName());   
       row.createCell(3).setCellValue(productToolList.get(i).getProductNum().toString());
            
        }



        OutputStream fileOut = new FileOutputStream("需要导出的路径");
        wb.write(fileOut);

        fileOut.close();

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作为AI语言模型,我无法进行POI库的开发和使用,但是我可以提供Java代码的参考: 1. 导出图片Excel ```java // 创建Excel文档 Workbook workbook = new XSSFWorkbook(); // 创建工作表 Sheet sheet = workbook.createSheet("图片示例"); // 加载图片 InputStream inputStream = new FileInputStream("image.jpg"); byte[] bytes = IOUtils.toByteArray(inputStream); int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG); // 插入图片 CreationHelper helper = workbook.getCreationHelper(); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = helper.createClientAnchor(); anchor.setCol1(0); anchor.setRow1(0); Picture picture = drawing.createPicture(anchor, pictureIdx); // 输出Excel文件 FileOutputStream fos = new FileOutputStream("excel.xlsx"); workbook.write(fos); ``` 2. 压缩图片 ```java // 加载图片 InputStream inputStream = new FileInputStream("image.jpg"); byte[] bytes = IOUtils.toByteArray(inputStream); // 压缩图片 BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(bytes)); int width = bufferedImage.getWidth(); int height = bufferedImage.getHeight(); double scale = 0.5; width *= scale; height *= scale; BufferedImage compressedImage = new BufferedImage(width, height, bufferedImage.getType()); Graphics2D g = compressedImage.createGraphics(); g.drawImage(bufferedImage, 0, 0, width, height, null); g.dispose(); // 输出压缩后的图片 ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(compressedImage, "jpg", baos); byte[] compressedBytes = baos.toByteArray(); FileOutputStream fos = new FileOutputStream("compressed.jpg"); fos.write(compressedBytes); ``` 你可以将以上代码结合起来,先将图片压缩,再将压缩后的图片插入Excel中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值