POI 自定义16进制颜色导出

今天导入一个Excel 要求什么格式导入就怎么显示,包括字体 颜色,还要怎么导出。这个poi导入的是16进制颜色,导出比较麻烦。具体参考以下代码吧

    private CellStyle createStyle(Workbook wb, ExcelCellStyle excelCellStyle) {
        if(styleMap.get(excelCellStyle.getBgColor())!=null)
            return styleMap.get(excelCellStyle.getBgColor());
        CellStyle style = wb.createCellStyle();
        Font titleFont = wb.createFont();
        //titleFont.setFontHeightInPoints((short)48);
        //titleFont.setColor(IndexedColors.DARK_BLUE.getIndex());
        style = wb.createCellStyle();
        String colorStr = excelCellStyle.getBgColor();
        if (colorStr.length() > 6)
            colorStr = colorStr.substring(2);
        HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette();//replacing the standard red with freebsd.org red
        palette.setColorAtIndex((short) 9,
                (byte) convertHexToNumber(colorStr.substring(0, 2)),//RGB red
                (byte) convertHexToNumber(colorStr.substring(2, 4)),//RGB green
                (byte) convertHexToNumber(colorStr.substring(4, 6)) //RGB blue
        );

        style.setFillForegroundColor((short) 9);
        style.setFillPattern((short) 1);
        //style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setFont(titleFont);
        styleMap.put(excelCellStyle.getBgColor(), style);
        return style;
    }
    private int convertHexToNumber(String hex) {
        int num = 0;
        try {
            num = Integer.parseInt(hex, 16);
        } catch (Throwable t) {
            logger.error("数字转换异常 " + t);
        }
        return num;
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值