public class JlDailyReportVerticalCellStyleStrategy extends AbstractVerticalCellStyleStrategy {
private short colors1;
private Workbook workbook;
private short colors2;
private Map<Integer, CellStyle> headCellStyleCache = new HashMap<Integer, CellStyle>();
private Map<Integer, CellStyle> contentCellStyleCache = new HashMap<Integer, CellStyle>();
public JlDailyReportVerticalCellStyleStrategy() {
}
public JlDailyReportVerticalCellStyleStrategy(short index1, short index2) {
this.colors1 = index1;
this.colors2 = index2;
}
@Override
protected void initCellStyle(Workbook workbook) {
this.workbook = workbook;
HSSFPalette palette = ((HSSFWorkbook) workbook).getCustomPalette();
setColorAtIndex(palette, "#FBE5D6", (short) 60);
setColorAtIndex(palette, "#BFBFBF", (short) 61);
setColorAtIndex(palette, "#F8CBAD", (short) 62);
setColorAtIndex(palette, "#F2F2F2", (short) 63);
}
private void setColorAtIndex(HSSFPalette palette, String color, short index) {
//获得RGB三种
String r = color.substring(1, 3);
String g = color.substring(3, 5);
String b = color.substring(5, 7);
int r2 = Integer.parseInt(r, 16);
int g2 = Integer.parseInt(g, 16);
int b2 = Integer.parseInt(b, 16);
palette.setColorAtIndex(index, (byte) r2, (byte) g2, (byte) b2);
}
@Override
protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
if (head == null) {
return;
}
int columnIndex = head.getColumnIndex();
if (headCellStyleCache.containsKey(columnIndex)) {
CellStyle cellStyle = headCellStyleCache.get(columnIndex);
if (cellStyle != null) {
cell.setCellStyle(cellStyle);
}
return;
}
WriteCellStyle headCellStyle = headCellStyle(head);
if (headCellStyle == null) {
headCellStyleCache.put(columnIndex, null);
} else {
CellStyle cellStyle = StyleUtil.buildHeadCellStyle(workbook, headCellStyle);
cellStyle.setWrapText(true);
Font font = workbook.createFont();
font.setFontHeight((short) 12);
headCellStyleCache.put(columnIndex, cellStyle);
cell.setCellStyle(cellStyle);
}
}
@Override
protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
if (head == null) {
return;
}
int columnIndex = head.getColumnIndex();
if (contentCellStyleCache.containsKey(columnIndex)) {
CellStyle cellStyle = contentCellStyleCache.get(columnIndex);
if (cellStyle != null) {
cell.setCellStyle(cellStyle);
}
return;
}
WriteCellStyle contentCellStyle = contentCellStyle(head);
if (contentCellStyle == null) {
contentCellStyleCache.put(columnIndex, null);
} else {
CellStyle cellStyle = StyleUtil.buildContentCellStyle(workbook, contentCellStyle);
contentCellStyleCache.put(columnIndex, cellStyle);
cell.setCellStyle(cellStyle);
}
}
@Override
protected WriteCellStyle headCellStyle(Head head) {
WriteCellStyle writeCellStyle = new WriteCellStyle();
writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
Integer columnIndex = head.getColumnIndex().intValue();
if (columnIndex == 0) {
writeCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
} else if (columnIndex >= 1 && columnIndex < 10) {
writeCellStyle.setFillForegroundColor(colors1);
} else if (columnIndex >= 10) {
writeCellStyle.setFillForegroundColor(colors2);
}
return writeCellStyle;
}
@Override
protected WriteCellStyle contentCellStyle(Head head) {
WriteCellStyle writeCellStyle = new WriteCellStyle();
writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
writeCellStyle.setBorderRight(BorderStyle.THIN);
writeCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
writeCellStyle.setBorderLeft(BorderStyle.THIN);
writeCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
writeCellStyle.setBorderTop(BorderStyle.THIN);
writeCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
writeCellStyle.setBorderBottom(BorderStyle.THIN);
writeCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
Integer columnIndex = head.getColumnIndex().intValue();
// 背景
if (columnIndex == 0) {
writeCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
} else if (columnIndex >= 1 && columnIndex < 10) {
writeCellStyle.setFillForegroundColor(colors1);
} else if (columnIndex >= 10) {
writeCellStyle.setFillForegroundColor(colors2);
}
return writeCellStyle;
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
super.afterCellDispose(writeSheetHolder, writeTableHolder, cellDataList, cell, head, relativeRowIndex, isHead);
}
}
网友评论