java POI Excel插入图片

先看下效果:

 

 效果:

在三行两列分别插入文字和图片:第一列纯文本,第二列是对应的图片。

下面上代码:

 /**
     * This Method loads the image from application resource and insert into the
     * Cell
     */
    private static void insertImageToCell(Workbook workbook, String fileType, int rowNum, Drawing drawing,
                                          String imageName) throws IOException {

        //Loading image from application resource
        InputStream is = new FileInputStream(imageName); //ReportBase.class.getClassLoader().getResourceAsStream(imageName);

        //Converting input stream into byte array
        byte[] inputImageBytes = IOUtils.toByteArray(is);
        int inputImagePictureID = workbook.addPicture(inputImageBytes, Workbook.PICTURE_TYPE_PNG);
        is.close();

        ClientAnchor anchor = null;

        //Creating the client anchor based on file format
        if (fileType.equals("xls")) {
            anchor = new HSSFClientAnchor();
        } else {
            anchor = new XSSFClientAnchor();
        }
        anchor.setCol1(1);
        anchor.setCol2(2);
        anchor.setRow1(rowNum - 1);
        anchor.setRow2(rowNum);

        drawing.createPicture(anchor, inputImagePictureID);
    }
private  static void  testExcel(){
        Workbook workbook = null;
        FileOutputStream fos = null;
        try {
            File file = new File("E:/company-logo.xlsx");
            String fileType = null;

            // Creating workbook object based on excel file format
            if (file.getName().endsWith(".xls")) {
                workbook = new HSSFWorkbook();
                fileType = "xls";
            } else if (file.getName().endsWith(".xlsx")) {
                workbook = new XSSFWorkbook();
                fileType = "xlsx";
            } else {
                System.err.println("File format should be XLS or XLSX only.");
                return;
            }

            Sheet sheet =workbook.createSheet("Company Logo");

            // Creating drawing object
            Drawing drawing = sheet.createDrawingPatriarch();

            //Adding first row data
            Row row1 = sheet.createRow(0);
            row1.setHeight((short) 1000);
            row1.createCell(0).setCellValue("Apple");
            insertImageToCell(workbook, fileType, 1, drawing, "E:\\Pictures\\88888.png");

            //Adding second row data
            Row row2 = sheet.createRow(1);
            row2.setHeight((short) 1000);
            row2.createCell(0).setCellValue("Google");
            insertImageToCell(workbook, fileType, 2, drawing, "E:\\Pictures\\88888.png");

            //Adding third row data
            Row row3 = sheet.createRow(2);
            row3.setHeight((short) 1000);
            row3.createCell(0).setCellValue("Facebook");
            insertImageToCell(workbook, fileType, 3, drawing, "E:\\Pictures\\88888.png");

            // Resize all columns to fit the content size
            for (int i = 0; i < 3; i++) {
                sheet.autoSizeColumn(i);
            }

            // Write the workbook to the excel file
            fos = new FileOutputStream(file);
            workbook.write(fos);
            System.out.println("Images have been added successfully.");

        } catch (IOException e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霍先生的虚拟宇宙网络

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值