diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/css/CssParseServer.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/css/CssParseServer.java index f56a1df..d235d2c 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/css/CssParseServer.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/css/CssParseServer.java @@ -44,9 +44,40 @@ public class CssParseServer { parseFontAttr(mapStyle); + getBackground(mapStyle); return mapToCellStyleEntity(mapStyle); } + private CellStyleEntity mapToCellStyleEntity(Map mapStyle) { + CellStyleEntity entity = new CellStyleEntity(); + entity.setAlign(mapStyle.get(TEXT_ALIGN)); + entity.setVetical(mapStyle.get(VETICAL_ALIGN)); + entity.setBackground(getBackground(mapStyle)); + entity.setHeight(mapStyle.get(HEIGHT)); + entity.setWidth(mapStyle.get(WIDTH)); + entity.setFont(getCssStyleFontEnity(mapStyle)); + entity.setBackground(mapStyle.get(BACKGROUND_COLOR)); + // TODO 这里较为复杂,后期处理 + /*CellStyleBorderEntity border = new CellStyleBorderEntity(); + entity.setBorder(border); + border.setBorderBottom(borderBottom);*/ + return entity; + } + + private CssStyleFontEnity getCssStyleFontEnity(Map style) { + CssStyleFontEnity font = new CssStyleFontEnity(); + font.setStyle(style.get(FONT_STYLE)); + int fontSize = PoiCssUtils.getInt(style.get(FONT_SIZE)); + if (fontSize > 0) { + font.setSize(fontSize); + } + font.setWeight(style.get(FONT_WEIGHT)); + font.setFamily(style.get(FONT_FAMILY)); + font.setDecoration(style.get(TEXT_DECORATION)); + font.setColor(style.get(COLOR)); + return font; + } + private void parseFontAttr(Map mapRtn) { log.debug("Parse Font Style."); @@ -152,56 +183,26 @@ public class CssParseServer { } } - private CellStyleEntity mapToCellStyleEntity(Map mapStyle) { - CellStyleEntity entity = new CellStyleEntity(); - entity.setAlign(mapStyle.get(TEXT_ALIGN)); - entity.setVetical(mapStyle.get(VETICAL_ALIGN)); - entity.setBackground(getBackground(mapStyle)); - entity.setHeight(mapStyle.get(HEIGHT)); - entity.setWidth(mapStyle.get(WIDTH)); - entity.setFont(getCssStyleFontEnity(mapStyle)); - // TODO 这里较为复杂,后期处理 - /*CellStyleBorderEntity border = new CellStyleBorderEntity(); - entity.setBorder(border); - border.setBorderBottom(borderBottom);*/ - return entity; - } - - private CssStyleFontEnity getCssStyleFontEnity(Map style) { - CssStyleFontEnity font = new CssStyleFontEnity(); - font.setStyle(style.get(FONT_STYLE)); - int fontSize = PoiCssUtils.getInt(style.get(FONT_SIZE)); - if (fontSize > 0) { - font.setSize(fontSize); - } - font.setWeight(style.get(FONT_WEIGHT)); - font.setFamily(style.get(FONT_FAMILY)); - font.setDecoration(style.get(TEXT_DECORATION)); - font.setColor(PoiCssUtils.parseColor(style.get(COLOR))); - return font; - } - private String getBackground(Map style) { - Map mapRtn = new HashMap(); String bg = style.get(BACKGROUND); String bgColor = null; if (StringUtils.isNotBlank(bg)) { for (String bgAttr : bg.split("(?<=\\)|\\w|%)\\s+(?=\\w)")) { if ((bgColor = PoiCssUtils.processColor(bgAttr)) != null) { - mapRtn.put(BACKGROUND_COLOR, bgColor); + style.put(BACKGROUND_COLOR, bgColor); break; } } } bg = style.get(BACKGROUND_COLOR); if (StringUtils.isNotBlank(bg) && (bgColor = PoiCssUtils.processColor(bg)) != null) { - mapRtn.put(BACKGROUND_COLOR, bgColor); + style.put(BACKGROUND_COLOR, bgColor); } if (bgColor != null) { - bgColor = mapRtn.get(BACKGROUND_COLOR); - if (!"#ffffff".equals(bgColor)) { - return mapRtn.get(BACKGROUND_COLOR); + bgColor = style.get(BACKGROUND_COLOR); + if ("#ffffff".equals(bgColor)) { + style.remove(BACKGROUND_COLOR); } } return null; diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/css/impl/BackgroundCssConvertImpl.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/css/impl/BackgroundCssConvertImpl.java index 4482f51..81a4564 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/css/impl/BackgroundCssConvertImpl.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/css/impl/BackgroundCssConvertImpl.java @@ -1,23 +1,39 @@ package org.jeecgframework.poi.excel.html.css.impl; +import org.apache.commons.lang3.StringUtils; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.xssf.usermodel.XSSFCell; +import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.jeecgframework.poi.excel.html.css.ICssConvertToExcel; import org.jeecgframework.poi.excel.html.css.ICssConvertToHtml; import org.jeecgframework.poi.excel.html.entity.style.CellStyleEntity; +import org.jeecgframework.poi.util.PoiCssUtils; public class BackgroundCssConvertImpl implements ICssConvertToExcel, ICssConvertToHtml { @Override public String convertToHtml(Cell cell, CellStyle cellStyle, CellStyleEntity style) { - + return null; } @Override public void convertToExcel(Cell cell, CellStyle cellStyle, CellStyleEntity style) { + if (StringUtils.isEmpty(style.getBackground())) { + return; + } + cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); // 填充图案 + if (cell instanceof XSSFCell) { + ((XSSFCellStyle) cellStyle) + .setFillForegroundColor(PoiCssUtils.parseColor(style.getBackground())); + } else if (cell instanceof HSSFCell) { + cellStyle.setFillForegroundColor( + PoiCssUtils.parseColor((HSSFWorkbook) cell.getRow().getSheet().getWorkbook(), + style.getBackground()).getIndex()); + } } - - } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/css/impl/TextCssConvertImpl.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/css/impl/TextCssConvertImpl.java index 485a321..7743b1c 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/css/impl/TextCssConvertImpl.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/css/impl/TextCssConvertImpl.java @@ -10,9 +10,13 @@ import org.apache.poi.hssf.util.HSSFColor.BLACK; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFColor; +import org.apache.poi.xssf.usermodel.XSSFFont; import org.jeecgframework.poi.excel.html.css.ICssConvertToExcel; import org.jeecgframework.poi.excel.html.css.ICssConvertToHtml; import org.jeecgframework.poi.excel.html.entity.style.CellStyleEntity; +import org.jeecgframework.poi.util.PoiCssUtils; public class TextCssConvertImpl implements ICssConvertToExcel, ICssConvertToHtml { @@ -42,9 +46,13 @@ public class TextCssConvertImpl implements ICssConvertToExcel, ICssConvertToHtml if (StringUtils.isNotBlank(fontFamily)) { font.setFontName(fontFamily); } - int color = style.getFont().getColor(); - if (color != 0 && color != BLACK.index) { - font.setColor((short) color); + String color = style.getFont().getColor(); + if (StringUtils.isNoneEmpty(color)) { + if (font instanceof HSSFFont) { + setFoutForHSSF(font, cell.getSheet().getWorkbook(), color); + } else if (font instanceof XSSFFont) { + setFoutForXSSF(font, color); + } } if (UNDERLINE.equals(style.getFont().getDecoration())) { font.setUnderline(Font.U_SINGLE); @@ -52,4 +60,18 @@ public class TextCssConvertImpl implements ICssConvertToExcel, ICssConvertToHtml cellStyle.setFont(font); } + private void setFoutForXSSF(Font font, String colorStr) { + XSSFColor color = PoiCssUtils.parseColor(colorStr); + ((XSSFFont)font).setColor(color); + } + + private void setFoutForHSSF(Font font, Workbook workbook, String colorStr) { + HSSFColor color = PoiCssUtils.parseColor((HSSFWorkbook) workbook, colorStr); + if (color != null) { + if (color.getIndex() != BLACK.index) { + font.setColor(color.getIndex()); + } + } + } + } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/entity/style/CssStyleFontEnity.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/entity/style/CssStyleFontEnity.java index 51724ad..059d05b 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/entity/style/CssStyleFontEnity.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/entity/style/CssStyleFontEnity.java @@ -35,7 +35,7 @@ public class CssStyleFontEnity { private int size; private String family; private String decoration; - private int color; + private String color; public String getStyle() { return style; @@ -69,11 +69,11 @@ public class CssStyleFontEnity { this.family = family; } - public int getColor() { + public String getColor() { return color; } - public void setColor(int color) { + public void setColor(String color) { this.color = color; } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiCssUtils.java b/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiCssUtils.java index c5f8f04..53dd6f0 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiCssUtils.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiCssUtils.java @@ -8,6 +8,7 @@ import java.util.regex.Pattern; import java.util.regex.Matcher; import org.slf4j.LoggerFactory; import org.apache.poi.hssf.util.HSSFColor; +import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFPalette; import org.apache.poi.hssf.usermodel.HSSFWorkbook; @@ -132,17 +133,37 @@ public class PoiCssUtils { /** * parse color + * @param workBook work book * @param color string color - * @return int + * @return HSSFColor */ - public static int parseColor(String color) { + public static HSSFColor parseColor(HSSFWorkbook workBook, String color) { + HSSFColor poiColor = null; if (StringUtils.isNotBlank(color)) { Color awtColor = Color.decode(color); if (awtColor != null) { - return awtColor.getRGB(); + int r = awtColor.getRed(); + int g = awtColor.getGreen(); + int b = awtColor.getBlue(); + HSSFPalette palette = workBook.getCustomPalette(); + poiColor = palette.findColor((byte) r, (byte) g, (byte) b); + if (poiColor == null) { + poiColor = palette.findSimilarColor(r, g, b); + } } } - return 0; + return poiColor; + } + + public static XSSFColor parseColor(String color) { + XSSFColor poiColor = null; + if (StringUtils.isNotBlank(color)) { + Color awtColor = Color.decode(color); + if (awtColor != null) { + poiColor = new XSSFColor(awtColor); + } + } + return poiColor; } // -- @@ -156,7 +177,7 @@ public class PoiCssUtils { return String.format("#%02x%02x%02x", r, g, b); } - private static int calcColorValue(String color) { + public static int calcColorValue(String color) { int rtn = 0; // matches 64 or 64% Matcher m = Pattern.compile("^(\\d*\\.?\\d+)\\s*(%)?$").matcher(color); @@ -170,4 +191,5 @@ public class PoiCssUtils { } return rtn; } + }