package org.jeecg.modules.adweb.common.util;

import org.apache.poi.ss.usermodel.Sheet;

public class AdaptiveWidthUtils {
    /**
     * 自适应宽度(中文支持)
     *
     * @param sheet sheet
     * @param size  因为for循环从0开始，size值为 列数-1
     */
    public static void setSizeColumn(Sheet sheet, int size) {
        for (int i = 0; i < size; i++) {
            int orgWidth = sheet.getColumnWidth(i);
            sheet.autoSizeColumn(i, true);
            int newWidth = (int) (sheet.getColumnWidth(i) + 100);
            int maxWith = 256 * 255;
            //限制下最大宽度
            if (newWidth > maxWith) {
                sheet.setColumnWidth(i, maxWith);
            } else sheet.setColumnWidth(i, Math.max(newWidth, orgWidth));
        }
    }
}
