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 d235d2c..0c069ee 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 @@ -3,12 +3,15 @@ package org.jeecgframework.poi.excel.html.css; import static org.jeecgframework.poi.excel.html.entity.HtmlCssConstant.*; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; +import org.jeecgframework.poi.excel.html.entity.style.CellStyleBorderEntity; import org.jeecgframework.poi.excel.html.entity.style.CellStyleEntity; import org.jeecgframework.poi.excel.html.entity.style.CssStyleFontEnity; import org.jeecgframework.poi.util.PoiCssUtils; @@ -22,7 +25,25 @@ import org.slf4j.LoggerFactory; */ public class CssParseServer { - private static final Logger log = LoggerFactory.getLogger(CssParseServer.class); + private static final Logger log = LoggerFactory.getLogger(CssParseServer.class); + + @SuppressWarnings("serial") + private final static Set BORDER_STYLES = new HashSet() { + { + // Specifies no border + add(NONE); + // The same as "none", except in border conflict resolution for table elements + add(HIDDEN); + // Specifies a dotted border + add(DOTTED); + // Specifies a dashed border + add(DASHED); + // Specifies a solid border + add(SOLID); + // Specifies a double border + add(DOUBLE); + } + }; public CellStyleEntity parseStyle(String style) { Map mapStyle = new HashMap(); @@ -44,7 +65,10 @@ public class CssParseServer { parseFontAttr(mapStyle); - getBackground(mapStyle); + parseBackground(mapStyle); + + parseBorder(mapStyle); + return mapToCellStyleEntity(mapStyle); } @@ -52,18 +76,34 @@ public class CssParseServer { CellStyleEntity entity = new CellStyleEntity(); entity.setAlign(mapStyle.get(TEXT_ALIGN)); entity.setVetical(mapStyle.get(VETICAL_ALIGN)); - entity.setBackground(getBackground(mapStyle)); + entity.setBackground(parseBackground(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);*/ + entity.setBorder(getCssStyleBorderEntity(mapStyle)); return entity; } + private CellStyleBorderEntity getCssStyleBorderEntity(Map mapStyle) { + CellStyleBorderEntity border = new CellStyleBorderEntity(); + border.setBorderTopColor(mapStyle.get(BORDER + "-" + TOP + "-" + COLOR)); + border.setBorderBottomColor(mapStyle.get(BORDER + "-" + BOTTOM + "-" + COLOR)); + border.setBorderLeftColor(mapStyle.get(BORDER + "-" + LEFT + "-" + COLOR)); + border.setBorderRightColor(mapStyle.get(BORDER + "-" + RIGHT + "-" + COLOR)); + + border.setBorderTopWidth(mapStyle.get(BORDER + "-" + TOP + "-" + WIDTH)); + border.setBorderBottomWidth(mapStyle.get(BORDER + "-" + BOTTOM + "-" + WIDTH)); + border.setBorderLeftWidth(mapStyle.get(BORDER + "-" + LEFT + "-" + WIDTH)); + border.setBorderRightWidth(mapStyle.get(BORDER + "-" + RIGHT + "-" + WIDTH)); + + border.setBorderTopStyle(mapStyle.get(BORDER + "-" + TOP + "-" + STYLE)); + border.setBorderBottomStyle(mapStyle.get(BORDER + "-" + BOTTOM + "-" + STYLE)); + border.setBorderLeftStyle(mapStyle.get(BORDER + "-" + LEFT + "-" + STYLE)); + border.setBorderRightStyle(mapStyle.get(BORDER + "-" + RIGHT + "-" + STYLE)); + return border; + } + private CssStyleFontEnity getCssStyleFontEnity(Map style) { CssStyleFontEnity font = new CssStyleFontEnity(); font.setStyle(style.get(FONT_STYLE)); @@ -78,6 +118,57 @@ public class CssParseServer { return font; } + public void parseBorder(Map style) { + for (String pos : new String[] { null, TOP, RIGHT, BOTTOM, LEFT }) { + // border[-attr] + if (pos == null) { + setBorderAttr(style, pos, style.get(BORDER)); + setBorderAttr(style, pos, style.get(BORDER + "-" + COLOR)); + setBorderAttr(style, pos, style.get(BORDER + "-" + WIDTH)); + setBorderAttr(style, pos, style.get(BORDER + "-" + STYLE)); + } + // border-pos[-attr] + else { + setBorderAttr(style, pos, style.get(BORDER + "-" + pos)); + for (String attr : new String[] { COLOR, WIDTH, STYLE }) { + String attrName = BORDER + "-" + pos + "-" + attr; + String attrValue = style.get(attrName); + if (StringUtils.isNotBlank(attrValue)) { + style.put(attrName, attrValue); + } + } + } + } + } + + private void setBorderAttr(Map mapBorder, String pos, String value) { + if (StringUtils.isNotBlank(value)) { + String borderColor = null; + for (String borderAttr : value.split("\\s+")) { + if ((borderColor = PoiCssUtils.processColor(borderAttr)) != null) { + setBorderAttr(mapBorder, pos, COLOR, borderColor); + } else if (PoiCssUtils.isNum(borderAttr)) { + setBorderAttr(mapBorder, pos, WIDTH, borderAttr); + } else if (BORDER_STYLES.contains(borderAttr)) { + setBorderAttr(mapBorder, pos, STYLE, borderAttr); + } else { + log.info("Border Attr [{}] Is Not Suppoted.", borderAttr); + } + } + } + } + + private void setBorderAttr(Map mapBorder, String pos, String attr, + String value) { + if (StringUtils.isNotBlank(pos)) { + mapBorder.put(BORDER + "-" + pos + "-" + attr, value); + } else { + for (String name : new String[] { TOP, RIGHT, BOTTOM, LEFT }) { + mapBorder.put(BORDER + "-" + name + "-" + attr, value); + } + } + } + private void parseFontAttr(Map mapRtn) { log.debug("Parse Font Style."); @@ -183,7 +274,7 @@ public class CssParseServer { } } - private String getBackground(Map style) { + private String parseBackground(Map style) { String bg = style.get(BACKGROUND); String bgColor = null; if (StringUtils.isNotBlank(bg)) { diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/css/impl/BorderCssConverImpl.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/css/impl/BorderCssConverImpl.java index 8bf1759..e10a174 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/css/impl/BorderCssConverImpl.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/css/impl/BorderCssConverImpl.java @@ -15,12 +15,25 @@ */ package org.jeecgframework.poi.excel.html.css.impl; +import static org.jeecgframework.poi.excel.html.entity.HtmlCssConstant.*; + +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.reflect.MethodUtils; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.hssf.util.HSSFColor; 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.XSSFColor; import org.jeecgframework.poi.excel.html.css.ICssConvertToExcel; import org.jeecgframework.poi.excel.html.css.ICssConvertToHtml; import org.jeecgframework.poi.excel.html.entity.style.CellStyleBorderEntity; import org.jeecgframework.poi.excel.html.entity.style.CellStyleEntity; +import org.jeecgframework.poi.util.PoiCssUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * 边框转换实现类 @@ -29,23 +42,104 @@ import org.jeecgframework.poi.excel.html.entity.style.CellStyleEntity; */ public class BorderCssConverImpl implements ICssConvertToExcel, ICssConvertToHtml { + private static Logger log = LoggerFactory.getLogger(BorderCssConverImpl.class); + @Override public String convertToHtml(Cell cell, CellStyle cellStyle, CellStyleEntity style) { - CellStyleBorderEntity border = new CellStyleBorderEntity(); - border.setBorderBottom(cellStyle.getBorderBottom()); - border.setBorderBottomColor(cellStyle.getBottomBorderColor()); - border.setBorderLeft(cellStyle.getBorderLeft()); - border.setBorderLeftColor(cellStyle.getLeftBorderColor()); - border.setBorderRight(cellStyle.getBorderRight()); - border.setBorderRightColor(cellStyle.getRightBorderColor()); - border.setBorderTop(cellStyle.getBorderTop()); - border.setBorderTopColor(cellStyle.getTopBorderColor()); - style.setBorder(border); return null; } @Override public void convertToExcel(Cell cell, CellStyle cellStyle, CellStyleEntity style) { + if (style == null || style.getBorder() == null) { + return; + } + CellStyleBorderEntity border = style.getBorder(); + for (String pos : new String[] { TOP, RIGHT, BOTTOM, LEFT }) { + String posName = StringUtils.capitalize(pos.toLowerCase()); + // color + String colorAttr = null; + try { + colorAttr = (String) MethodUtils.invokeMethod(border, + "getBorder" + posName + "Color"); + } catch (Exception e) { + log.error("Set Border Style Error Caused.", e); + } + if (StringUtils.isNotEmpty(colorAttr)) { + if (cell instanceof HSSFCell) { + HSSFColor poiColor = PoiCssUtils + .parseColor((HSSFWorkbook) cell.getSheet().getWorkbook(), colorAttr); + if (poiColor != null) { + try { + MethodUtils.invokeMethod(cellStyle, "set" + posName + "BorderColor", + poiColor.getIndex()); + } catch (Exception e) { + log.error("Set Border Color Error Caused.", e); + } + } + } + if (cell instanceof XSSFCell) { + XSSFColor poiColor = PoiCssUtils.parseColor(colorAttr); + if (poiColor != null) { + try { + MethodUtils.invokeMethod(cellStyle, "set" + posName + "BorderColor", + poiColor); + } catch (Exception e) { + log.error("Set Border Color Error Caused.", e); + } + } + } + } + // width + int width = 0; + try { + String widthStr = (String) MethodUtils.invokeMethod(border, + "getBorder" + posName + "Width"); + if (PoiCssUtils.isNum(widthStr)) { + width = Integer.parseInt(widthStr); + } + } catch (Exception e) { + log.error("Set Border Style Error Caused.", e); + } + String styleValue = null; + try { + styleValue = (String) MethodUtils.invokeMethod(border, + "getBorder" + posName + "Style"); + } catch (Exception e) { + log.error("Set Border Style Error Caused.", e); + } + short shortValue = -1; + // empty or solid + if (StringUtils.isBlank(styleValue) || "solid".equals(styleValue)) { + if (width > 2) { + shortValue = CellStyle.BORDER_THICK; + } else if (width > 1) { + shortValue = CellStyle.BORDER_MEDIUM; + } else { + shortValue = CellStyle.BORDER_THIN; + } + } else if (ArrayUtils.contains(new String[] { NONE, HIDDEN }, styleValue)) { + shortValue = CellStyle.BORDER_NONE; + } else if (DOUBLE.equals(styleValue)) { + shortValue = CellStyle.BORDER_DOUBLE; + } else if (DOTTED.equals(styleValue)) { + shortValue = CellStyle.BORDER_DOTTED; + } else if (DASHED.equals(styleValue)) { + if (width > 1) { + shortValue = CellStyle.BORDER_MEDIUM_DASHED; + } else { + shortValue = CellStyle.BORDER_DASHED; + } + } + // border style + if (shortValue != -1) { + try { + MethodUtils.invokeMethod(cellStyle, "setBorder" + posName, shortValue); + } catch (Exception e) { + log.error("Set Border Style Error Caused.", e); + } + } + } } } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/entity/HtmlCssConstant.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/entity/HtmlCssConstant.java index 6ee13f2..5611165 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/entity/HtmlCssConstant.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/entity/HtmlCssConstant.java @@ -46,5 +46,11 @@ public interface HtmlCssConstant { public final String VETICAL_ALIGN = "vertical-align"; public final String BACKGROUND = "background"; public final String BACKGROUND_COLOR = "background-color"; + public final String NONE = "none"; + public final String HIDDEN = "hidden"; + public final String SOLID = "solid"; + public final String DOUBLE = "double"; + public final String DOTTED = "dotted"; + public final String DASHED = "dashed"; } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/entity/style/CellStyleBorderEntity.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/entity/style/CellStyleBorderEntity.java index 56813ba..82401b0 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/entity/style/CellStyleBorderEntity.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/entity/style/CellStyleBorderEntity.java @@ -22,91 +22,133 @@ package org.jeecgframework.poi.excel.html.entity.style; */ public class CellStyleBorderEntity { - private short borderLeft; + private String borderLeftColor; - private short borderRight; + private String borderRightColor; - private short borderTop; + private String borderTopColor; - private short borderBottom; + private String borderBottomColor; - private short borderLeftColor; + private String borderLeftStyle; - private short borderRightColor; + private String borderRightStyle; - private short borderTopColor; + private String borderTopStyle; - private short borderBottomColor; + private String borderBottomStyle; - public short getBorderLeft() { - return borderLeft; + private String borderLeftWidth; + + private String borderRightWidth; + + private String borderTopWidth; + + private String borderBottomWidth; + + public String getBorderLeftColor() { + return borderLeftColor; } - public void setBorderLeft(short borderLeft) { - this.borderLeft = borderLeft; + public void setBorderLeftColor(String borderLeftColor) { + this.borderLeftColor = borderLeftColor; } - public short getBorderRight() { - return borderRight; + public String getBorderRightColor() { + return borderRightColor; } - public void setBorderRight(short borderRight) { - this.borderRight = borderRight; + public void setBorderRightColor(String borderRightColor) { + this.borderRightColor = borderRightColor; } - public short getBorderTop() { - return borderTop; + public String getBorderTopColor() { + return borderTopColor; } - public void setBorderTop(short borderTop) { - this.borderTop = borderTop; + public void setBorderTopColor(String borderTopColor) { + this.borderTopColor = borderTopColor; } - public short getBorderBottom() { - return borderBottom; + public String getBorderBottomColor() { + return borderBottomColor; } - public void setBorderBottom(short borderBottom) { - this.borderBottom = borderBottom; + public void setBorderBottomColor(String borderBottomColor) { + this.borderBottomColor = borderBottomColor; } - public short getBorderLeftColor() { - return borderLeftColor; + public String getBorderLeftStyle() { + return borderLeftStyle; } - public void setBorderLeftColor(short borderLeftColor) { - this.borderLeftColor = borderLeftColor; + public void setBorderLeftStyle(String borderLeftStyle) { + this.borderLeftStyle = borderLeftStyle; } - public short getBorderRightColor() { - return borderRightColor; + public String getBorderRightStyle() { + return borderRightStyle; } - public void setBorderRightColor(short borderRightColor) { - this.borderRightColor = borderRightColor; + public void setBorderRightStyle(String borderRightStyle) { + this.borderRightStyle = borderRightStyle; } - public short getBorderTopColor() { - return borderTopColor; + public String getBorderTopStyle() { + return borderTopStyle; } - public void setBorderTopColor(short borderTopColor) { - this.borderTopColor = borderTopColor; + public void setBorderTopStyle(String borderTopStyle) { + this.borderTopStyle = borderTopStyle; } - public short getBorderBottomColor() { - return borderBottomColor; + public String getBorderBottomStyle() { + return borderBottomStyle; } - public void setBorderBottomColor(short borderBottomColor) { - this.borderBottomColor = borderBottomColor; + public void setBorderBottomStyle(String borderBottomStyle) { + this.borderBottomStyle = borderBottomStyle; + } + + public String getBorderLeftWidth() { + return borderLeftWidth; + } + + public void setBorderLeftWidth(String borderLeftWidth) { + this.borderLeftWidth = borderLeftWidth; + } + + public String getBorderRightWidth() { + return borderRightWidth; + } + + public void setBorderRightWidth(String borderRightWidth) { + this.borderRightWidth = borderRightWidth; + } + + public String getBorderTopWidth() { + return borderTopWidth; + } + + public void setBorderTopWidth(String borderTopWidth) { + this.borderTopWidth = borderTopWidth; + } + + public String getBorderBottomWidth() { + return borderBottomWidth; + } + + public void setBorderBottomWidth(String borderBottomWidth) { + this.borderBottomWidth = borderBottomWidth; } @Override public String toString() { - return new StringBuilder().append(borderBottom).append(borderBottomColor).append(borderLeft) - .append(borderLeftColor).append(borderRight).append(borderRightColor).append(borderTop) - .append(borderTopColor).toString(); + return new StringBuilder().append(borderLeftColor).append(borderRightColor) + .append(borderTopColor).append(borderBottomColor).append(borderLeftStyle) + .append(borderRightStyle).append(borderTopStyle).append(borderBottomStyle) + .append(borderLeftWidth).append(borderRightWidth).append(borderTopWidth) + .append(borderBottomWidth).toString(); }