Part 1 was just laying some foundation. Now it's time to tackle some of the more troublesome bits of managing XLSX through POI. The first challenge I wanted to tackle is how to define Fonts and CellStyles. Every time you create one of these elements, it gets added to the .xlsx file, even if it is a duplicate of something you've already created. Also, there is a maximum number of each, so if you create your cells dynamically, you can run out of room to create new cells. You can get around this by first creating your spreadsheet, and then applying styling to groups of cells. Buy my goal was dynamic reports. I want to write a report without knowing what kind of data is going to be in each row or cell.
The first question I asked myself is how I wanted to define and keep track of CellStyles and Fonts. I used Enums. Here is how I define fonts.
The first question I asked myself is how I wanted to define and keep track of CellStyles and Fonts. I used Enums. Here is how I define fonts.
public enum Fonts { REPORT_HEADER(20, 700, Color.BLACK, false), REPORT_SUBHEADER(14, 700, Color.BLACK, false), COLUMN_HEADER(10, 700, Color.BLACK, false), DATA(10, 400, Color.BLACK, false), TOTAL(10, 700, Color.BLACK, false), ERROR(14, 700, Color.RED, false); private short fontHeight; private short fontWeight; private Color color; private boolean italic; private Fonts(int fontHeight, int fontWeight, Color color, boolean italic) { this.fontHeight = (short) fontHeight; this.fontWeight = (short) fontWeight; this.color = color; this.italic = italic; } public short getFontHeight() { return fontHeight; } public short getFontWeight() { return fontWeight; } public XSSFColor getColor() { /** * Reverse black and white due to bug */ if (color.equals(Color.BLACK)) { return new XSSFColor(Color.WHITE); } else if (color.equals(Color.WHITE)) { return new XSSFColor(Color.BLACK); } else { return new XSSFColor(color); } } public boolean isItalic() { return italic; } }And now I need a manager class to handle the creation and lookup of fonts. This class takes a Fonts argument, such as Fonts.REPORT_HEADER and returns the existing font if it has already been created, or creates a new one and returns that. In this way, I only ever create a font once.
public class FontManager { private final Workbook wb = null; private final MapStyleManager works exactly the same and will be in Part 3...styleMap; protected FontManager(Workbook wb) { this.wb = wb; this.fontMap = new HashMap ; } public Font get(Fonts f) { if (!fontMap.containsKey(f)) { fontMap.put(f, createFont(f)); } return fontMap.get(f); } private Font createFont(Fonts f) { XSSFFont font = (XSSFFont) wb.createFont(); font.setBoldweight(f.getFontWeight()); font.setFontHeightInPoints(f.getFontHeight()); font.setFontName(Fonts.FONTNAME); font.setColor(f.getColor()); return font; } }
Comments
Post a Comment