|
@@ -3,9 +3,9 @@ package com.rongwei.bscommon.sys.utils;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.rongwei.bsentity.dto.luckysheet.CellStyleCellBo;
|
|
|
import com.rongwei.rwcommon.utils.JSONUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.poi.hssf.util.HSSFColor;
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
|
|
@@ -279,6 +279,11 @@ public class XlsSheetUtil {
|
|
|
return ;
|
|
|
}
|
|
|
|
|
|
+ Map<String, CellStyle> cellStyleMap = new HashMap<>(100);
|
|
|
+
|
|
|
+ //单元格创建标识,false表示已有
|
|
|
+ boolean createCellFlag = false;
|
|
|
+
|
|
|
for(int m = 0, n = borderInfo.size(); m < n; m++) {
|
|
|
JSONObject borderCell = borderInfo.getJSONObject(m);
|
|
|
String rangeType = borderCell.getString("rangeType");
|
|
@@ -313,32 +318,31 @@ public class XlsSheetUtil {
|
|
|
|
|
|
for(int colStart = cols.getIntValue(0), colEnd = cols.getIntValue(1); colStart <= colEnd; colStart++) {
|
|
|
|
|
|
- Cell cell = row.getCell(colStart);
|
|
|
+ createCellFlag = false;
|
|
|
|
|
|
+ Cell cell = row.getCell(colStart);
|
|
|
CellStyle cellStyle;
|
|
|
if(cell == null) {
|
|
|
+ createCellFlag = true;
|
|
|
cell = row.createCell(colStart);
|
|
|
- cellStyle = wb.createCellStyle();
|
|
|
- cell.setCellStyle(cellStyle);
|
|
|
- } else {
|
|
|
- cellStyle = cell.getCellStyle();
|
|
|
}
|
|
|
|
|
|
+ CellStyleCellBo cellBo = new CellStyleCellBo();
|
|
|
+ cellBo.setBottomBorderColor(colorByStr);
|
|
|
+ cellBo.setBorderBottom(borderStyle);
|
|
|
|
|
|
+ cellBo.setTopBorderColor(colorByStr);
|
|
|
+ cellBo.setBorderTop(borderStyle);
|
|
|
|
|
|
+ cellBo.setLeftBorderColor(colorByStr);
|
|
|
+ cellBo.setBorderLeft(borderStyle);
|
|
|
|
|
|
- cellStyle.setBottomBorderColor(colorByStr);
|
|
|
- cellStyle.setBorderBottom(borderStyle);
|
|
|
-
|
|
|
- cellStyle.setTopBorderColor(colorByStr);
|
|
|
- cellStyle.setBorderTop(borderStyle);
|
|
|
+ cellBo.setRightBorderColor(colorByStr);
|
|
|
+ cellBo.setBorderRight(borderStyle);
|
|
|
|
|
|
- cellStyle.setLeftBorderColor(colorByStr);
|
|
|
- cellStyle.setBorderLeft(borderStyle);
|
|
|
-
|
|
|
- cellStyle.setRightBorderColor(colorByStr);
|
|
|
- cellStyle.setBorderRight(borderStyle);
|
|
|
+ cellStyle = getCellStyle(cell, cellBo, cellStyleMap, wb, createCellFlag);
|
|
|
|
|
|
+ cell.setCellStyle(cellStyle);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -358,44 +362,42 @@ public class XlsSheetUtil {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- CellStyle cellStyle = cell.getCellStyle();
|
|
|
-
|
|
|
- if(cellStyle == null) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
+ CellStyleCellBo cellBo = new CellStyleCellBo();
|
|
|
JSONObject bObj = cellBorderValue.getJSONObject("b");
|
|
|
if(bObj != null) {
|
|
|
String color = bObj.getString("color");
|
|
|
Short style = bObj.getShort("style");
|
|
|
- cellStyle.setBottomBorderColor(ColorUtil.getColorByStr(color));
|
|
|
- cellStyle.setBorderBottom(BorderStyle.valueOf(style));
|
|
|
+ cellBo.setBottomBorderColor(ColorUtil.getColorByStr(color));
|
|
|
+ cellBo.setBorderBottom(BorderStyle.valueOf(style));
|
|
|
}
|
|
|
|
|
|
JSONObject tObj = cellBorderValue.getJSONObject("t");
|
|
|
if(tObj != null) {
|
|
|
String color = tObj.getString("color");
|
|
|
Short style = tObj.getShort("style");
|
|
|
- cellStyle.setTopBorderColor(ColorUtil.getColorByStr(color));
|
|
|
- cellStyle.setBorderTop(BorderStyle.valueOf(style));
|
|
|
+ cellBo.setTopBorderColor(ColorUtil.getColorByStr(color));
|
|
|
+ cellBo.setBorderTop(BorderStyle.valueOf(style));
|
|
|
}
|
|
|
|
|
|
JSONObject lObj = cellBorderValue.getJSONObject("l");
|
|
|
if(lObj != null) {
|
|
|
String color = lObj.getString("color");
|
|
|
Short style = lObj.getShort("style");
|
|
|
- cellStyle.setLeftBorderColor(ColorUtil.getColorByStr(color));
|
|
|
- cellStyle.setBorderLeft(BorderStyle.valueOf(style));
|
|
|
+ cellBo.setLeftBorderColor(ColorUtil.getColorByStr(color));
|
|
|
+ cellBo.setBorderLeft(BorderStyle.valueOf(style));
|
|
|
}
|
|
|
|
|
|
JSONObject rObj = cellBorderValue.getJSONObject("r");
|
|
|
if(rObj != null) {
|
|
|
String color = rObj.getString("color");
|
|
|
Short style = rObj.getShort("style");
|
|
|
- cellStyle.setRightBorderColor(ColorUtil.getColorByStr(color));
|
|
|
- cellStyle.setBorderRight(BorderStyle.valueOf(style));
|
|
|
+ cellBo.setRightBorderColor(ColorUtil.getColorByStr(color));
|
|
|
+ cellBo.setBorderRight(BorderStyle.valueOf(style));
|
|
|
}
|
|
|
+
|
|
|
+ CellStyle cellStyle = getCellStyle(cell, cellBo, cellStyleMap, wb, false);
|
|
|
+ cell.setCellStyle(cellStyle);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
@@ -403,6 +405,109 @@ public class XlsSheetUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取单元格样式
|
|
|
+ *
|
|
|
+ * 新建的单元格使用边框区分
|
|
|
+ *
|
|
|
+ * @param cell
|
|
|
+ * @param cellBo
|
|
|
+ * @param map
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static CellStyle getCellStyle(Cell cell, CellStyleCellBo cellBo, Map<String, CellStyle> map, Workbook wb, boolean createCellFlag) {
|
|
|
+
|
|
|
+ CellStyle cellStyle = cell.getCellStyle();
|
|
|
+
|
|
|
+ String styleKey = getCellStyleKey(cellBo);
|
|
|
+
|
|
|
+ if(createCellFlag) {
|
|
|
+ cellStyle = map.get(styleKey);
|
|
|
+ if(ObjectUtil.isNull(cellStyle)) {
|
|
|
+ cellStyle = wb.createCellStyle();
|
|
|
+ map.put(styleKey, cellStyle);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNotEmpty(cellBo.getBorderBottom())) {
|
|
|
+ cellStyle.setBorderBottom(cellBo.getBorderBottom());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNotEmpty(cellBo.getBottomBorderColor())) {
|
|
|
+ cellStyle.setBottomBorderColor(cellBo.getBottomBorderColor());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNotEmpty(cellBo.getBorderTop())) {
|
|
|
+ cellStyle.setBorderTop(cellBo.getBorderTop());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNotEmpty(cellBo.getTopBorderColor())) {
|
|
|
+ cellStyle.setTopBorderColor(cellBo.getTopBorderColor());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNotEmpty(cellBo.getBorderLeft())) {
|
|
|
+ cellStyle.setBorderLeft(cellBo.getBorderLeft());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNotEmpty(cellBo.getLeftBorderColor())) {
|
|
|
+ cellStyle.setLeftBorderColor(cellBo.getLeftBorderColor());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNotEmpty(cellBo.getBorderRight())) {
|
|
|
+ cellStyle.setBorderRight(cellBo.getBorderRight());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNotEmpty(cellBo.getRightBorderColor())) {
|
|
|
+ cellStyle.setRightBorderColor(cellBo.getRightBorderColor());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return cellStyle;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取key
|
|
|
+ *
|
|
|
+ * @param cellBo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static String getCellStyleKey(CellStyleCellBo cellBo) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ if(ObjectUtil.isNotEmpty(cellBo.getBorderBottom())) {
|
|
|
+ sb.append(cellBo.getBorderBottom());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNotEmpty(cellBo.getBottomBorderColor())) {
|
|
|
+ sb.append(cellBo.getBottomBorderColor());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNotEmpty(cellBo.getBorderTop())) {
|
|
|
+ sb.append(cellBo.getBorderTop());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNotEmpty(cellBo.getTopBorderColor())) {
|
|
|
+ sb.append(cellBo.getTopBorderColor());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNotEmpty(cellBo.getBorderLeft())) {
|
|
|
+ sb.append(cellBo.getBorderLeft());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNotEmpty(cellBo.getLeftBorderColor())) {
|
|
|
+ sb.append(cellBo.getLeftBorderColor());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNotEmpty(cellBo.getBorderRight())) {
|
|
|
+ sb.append(cellBo.getBorderRight());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNotEmpty(cellBo.getRightBorderColor())) {
|
|
|
+ sb.append(cellBo.getRightBorderColor());
|
|
|
+ }
|
|
|
+
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 单元格字体相关样式
|
|
|
* @param wb
|