java poi操做 excel行自適應高度

import java.text.SimpleDateFormat;java

import java.util.Date;apache

 

import org.apache.commons.lang3.StringUtils;字體

import org.apache.poi.hssf.usermodel.HSSFCell;ui

import org.apache.poi.hssf.usermodel.HSSFDataFormat;spa

import org.apache.poi.hssf.usermodel.HSSFDateUtil;.net

import org.apache.poi.hssf.usermodel.HSSFRow;excel

import org.apache.poi.hssf.usermodel.HSSFSheet;orm

import org.apache.poi.hssf.usermodel.HSSFWorkbook;get

import org.apache.poi.ss.usermodel.Cell;it

import org.apache.poi.ss.util.CellRangeAddress;

 

/**

 * excel行自適應高度

 * @author lw

 * @version 2019827 下午1:59:27

 */

public class AutoRowHeightUtil

{

    /**

     * 自適應excel行高

     * @author lw

     * @date 2019827 上午10:27:33

     * @param cell

     * @param margin 字體邊距

     */

    public static void autoRowHeight(HSSFCell cell, short margin)

    {

        if(cell == null)

        {

            return ;

        }

        String cellVal = getStringCellValue(cell) ;

        if(StringUtils.isBlank(cellVal))

        {

            return ;

        }

        HSSFRow row = cell.getRow();

        HSSFWorkbook workbook = row.getSheet().getWorkbook();

       

        short charHeight = cell.getCellStyle().getFont(workbook).getFontHeightInPoints();

        float charPx = charHeight / (float)72 * (float)96;//字體像素

        float cellWidthPx = getCellWidth(cell);//寬度像素

        int charLength = cellVal.length();//字符長度

        int charInCell = (int)(cellWidthPx / charPx);//每一個列(包括合併的列)的字數

        //在指定寬度的列中的字符展現行數

        int rowNumcharLength/ charInCell + (charLength % charInCell > 0 ? 1 : 0);

        //自適應以後的行高點數

        short cellHeightPx = (short) (((short)rowNum) * (charHeight + margin));

        row.setHeightInPoints(cellHeightPx);

    }

   

    /**

     * 自適應excel行高

     * @author lw

     * @date 2019827 上午10:27:33

     * @param cell

     */

    private static String getStringCellValue(Cell cell)

    {

        switch (cell.getCellType())

        {

            case HSSFCell.CELL_TYPE_FORMULA:

 

                if (cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("yyyy-MM-dd"))

                {

                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

 

                    return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));

                }

 

                return cell.getCellFormula();

            case HSSFCell.CELL_TYPE_STRING:

                return cell.getStringCellValue();

            case HSSFCell.CELL_TYPE_NUMERIC:

 

                if (HSSFDateUtil.isCellDateFormatted(cell))

                {

                    String dateStr = "";

                    int style = cell.getCellStyle().getDataFormat();

                    Date date = cell.getDateCellValue();

                    // 對不一樣格式的日期類型作不一樣的輸出,與單元格格式保持一致

                    switch (style)

                    {

                        case 178:

                            dateStr = new SimpleDateFormat("yyyy''M''d''").format(date);

                            break;

                        case 14:

                            dateStr = new SimpleDateFormat("yyyy-MM-dd").format(date);

                            break;

                        case 179:

                            dateStr = new SimpleDateFormat("yyyy/MM/dd HH:mm").format(date);

                            break;

                        case 181:

                            dateStr = new SimpleDateFormat("yyyy/MM/dd HH:mm a ").format(date);

                            break;

                        case 22:

                            dateStr = new SimpleDateFormat(" yyyy/MM/dd HH:mm:ss ").format(date);

                            break;

                        default:

                            break;

                    }

                    return dateStr;

                }

                else

                {

                    cell.setCellType(Cell.CELL_TYPE_STRING);

                }

 

                return cell.getStringCellValue();

            case HSSFCell.CELL_TYPE_BOOLEAN:

                return cell.getBooleanCellValue() ? "TRUE" : "FALSE";

            default:

                return "";

        }

    }

   

    /**

     * 獲取單元格及合併單元格的寬度

     * @author lw

     * @date 2019628 下午1:58:42

     * @param cell 單元格

     * @return 單元格及合併單元格的寬度(像素)

     */

    private static float getCellWidth(HSSFCell cell)

    {

        if(cell == null )

        {

            return 0;

        }

        HSSFSheet sheet = cell.getSheet();

        int rowIndex = cell.getRowIndex();

        int columnIndex = cell.getColumnIndex();

        float width = sheet.getColumnWidthInPixels(columnIndex);

       

        boolean isPartOfRegion = false;

        int firstColumn = 0;

        int lastColumn = 0;

        int firstRow = 0;

        int lastRow = 0;

        int sheetMergeCount = sheet.getNumMergedRegions();

        for (int i = 0; i < sheetMergeCount; i++)

        {

            CellRangeAddress ca = sheet.getMergedRegion(i);

           

            firstColumn = ca.getFirstColumn();

            lastColumn = ca.getLastColumn();

            firstRow = ca.getFirstRow();

            lastRow = ca.getLastRow();

            if (rowIndex == firstRow && rowIndex <= lastRow)

            {

                if (columnIndex == firstColumn && columnIndex <= lastColumn)

                {

                    isPartOfRegion = true;

                    break;

                }

            }

        }

        if(isPartOfRegion)

        {

            width = 0;

            for (int i = firstColumn; i <= lastColumn; i++)

            {

                width += sheet.getColumnWidthInPixels(i);

            }

        }

        return width;

    }

}

相關文章
相關標籤/搜索