PoiUtils.java 8.3 KB
package com.bsth.service.schedule.utils;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.WorkbookUtil;
import org.apache.poi.xssf.usermodel.*;

import java.awt.Color;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * POI操作的until方法。
 */
public class PoiUtils {
    /**
     * 使用poi读取cell中的值
     * @param cell
     * @return
     */
    public static String getStringValueFromCell(Cell cell) {
        SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd");
        DecimalFormat decimalFormat = new DecimalFormat("#.#");
        String cellValue = "";
        if(cell == null) {
            return cellValue;
        }
        else if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
            cellValue = cell.getStringCellValue();
        }

        else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
            if(HSSFDateUtil.isCellDateFormatted(cell)) {
                double d = cell.getNumericCellValue();
                Date date = HSSFDateUtil.getJavaDate(d);
                cellValue = sFormat.format(date);
            }
            else {
                cellValue = decimalFormat.format((cell.getNumericCellValue()));
            }
        }
        else if(cell.getCellType() == Cell.CELL_TYPE_BLANK) {
            cellValue = "";
        }
        else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
            cellValue = String.valueOf(cell.getBooleanCellValue());
        }
        else if(cell.getCellType() == Cell.CELL_TYPE_ERROR) {
            cellValue = "";
        }
        else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
            cellValue = cell.getCellFormula().toString();
        }
        return cellValue;
    }

    public static XSSFCell createStringXSSFCell(
            XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, String value,
            Color backgroundColor) {
        return createXSSFCell(
                xssfWorkbook, xssfRow, column,
                value, XSSFCell.CELL_TYPE_STRING,
                HorizontalAlignment.CENTER, VerticalAlignment.CENTER,
                BorderStyle.MEDIUM, new Color(0xdedede),
                (short) 13, new Color(0x2765A7), "宋体",
                backgroundColor, FillPatternType.SOLID_FOREGROUND
        );
    }

    public static XSSFCell createStringXSSFCell(
            XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, String value) {
        return createXSSFCell(
            xssfWorkbook, xssfRow, column,
                value, XSSFCell.CELL_TYPE_STRING,
                HorizontalAlignment.CENTER, VerticalAlignment.CENTER,
                BorderStyle.MEDIUM, new Color(0xdedede),
                (short) 13, new Color(0x2765A7), "宋体",
                new Color(0xffffff), FillPatternType.SOLID_FOREGROUND
        );
    }

    public static XSSFCell createDoubleXSSFCell(
            XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, Double value) {
        return createXSSFCell(
                xssfWorkbook, xssfRow, column,
                value, XSSFCell.CELL_TYPE_NUMERIC,
                HorizontalAlignment.CENTER, VerticalAlignment.CENTER,
                BorderStyle.MEDIUM, new Color(0xdedede),
                (short) 13, new Color(0x2765A7), "宋体",
                new Color(0xffffff), FillPatternType.SOLID_FOREGROUND
        );
    }

    public static XSSFCell createIntegerXSSFCell(
            XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, Integer value) {
        return createXSSFCell(
                xssfWorkbook, xssfRow, column,
                value, XSSFCell.CELL_TYPE_NUMERIC,
                HorizontalAlignment.CENTER, VerticalAlignment.CENTER,
                BorderStyle.MEDIUM, new Color(0xdedede),
                (short) 13, new Color(0x2765A7), "宋体",
                new Color(0xffffff), FillPatternType.SOLID_FOREGROUND
        );
    }

    public static XSSFCell createBlankXSSFCell(
            XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column) {
        return createXSSFCell(
                xssfWorkbook, xssfRow, column,
                null, XSSFCell.CELL_TYPE_BLANK,
                HorizontalAlignment.CENTER, VerticalAlignment.CENTER,
                BorderStyle.MEDIUM, new Color(0xdedede),
                (short) 13, new Color(0x2765A7), "宋体",
                new Color(0xffffff), FillPatternType.SOLID_FOREGROUND
        );
    }

    public static XSSFCell setIntegerStyleXSSFCellStyle(XSSFWorkbook xssfWorkbook, XSSFCell xssfCell) {
        CreationHelper creationHelper = xssfWorkbook.getCreationHelper();
        XSSFCellStyle xssfCellStyle = xssfCell.getCellStyle();
        xssfCellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("0"));
        return xssfCell;
    }
    public static XSSFCell setDoubleStyleXSSFCellStyle(XSSFWorkbook xssfWorkbook, XSSFCell xssfCell) {
        CreationHelper creationHelper = xssfWorkbook.getCreationHelper();
        XSSFCellStyle xssfCellStyle = xssfCell.getCellStyle();
        xssfCellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("0.00"));
        return xssfCell;
    }

    public static XSSFCell createXSSFCell(
            XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column,
            Object value, int valueType,
            HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment,
            BorderStyle borderStyle, Color borderColor,
            short fontSize, Color fontColor, String fontName,
            Color backgroudColor, FillPatternType fillPatternType) {
        CreationHelper creationHelper = xssfWorkbook.getCreationHelper();

        // 1、创建单元格对象
        XSSFCell cell = xssfRow.createCell(column);

        // 2、设定样式
        XSSFCellStyle cellStyle = xssfWorkbook.createCellStyle();

        // 设定值
        if (valueType == XSSFCell.CELL_TYPE_STRING) {
            cell.setCellValue(creationHelper.createRichTextString(
                    WorkbookUtil.createSafeSheetName(String.valueOf(value))));
        } else if (valueType == XSSFCell.CELL_TYPE_NUMERIC) {
            if (value instanceof Date) { // 日期
                cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-mm-dd"));
                cell.setCellValue((Date) value);
            } else if (value instanceof Double) {
                cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("0.00"));
                cell.setCellValue((Double) value);
            } else if (value instanceof Integer) {
                cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("0"));
                cell.setCellValue(Double.valueOf(value.toString()));
            } else {
                throw new RuntimeException("只支持 Date Double Integer 单元格类型");
            }
        } else if (valueType == XSSFCell.CELL_TYPE_BLANK) {
            cell.setCellType(Cell.CELL_TYPE_BLANK);
        }

        else {
            throw new RuntimeException("暂时不支持字符串、日期、数字以为的类型");
        }

        // 对齐方式
        cellStyle.setAlignment(horizontalAlignment);
        cellStyle.setVerticalAlignment(verticalAlignment);

        // 边框样式
        cellStyle.setBorderTop(borderStyle);
        cellStyle.setTopBorderColor(new XSSFColor(borderColor));
        cellStyle.setBorderBottom(borderStyle);
        cellStyle.setBottomBorderColor(new XSSFColor(borderColor));
        cellStyle.setBorderLeft(borderStyle);
        cellStyle.setLeftBorderColor(new XSSFColor(borderColor));
        cellStyle.setBorderRight(borderStyle);
        cellStyle.setRightBorderColor(new XSSFColor(borderColor));

        // 字体颜色
        XSSFFont font = xssfWorkbook.createFont();
        font.setColor(new XSSFColor(fontColor));
        font.setFontHeightInPoints(fontSize);
        font.setFontName(fontName);
        cellStyle.setFont(font);


        // 单元背景色
        cellStyle.setFillForegroundColor(new XSSFColor(backgroudColor));
        cellStyle.setFillPattern(fillPatternType);

        // TODO

        cell.setCellStyle(cellStyle);
        return cell;
    }

}