要控制图片的大小,可以修改以下代码:

if (dataKeys[cellNum].equals("images")) {
    JSONArray images = temp.optJSONArray("images");
    if (images != null) {
        int pictureWidth = 75; // 设置图片宽度(单位:像素)
        int pictureHeight = 75; // 设置图片高度(单位:像素)
        int xOffset = 5; // 图片之间的横向偏移量
        int yOffset = 5; // 图片之间的纵向偏移量
        
        // 创建一个合并单元格,用于存储多张图片
        CellRangeAddress mergedRegion = new CellRangeAddress(i + rowcount, i + rowcount, cellNum, cellNum + images.length() - 1);
        sheet.addMergedRegion(mergedRegion);
        
        for (int j = 0; j < images.length(); j++) {
            String imageUrl = images.optString(j);
            URL imageUrlObj = new URL(imageUrl);
            InputStream imageStream = imageUrlObj.openStream();
            byte[] byteArray = IOUtils.toByteArray(imageStream);
            int pictureIndex = wb.addPicture(byteArray, Workbook.PICTURE_TYPE_JPEG);

            XSSFClientAnchor anchor = new XSSFClientAnchor(
                xOffset, yOffset, xOffset + pictureWidth, yOffset + pictureHeight,
                (short) cellNum + j, i + rowcount, (short) (cellNum + j), i + rowcount
            );

            XSSFPicture picture = patriarch.createPicture(anchor, pictureIndex);
            picture.resize(pictureWidth, pictureHeight); // 调整图片大小

            xOffset += pictureWidth + 5; // 更新下一张偏移量
        }
    }
}

在上述代码中,pictureWidthpictureHeight分别表示图片的宽度和高度,可以根据需要进行调整

if dataKeyscellNumequalsimages JSONArray images = tempoptJSONArrayimages; if images != null int pictureWidth = 75; 设置图片宽度单位:像素 int pictureHeight = 75; 设置图片高度单位:像素 int xO

原文地址: https://www.cveoy.top/t/topic/iYmV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录