java poi 导出Excel单元格等比例缩放内边距


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Units;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author wency
 * @description: Excel导出处理图片
 **/
public class ExcelHandleImageUtils {

    /**
     * @param inputStream 图片转换的输入流
     * @param sheet
     * @param workbook
     * @param col         图片所在的列
     * @param row         图片所在的行
     * @throws IOException
     */
    public static void excelHandleImage(InputStream inputStream, XSSFSheet sheet, XSSFWorkbook workbook, int col, int row) throws IOException {
        float rowHeightInPoints = 54f;

        float rowHeightInPixels = rowHeightInPoints * Units.PIXEL_DPI / Units.POINT_DPI;

        sheet.setColumnWidth(col, 26 * 256); // 50 default characters width
        float colWidthInPixels = sheet.getColumnWidthInPixels(col);


        byte[] imageBytes = IOUtils.toByteArray(inputStream);
        BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageBytes));


        if (img == null || img.getWidth(null) <= 0
                || img.getHeight(null) <= 0) {
            throw new RuntimeException("is not img");
        }
        int pictureureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_JPEG);

        inputStream.close();

        CreationHelper helper = workbook.getCreationHelper();
        Drawing drawing = sheet.createDrawingPatriarch();
        ClientAnchor anchor = helper.createClientAnchor();

        //set start position of picture's anchor to top left B2
        anchor.setRow1(row);
        anchor.setCol1(col);

        //create picture
        Picture pict = drawing.createPicture(anchor, pictureureIdx);

        //get picture's original size
//        int pictOriginalWidthInPixels = pict.getImageDimension().width;
//        int pictOriginalHeightInPixels = pict.getImageDimension().height;

        //set picture's wanted size
        float pictWidthInCm = 3f;
        float pictWidthInPixels = pictWidthInCm / 2.54f * 72f * Units.PIXEL_DPI / Units.POINT_DPI;
        //want scaling in aspect ratio?是否需要缩放纵横比?
//        float scale = pictWidthInPixels / pictOriginalWidthInPixels;
//        float pictHeightInPixels = pictOriginalHeightInPixels * scale;
        //want explicit set height too?
        float pictHeightInCm = 1.5f;
        float pictHeightInPixels = pictHeightInCm / 2.54f * 72f * Units.PIXEL_DPI / Units.POINT_DPI;

        //calculate the horizontal center position
        int horCenterPosInPixels = Math.round(colWidthInPixels / 2f - pictWidthInPixels / 2f);
        //set the horizontal center position as Dx1 of anchor
        anchor.setDx1(horCenterPosInPixels * Units.EMU_PER_PIXEL); //in unit EMU for XSSF

        //calculate the vertical center position
        int vertCenterPosInPixels = Math.round(rowHeightInPixels / 2f - pictHeightInPixels / 2f);
        //set the vertical center position as Dy1 of anchor
        anchor.setDy1(Math.round(vertCenterPosInPixels * Units.EMU_PER_PIXEL)); //in unit EMU for XSSF

        //set end position of picture's anchor to top left B2
        anchor.setRow2(row);
        anchor.setCol2(col);

        //calculate the horizontal end position of picture
        int horCenterEndPosInPixels = Math.round(horCenterPosInPixels + pictWidthInPixels);
        //set the horizontal end position as Dx2 of anchor
        anchor.setDx2(horCenterEndPosInPixels * Units.EMU_PER_PIXEL); //in unit EMU for XSSF

        //calculate the vertical end position of picture
        int vertCenterEndPosInPixels = Math.round(vertCenterPosInPixels + pictHeightInPixels);
        //set the vertical end position as Dy2 of anchor
        anchor.setDy2(Math.round(vertCenterEndPosInPixels * Units.EMU_PER_PIXEL)); //in unit EMU for XSSF

        // 图片宽高缩放比例
        pict.resize(0.4, 0.6);

    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Image {

        /**
         * 图片默认列
         */
        private int col;
        /**
         * 图片默认行
         */
        private int row;

        private InputStream inputStream;
    }
}

posted @ 2025-04-03 15:28  程序员の奇妙冒险  阅读(46)  评论(0)    收藏  举报