POI 自定义16进制颜色导出

来源:互联网 发布:mac用的office 编辑:程序博客网 时间:2024/06/12 20:39

今天导入一个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;    }
原创粉丝点击