Between the holidays and some deadline pressure, it's taken longer to get to this than I'd like. But at last, here it is. This is my CellStyleManager and CellStyles enum.
public class CellStyleManager {
public static final int BORDERLOC_NONE = 0;
public static final int BORDERLOC_TOP = 1;
public static final int BORDERLOC_BOTTOM = 2;
public static final int BORDERLOC_LEFT = 4;
public static final int BORDERLOC_RIGHT = 8;
public static final int BORDERLOC_ALL = BORDERLOC_BOTTOM | BORDERLOC_TOP | BORDERLOC_LEFT | BORDERLOC_RIGHT;
public static final int BORDERLOC_SIDES = BORDERLOC_LEFT | BORDERLOC_RIGHT;
public static final int BORDERLOC_TB = BORDERLOC_TOP | BORDERLOC_BOTTOM;
private final Workbook wb;
private final Map<cellstyles cellstyle=""> styleMap;
private final FontManager fontManager;
public CellStyleManager(Workbook wb) {
this.wb = wb;
this.fontManager = new FontManager(wb);
this.styleMap = new HashMap<cellstyles cellstyle="">();
}
public CellStyle get(CellStyles cs) {
if (!styleMap.containsKey(cs)) {
styleMap.put(cs, createCellStyle(cs));
}
return styleMap.get(cs);
}
protected CellStyle createCellStyle(CellStyles cs) {
XSSFCellStyle style = (XSSFCellStyle) wb.createCellStyle();
style.setAlignment(cs.getHalign());
// Default borders
style.setBorderTop(CellStyle.BORDER_THIN);
style.setBorderColor(BorderSide.TOP, new XSSFColor(Color.LIGHT_GRAY));
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setBorderColor(BorderSide.LEFT, new XSSFColor(Color.LIGHT_GRAY));
style.setBorderRight(CellStyle.BORDER_THIN);
style.setBorderColor(BorderSide.RIGHT, new XSSFColor(Color.LIGHT_GRAY));
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBorderColor(BorderSide.BOTTOM, new XSSFColor(Color.LIGHT_GRAY));
if ((cs.getBorders() & BORDERLOC_TOP) > 0) {
style.setBorderTop(CellStyle.BORDER_MEDIUM);
if (cs.getBorderColor() != null) {
style.setBorderColor(BorderSide.TOP, cs.getBorderColor());
} else {
style.setTopBorderColor(new XSSFColor(Color.BLACK));
}
}
if ((cs.getBorders() & BORDERLOC_BOTTOM) > 0) {
style.setBorderBottom(CellStyle.BORDER_MEDIUM);
if (cs.getBorderColor() != null) {
style.setBottomBorderColor(cs.getBorderColor());
} else {
style.setBottomBorderColor(new XSSFColor(Color.BLACK));
}
}
if ((cs.getBorders() & BORDERLOC_LEFT) > 0) {
style.setBorderLeft(CellStyle.BORDER_MEDIUM);
if (cs.getBorderColor() != null) {
style.setLeftBorderColor(cs.getBorderColor());
} else {
style.setLeftBorderColor(new XSSFColor(Color.BLACK));
}
}
if ((cs.getBorders() & BORDERLOC_RIGHT) > 0) {
style.setBorderRight(CellStyle.BORDER_MEDIUM);
if (cs.getBorderColor() != null) {
style.setRightBorderColor(cs.getBorderColor());
} else {
style.setRightBorderColor(new XSSFColor(Color.BLACK));
}
}
style.setFont(fontManager.get(cs.getFont()));
style.setWrapText(cs.isWordWrap());
style.setVerticalAlignment(cs.getValign());
if (cs.getBgColor() != null) {
style.setFillForegroundColor(cs.getBgColor());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
}
if (cs.getFormat() != null && !cs.getFormat().isEmpty()) {
style.setDataFormat(wb.createDataFormat().getFormat(cs.getFormat()));
}
return style;
}
My CellStyleManager also manages the FontManager. So I can really ignore it once this piece is written. Nothing really interesting here except using some bitwise logic to handle borders. And here is my CellStyles enum.
public enum CellStyles {
REPORT_HEADER(Cell.CELL_TYPE_STRING, String.class, null, CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.REPORT_HEADER, true),
REPORT_SUBHEADER(Cell.CELL_TYPE_STRING, String.class, null, CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.REPORT_SUBHEADER, true),
COLUMN_HEADERLEFT(Cell.CELL_TYPE_STRING, String.class, null, CellStyleManager.BORDERLOC_BOTTOM, Color.BLACK, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.COLUMN_HEADER, true),
COLUMN_HEADERRIGHT(Cell.CELL_TYPE_STRING, String.class, null, CellStyleManager.BORDERLOC_BOTTOM, Color.BLACK, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.COLUMN_HEADER, true),
CATEGORY(Cell.CELL_TYPE_STRING, String.class, null, CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.CATEGORY, false),
DATA_TEXT(Cell.CELL_TYPE_STRING, String.class, null, CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.DATA, false),
DATA_DATE(Cell.CELL_TYPE_NUMERIC, Date.class, "dd MMM yyyy", CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.DATA, false),
DATA_INTEGER(Cell.CELL_TYPE_NUMERIC, Integer.class, "###,###,##0", CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.DATA, false),
DATA_CURRENCY(Cell.CELL_TYPE_NUMERIC, Double.class, "$###,###,##0.00", CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.DATA, false),
DATA_DOUBLE(Cell.CELL_TYPE_NUMERIC, Double.class, "###,###,##0.00", CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.DATA, false),
TOTAL_BLANK(Cell.CELL_TYPE_BLANK, String.class, null, CellStyleManager.BORDERLOC_TOP, null, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.TOTAL, false),
TOTAL_INTEGER(Cell.CELL_TYPE_NUMERIC, Integer.class, "###,###,##0", CellStyleManager.BORDERLOC_TOP, null, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.TOTAL, false),
TOTAL_CURRENCY(Cell.CELL_TYPE_NUMERIC, Double.class, "$###,###,##0.00", CellStyleManager.BORDERLOC_TOP, null, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.TOTAL, false),
ERROR(Cell.CELL_TYPE_ERROR, String.class, null, CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.ERROR, false);
private int dataType;
private Class<?> expected;
private String format;
private Color borderColor;
private int borders;
private int halign;
private int valign;
private Color bgColor;
private Fonts font;
private boolean wrap;
private CellStyles(int dataType, Class<?> expected, String format, int borders, Color borderColor, int halign, int valign,
Color bgColor, Fonts font, boolean wrap) {
this.dataType = dataType;
this.expected = expected;
this.format = format;
this.borders = borders;
this.borderColor = borderColor;
this.halign = halign;
this.valign = valign;
this.bgColor = bgColor;
this.font = font;
this.wrap = wrap;
}
public int getDataType() {
return dataType;
}
public Class<?> getExpected() {
return expected;
}
public String getFormat() {
return format;
}
public XSSFColor getBorderColor() {
if (borderColor == null) {
return new XSSFColor(Color.LIGHT_GRAY);
} else {
return new XSSFColor(borderColor);
}
}
public int getBorders() {
return borders;
}
public short getHalign() {
return (short) halign;
}
public short getValign() {
return (short) valign;
}
public XSSFColor getBgColor() {
if (bgColor == null) {
return new XSSFColor(Color.WHITE);
} else {
return new XSSFColor(bgColor);
}
}
public Fonts getFont() {
return font;
}
public boolean isWordWrap() {
return wrap;
}
public int getRowHeight() {
// return (int) Math.round(font.getFontHeight() * 1.5);
return -1;
}
public boolean validate(Object o) {
if (o == null || o.getClass() == expected) {
return true;
} else {
return false;
}
}
I think I have everything I might need covered here, but if not, I can always add a new cell style definition.
Next up: combining all of this into a report (or at least the abstract base class of a report).
public class CellStyleManager {
public static final int BORDERLOC_NONE = 0;
public static final int BORDERLOC_TOP = 1;
public static final int BORDERLOC_BOTTOM = 2;
public static final int BORDERLOC_LEFT = 4;
public static final int BORDERLOC_RIGHT = 8;
public static final int BORDERLOC_ALL = BORDERLOC_BOTTOM | BORDERLOC_TOP | BORDERLOC_LEFT | BORDERLOC_RIGHT;
public static final int BORDERLOC_SIDES = BORDERLOC_LEFT | BORDERLOC_RIGHT;
public static final int BORDERLOC_TB = BORDERLOC_TOP | BORDERLOC_BOTTOM;
private final Workbook wb;
private final Map<cellstyles cellstyle=""> styleMap;
private final FontManager fontManager;
public CellStyleManager(Workbook wb) {
this.wb = wb;
this.fontManager = new FontManager(wb);
this.styleMap = new HashMap<cellstyles cellstyle="">();
}
public CellStyle get(CellStyles cs) {
if (!styleMap.containsKey(cs)) {
styleMap.put(cs, createCellStyle(cs));
}
return styleMap.get(cs);
}
protected CellStyle createCellStyle(CellStyles cs) {
XSSFCellStyle style = (XSSFCellStyle) wb.createCellStyle();
style.setAlignment(cs.getHalign());
// Default borders
style.setBorderTop(CellStyle.BORDER_THIN);
style.setBorderColor(BorderSide.TOP, new XSSFColor(Color.LIGHT_GRAY));
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setBorderColor(BorderSide.LEFT, new XSSFColor(Color.LIGHT_GRAY));
style.setBorderRight(CellStyle.BORDER_THIN);
style.setBorderColor(BorderSide.RIGHT, new XSSFColor(Color.LIGHT_GRAY));
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBorderColor(BorderSide.BOTTOM, new XSSFColor(Color.LIGHT_GRAY));
if ((cs.getBorders() & BORDERLOC_TOP) > 0) {
style.setBorderTop(CellStyle.BORDER_MEDIUM);
if (cs.getBorderColor() != null) {
style.setBorderColor(BorderSide.TOP, cs.getBorderColor());
} else {
style.setTopBorderColor(new XSSFColor(Color.BLACK));
}
}
if ((cs.getBorders() & BORDERLOC_BOTTOM) > 0) {
style.setBorderBottom(CellStyle.BORDER_MEDIUM);
if (cs.getBorderColor() != null) {
style.setBottomBorderColor(cs.getBorderColor());
} else {
style.setBottomBorderColor(new XSSFColor(Color.BLACK));
}
}
if ((cs.getBorders() & BORDERLOC_LEFT) > 0) {
style.setBorderLeft(CellStyle.BORDER_MEDIUM);
if (cs.getBorderColor() != null) {
style.setLeftBorderColor(cs.getBorderColor());
} else {
style.setLeftBorderColor(new XSSFColor(Color.BLACK));
}
}
if ((cs.getBorders() & BORDERLOC_RIGHT) > 0) {
style.setBorderRight(CellStyle.BORDER_MEDIUM);
if (cs.getBorderColor() != null) {
style.setRightBorderColor(cs.getBorderColor());
} else {
style.setRightBorderColor(new XSSFColor(Color.BLACK));
}
}
style.setFont(fontManager.get(cs.getFont()));
style.setWrapText(cs.isWordWrap());
style.setVerticalAlignment(cs.getValign());
if (cs.getBgColor() != null) {
style.setFillForegroundColor(cs.getBgColor());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
}
if (cs.getFormat() != null && !cs.getFormat().isEmpty()) {
style.setDataFormat(wb.createDataFormat().getFormat(cs.getFormat()));
}
return style;
}
My CellStyleManager also manages the FontManager. So I can really ignore it once this piece is written. Nothing really interesting here except using some bitwise logic to handle borders. And here is my CellStyles enum.
public enum CellStyles {
REPORT_HEADER(Cell.CELL_TYPE_STRING, String.class, null, CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.REPORT_HEADER, true),
REPORT_SUBHEADER(Cell.CELL_TYPE_STRING, String.class, null, CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.REPORT_SUBHEADER, true),
COLUMN_HEADERLEFT(Cell.CELL_TYPE_STRING, String.class, null, CellStyleManager.BORDERLOC_BOTTOM, Color.BLACK, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.COLUMN_HEADER, true),
COLUMN_HEADERRIGHT(Cell.CELL_TYPE_STRING, String.class, null, CellStyleManager.BORDERLOC_BOTTOM, Color.BLACK, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.COLUMN_HEADER, true),
CATEGORY(Cell.CELL_TYPE_STRING, String.class, null, CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.CATEGORY, false),
DATA_TEXT(Cell.CELL_TYPE_STRING, String.class, null, CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.DATA, false),
DATA_DATE(Cell.CELL_TYPE_NUMERIC, Date.class, "dd MMM yyyy", CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.DATA, false),
DATA_INTEGER(Cell.CELL_TYPE_NUMERIC, Integer.class, "###,###,##0", CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.DATA, false),
DATA_CURRENCY(Cell.CELL_TYPE_NUMERIC, Double.class, "$###,###,##0.00", CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.DATA, false),
DATA_DOUBLE(Cell.CELL_TYPE_NUMERIC, Double.class, "###,###,##0.00", CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.DATA, false),
TOTAL_BLANK(Cell.CELL_TYPE_BLANK, String.class, null, CellStyleManager.BORDERLOC_TOP, null, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.TOTAL, false),
TOTAL_INTEGER(Cell.CELL_TYPE_NUMERIC, Integer.class, "###,###,##0", CellStyleManager.BORDERLOC_TOP, null, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.TOTAL, false),
TOTAL_CURRENCY(Cell.CELL_TYPE_NUMERIC, Double.class, "$###,###,##0.00", CellStyleManager.BORDERLOC_TOP, null, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.TOTAL, false),
ERROR(Cell.CELL_TYPE_ERROR, String.class, null, CellStyleManager.BORDERLOC_NONE, null, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM, Color.WHITE, Fonts.ERROR, false);
private int dataType;
private Class<?> expected;
private String format;
private Color borderColor;
private int borders;
private int halign;
private int valign;
private Color bgColor;
private Fonts font;
private boolean wrap;
private CellStyles(int dataType, Class<?> expected, String format, int borders, Color borderColor, int halign, int valign,
Color bgColor, Fonts font, boolean wrap) {
this.dataType = dataType;
this.expected = expected;
this.format = format;
this.borders = borders;
this.borderColor = borderColor;
this.halign = halign;
this.valign = valign;
this.bgColor = bgColor;
this.font = font;
this.wrap = wrap;
}
public int getDataType() {
return dataType;
}
public Class<?> getExpected() {
return expected;
}
public String getFormat() {
return format;
}
public XSSFColor getBorderColor() {
if (borderColor == null) {
return new XSSFColor(Color.LIGHT_GRAY);
} else {
return new XSSFColor(borderColor);
}
}
public int getBorders() {
return borders;
}
public short getHalign() {
return (short) halign;
}
public short getValign() {
return (short) valign;
}
public XSSFColor getBgColor() {
if (bgColor == null) {
return new XSSFColor(Color.WHITE);
} else {
return new XSSFColor(bgColor);
}
}
public Fonts getFont() {
return font;
}
public boolean isWordWrap() {
return wrap;
}
public int getRowHeight() {
// return (int) Math.round(font.getFontHeight() * 1.5);
return -1;
}
public boolean validate(Object o) {
if (o == null || o.getClass() == expected) {
return true;
} else {
return false;
}
}
I think I have everything I might need covered here, but if not, I can always add a new cell style definition.
Next up: combining all of this into a report (or at least the abstract base class of a report).
Comments
Post a Comment