【POI】Java设置Excel(.xlsx)单元格属性

5 篇文章 1 订阅

使用过的几种样式的设置方式:

XXXServiceImpl.java

public class XXXServiceImpl implements XXXService {

    @Override
    public void test() {
        Map<String, Object> styleMap = new HashMap<String, Object>();

        styleMap.put("backgroundColor", new XSSFColor(new Color(0, 0, 0)));
//        styleMap.put("backgroundColor", IndexedColors.WHITE.getIndex());

        styleMap.put("fontColor", new XSSFColor(new Color(255, 255, 255)));

        styleMap.put("fontName", "MS Pゴシック");
//        styleMap.put("fontName", "Meiryo");

        styleMap.put("fontSize", (short) 10);
        styleMap.put("fontBold", true);
        styleMap.put("isHorizontalAlignCenter", true);

        // 细线
        styleMap.put("borderTopStyle", BorderStyle.THIN);
//        // 粗线
//        styleMap.put("borderTopStyle", BorderStyle.MEDIUM);
//        // 虚线
//        styleMap.put("borderTopStyle", BorderStyle.HAIR);

        styleMap.put("borderTopColor", new XSSFColor(new Color(0, 0, 0)));

        styleMap.put("dataFomrat", "#,##0");
//        styleMap.put("dataFomrat", "0%");
//        styleMap.put("dataFomrat", "0.00%");
//        styleMap.put("dataFomrat", "¥#,##0.0");
//        styleMap.put("dataFomrat", "¥#,##0");
    }
}

XSSFUtils.java

public class XSSFUtils {

    /**
     * セルの外観を設定する
     *
     * @param workbook
     * @param cell
     * @param styleMap
     *              backgroundColor             XSSFColor
     *              fontColor                   XSSFColor
     *              fontName                    String
     *              fontSize                    Number
     *              fontBold                    boolean
     *              isHorizontalAlignCenter     boolean
     *              borderTopStyle              BorderStyle
     *              borderBottomStyle           BorderStyle
     *              borderLeftStyle             BorderStyle
     *              borderRightStyle            BorderStyle
     *              borderTopColor              XSSFColor
     *              borderBottomColor           XSSFColor
     *              borderLeftColor             XSSFColor
     *              borderRightColor            XSSFColor
     *              dataFomrat                  String
     */
    public static void clearAndSetCellStyle(XSSFWorkbook workbook, XSSFCell cell, Map<String, Object> styleMap) {

        XSSFCellStyle cellStyle = workbook.createCellStyle();

        if (styleMap.get("backgroundColor") != null) {
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            cellStyle.setFillForegroundColor((XSSFColor) styleMap.get("backgroundColor"));
        }

        XSSFFont font = workbook.createFont();
        if (styleMap.get("fontName") != null) {
            font.setFontName(styleMap.get("fontName").toString());
        }
        if (styleMap.get("fontColor") != null) {
            font.setColor((XSSFColor) styleMap.get("fontColor"));
        }
        if (styleMap.get("fontBold") != null) {
            font.setBold((boolean) styleMap.get("fontBold"));
        }
        if (styleMap.get("fontSize") != null) {
            font.setFontHeightInPoints(((Number) styleMap.get("fontSize")).shortValue());
        }
        cellStyle.setFont(font);

        if (styleMap.get("isHorizontalAlignCenter") != null) {
            boolean isHorizontalAlignCenter = (boolean) styleMap.get("isHorizontalAlignCenter");
            if (isHorizontalAlignCenter) {
                cellStyle.setAlignment(HorizontalAlignment.CENTER);
            }
        }
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        if (styleMap.get("borderTopStyle") != null) {
            cellStyle.setBorderTop((BorderStyle) styleMap.get("borderTopStyle"));
        }
        if (styleMap.get("borderBottomStyle") != null) {
            cellStyle.setBorderBottom((BorderStyle) styleMap.get("borderBottomStyle"));
        }
        if (styleMap.get("borderLeftStyle") != null) {
            cellStyle.setBorderLeft((BorderStyle) styleMap.get("borderLeftStyle"));
        }
        if (styleMap.get("borderRightStyle") != null) {
            cellStyle.setBorderRight((BorderStyle) styleMap.get("borderRightStyle"));
        }

        if (styleMap.get("borderTopColor") != null) {
            cellStyle.setTopBorderColor((XSSFColor) styleMap.get("borderTopColor"));
        }
        if (styleMap.get("borderBottomColor") != null) {
            cellStyle.setBottomBorderColor((XSSFColor) styleMap.get("borderBottomColor"));
        }
        if (styleMap.get("borderLeftColor") != null) {
            cellStyle.setLeftBorderColor((XSSFColor) styleMap.get("borderLeftColor"));
        }
        if (styleMap.get("borderRightColor") != null) {
            cellStyle.setRightBorderColor((XSSFColor) styleMap.get("borderRightColor"));
        }

        if (styleMap.get("dataFomrat") != null) {
            cellStyle.setDataFormat(workbook.createDataFormat().getFormat(styleMap.get("dataFomrat").toString()));
            cell.setCellStyle(cellStyle);
        }

        cell.setCellStyle(cellStyle);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值