commit
2ef267d046
@ -0,0 +1,56 @@
|
||||
package au.com.royalpay.payment.manage.support.poi;
|
||||
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.Font;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
|
||||
/**
|
||||
* Create by davep at 2020-03-12 13:27
|
||||
*/
|
||||
public class CellStyleFactory {
|
||||
private final short borderTop;
|
||||
private final short borderBottom;
|
||||
private final short borderLeft;
|
||||
private final short borderRight;
|
||||
private final short align;
|
||||
private final short valign;
|
||||
|
||||
public CellStyleFactory(short borderTop, short borderBottom, short borderLeft, short borderRight, short align, short valign) {
|
||||
this.borderTop = borderTop;
|
||||
this.borderBottom = borderBottom;
|
||||
this.borderLeft = borderLeft;
|
||||
this.borderRight = borderRight;
|
||||
this.align = align;
|
||||
this.valign = valign;
|
||||
}
|
||||
|
||||
public CellStyleFactory(short borderTopBottom, short borderLeftRight, short align, short valign) {
|
||||
this(borderTopBottom, borderTopBottom, borderLeftRight, borderLeftRight, align, valign);
|
||||
}
|
||||
|
||||
public CellStyleFactory(short border, short align, short valign) {
|
||||
this(border, border, align, valign);
|
||||
}
|
||||
|
||||
public CellStyleFactory(short border, short align) {
|
||||
this(border, border, align, CellStyle.VERTICAL_CENTER);
|
||||
}
|
||||
|
||||
public CellStyle buildCellStyle(Workbook workbook, Font font) {
|
||||
CellStyle style = workbook.createCellStyle();
|
||||
style.setBorderBottom(borderBottom);
|
||||
style.setBorderRight(borderRight);
|
||||
style.setBorderLeft(borderLeft);
|
||||
style.setBorderTop(borderTop);
|
||||
style.setAlignment(align);
|
||||
style.setVerticalAlignment(valign);
|
||||
if (font != null) {
|
||||
style.setFont(font);
|
||||
}
|
||||
return style;
|
||||
}
|
||||
|
||||
public CellStyle buildCellStyle(Workbook workbook) {
|
||||
return buildCellStyle(workbook, null);
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package au.com.royalpay.payment.manage.support.poi;
|
||||
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Create by davep at 2020-03-12 11:59
|
||||
*/
|
||||
public class ExcelFileBuilder {
|
||||
private ExcelTemplate template;
|
||||
private Map<String, List<JSONObject>> sheetsData;
|
||||
private boolean blocked = false;
|
||||
|
||||
public ExcelFileBuilder(ExcelTemplate template) {
|
||||
this.template = template;
|
||||
this.sheetsData = new HashMap<>();
|
||||
}
|
||||
|
||||
public ExcelFileBuilder bindData(String sheetName, List<JSONObject> data) {
|
||||
checkBlock();
|
||||
if (template.hasSheet(sheetName)) {
|
||||
sheetsData.put(sheetName, data);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Sheet name '" + sheetName + "' not exists");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void checkBlock() {
|
||||
if (blocked) {
|
||||
throw new IllegalStateException("Builder blocked");
|
||||
}
|
||||
}
|
||||
|
||||
public ExcelFileBuilder bindSingleSheetData(List<JSONObject> data) {
|
||||
if (template.getSheets().size() == 1) {
|
||||
String name = new ArrayList<>(template.getSheets().keySet()).get(0);
|
||||
bindData(name, data);
|
||||
} else {
|
||||
throw new IllegalArgumentException("There are more than one sheet");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void build(OutputStream out) {
|
||||
blocked = true;
|
||||
try (Workbook workbook = new XSSFWorkbook()) {
|
||||
Map<String, ExcelTemplate.SheetTemplate> sheetTpls = template.getSheets();
|
||||
for (Map.Entry<String, ExcelTemplate.SheetTemplate> sheetTpl : sheetTpls.entrySet()) {
|
||||
Sheet sheet = workbook.createSheet(sheetTpl.getKey());
|
||||
List<JSONObject> data = sheetsData.get(sheetTpl.getKey());
|
||||
if (data == null) {
|
||||
continue;
|
||||
}
|
||||
sheetTpl.getValue().buildSheet(sheet, data);
|
||||
}
|
||||
workbook.write(out);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,182 @@
|
||||
package au.com.royalpay.payment.manage.support.poi;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Create by davep at 2020-03-12 11:59
|
||||
*/
|
||||
public class ExcelTemplate {
|
||||
public static final CellStyleFactory STYLE_LEFT = new CellStyleFactory(CellStyle.BORDER_THIN, CellStyle.ALIGN_LEFT);
|
||||
public static final CellStyleFactory STYLE_RIGHT = new CellStyleFactory(CellStyle.BORDER_THIN, CellStyle.ALIGN_RIGHT);
|
||||
private Map<String, SheetTemplate> sheets;
|
||||
|
||||
public ExcelTemplate() {
|
||||
this.sheets = new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
public SheetTemplate addSheet(String name) {
|
||||
SheetTemplate sheetTpl = new SheetTemplate(this, name);
|
||||
sheets.put(name, sheetTpl);
|
||||
return sheetTpl;
|
||||
}
|
||||
|
||||
protected Map<String, SheetTemplate> getSheets() {
|
||||
return sheets;
|
||||
}
|
||||
|
||||
protected boolean hasSheet(String sheetName) {
|
||||
return sheets.containsKey(sheetName);
|
||||
}
|
||||
|
||||
public static class SheetTemplate {
|
||||
private ExcelTemplate parent;
|
||||
private String name;
|
||||
private List<ColumnTemplate> columns;
|
||||
|
||||
private SheetTemplate(ExcelTemplate parent, String name) {
|
||||
this.parent = parent;
|
||||
this.name = name;
|
||||
this.columns = new ArrayList<>();
|
||||
}
|
||||
|
||||
public ExcelTemplate build() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public SheetTemplate newSheet(String name) {
|
||||
return parent.addSheet(name);
|
||||
}
|
||||
|
||||
public ColumnTemplate addColumn(int columnType, String name, ValueProvider<?> provider) {
|
||||
ColumnTemplate col = new ColumnTemplate(this, name, columnType, provider);
|
||||
columns.add(col);
|
||||
return col;
|
||||
}
|
||||
|
||||
public ColumnTemplate addDateColumn(String pattern, String name) {
|
||||
return addDateColumn(pattern, name, name);
|
||||
}
|
||||
|
||||
public ColumnTemplate addDateColumn(String pattern, String name, String key) {
|
||||
return this.addColumn(Cell.CELL_TYPE_STRING, name, json -> DateFormatUtils.format(json.getDate(key), pattern));
|
||||
}
|
||||
|
||||
public ColumnTemplate addNumberColumn(String name) {
|
||||
return this.addNumberColumn(name, name);
|
||||
}
|
||||
|
||||
public ColumnTemplate addNumberColumn(String name, String key) {
|
||||
return this.addColumn(Cell.CELL_TYPE_NUMERIC, name, json -> json.getString(key));
|
||||
}
|
||||
|
||||
public ColumnTemplate addStringColumn(String name) {
|
||||
return this.addStringColumn(name, name);
|
||||
}
|
||||
|
||||
public ColumnTemplate addStringColumn(String name, String key) {
|
||||
return this.addColumn(Cell.CELL_TYPE_STRING, name, json -> json.getString(key));
|
||||
}
|
||||
|
||||
public ColumnTemplate addAutoTypeColumn(String name, ValueProvider<?> provider) {
|
||||
ColumnTemplate col = new ColumnTemplate(this, name, null, provider);
|
||||
columns.add(col);
|
||||
return col;
|
||||
}
|
||||
|
||||
protected String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
protected List<ColumnTemplate> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public void buildSheet(Sheet sheet, List<JSONObject> data) {
|
||||
int rowNum = 0;
|
||||
Row titleRow = sheet.createRow(rowNum);
|
||||
rowNum++;
|
||||
Workbook workbook = sheet.getWorkbook();
|
||||
Font boldFont = PoiUtils.initBoldFont(workbook);
|
||||
CellStyle titleStyle = new CellStyleFactory(CellStyle.BORDER_THIN, CellStyle.ALIGN_CENTER).buildCellStyle(workbook, boldFont);
|
||||
for (int i = 0, len = columns.size(); i < len; i++) {
|
||||
ColumnTemplate col = columns.get(i);
|
||||
Cell cell = titleRow.createCell(i, Cell.CELL_TYPE_STRING);
|
||||
cell.setCellStyle(titleStyle);
|
||||
cell.setCellValue(col.name);
|
||||
}
|
||||
for (JSONObject item : data) {
|
||||
Row dataRow = sheet.createRow(rowNum);
|
||||
rowNum++;
|
||||
for (int i = 0, len = columns.size(); i < len; i++) {
|
||||
ColumnTemplate col = columns.get(i);
|
||||
col.buildCell(dataRow, i, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class ColumnTemplate {
|
||||
private SheetTemplate parent;
|
||||
private String name;
|
||||
private ValueProvider<?> valueProvider;
|
||||
private Integer columnType;
|
||||
private CellStyleFactory cellStyle;
|
||||
private FontFactory fontFactory;
|
||||
|
||||
public ColumnTemplate(SheetTemplate parent, String name, Integer columnType, ValueProvider<?> valueProvider) {
|
||||
this.parent = parent;
|
||||
this.name = name;
|
||||
this.columnType = columnType;
|
||||
this.valueProvider = valueProvider;
|
||||
}
|
||||
|
||||
public ColumnTemplate setCellStyle(CellStyleFactory cellStyle) {
|
||||
this.cellStyle = cellStyle;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SheetTemplate ok() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
protected void buildCell(Row row, int columnNum, JSONObject data) {
|
||||
Object value = valueProvider.getValue(data);
|
||||
if (columnType == null) {
|
||||
columnType = PoiUtils.convertCellType(value);
|
||||
}
|
||||
CellStyle style = initCellStyle(row.getSheet().getWorkbook(), columnType);
|
||||
Cell cell = row.createCell(columnNum, columnType);
|
||||
cell.setCellStyle(style);
|
||||
PoiUtils.setCellValue(cell, value);
|
||||
}
|
||||
|
||||
private CellStyle initCellStyle(Workbook workbook, Integer columnType) {
|
||||
Font font = fontFactory != null ? fontFactory.buildFont(workbook) : null;
|
||||
if (cellStyle == null) {
|
||||
if (columnType == Cell.CELL_TYPE_NUMERIC || this.columnType == Cell.CELL_TYPE_BOOLEAN) {
|
||||
return STYLE_RIGHT.buildCellStyle(workbook, font);
|
||||
} else {
|
||||
return STYLE_LEFT.buildCellStyle(workbook, font);
|
||||
}
|
||||
} else {
|
||||
return cellStyle.buildCellStyle(workbook, font);
|
||||
}
|
||||
}
|
||||
|
||||
public ColumnTemplate setFontFactory(FontFactory fontFactory) {
|
||||
this.fontFactory = fontFactory;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public interface ValueProvider<T> {
|
||||
T getValue(JSONObject item);
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package au.com.royalpay.payment.manage.support.poi;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Font;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
|
||||
/**
|
||||
* Create by davep at 2020-03-12 13:27
|
||||
*/
|
||||
public class FontFactory {
|
||||
private boolean bold;
|
||||
private short color;
|
||||
private boolean italic;
|
||||
|
||||
public FontFactory(boolean bold, short color, boolean italic) {
|
||||
this.bold = bold;
|
||||
this.color = color;
|
||||
this.italic = italic;
|
||||
}
|
||||
|
||||
public FontFactory(boolean bold, boolean italic) {
|
||||
this(bold, Font.COLOR_NORMAL, italic);
|
||||
}
|
||||
|
||||
public FontFactory(boolean bold) {
|
||||
this(bold, false);
|
||||
}
|
||||
|
||||
public Font buildFont(Workbook workbook) {
|
||||
Font font = workbook.createFont();
|
||||
font.setBold(bold);
|
||||
font.setColor(color);
|
||||
font.setItalic(italic);
|
||||
return font;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
package au.com.royalpay.payment.manage.support.poi;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.Font;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.time.temporal.Temporal;
|
||||
import java.time.temporal.TemporalField;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Create by davep at 2020-03-12 12:43
|
||||
*/
|
||||
public class PoiUtils {
|
||||
private static final Map<Class<?>, Class<?>> PRIMITIVE_CLASSES = new HashMap<>();
|
||||
|
||||
static {
|
||||
PRIMITIVE_CLASSES.put(boolean.class, Boolean.class);
|
||||
PRIMITIVE_CLASSES.put(byte.class, Byte.class);
|
||||
PRIMITIVE_CLASSES.put(short.class, Short.class);
|
||||
PRIMITIVE_CLASSES.put(char.class, Character.class);
|
||||
PRIMITIVE_CLASSES.put(int.class, Integer.class);
|
||||
PRIMITIVE_CLASSES.put(long.class, Long.class);
|
||||
PRIMITIVE_CLASSES.put(float.class, Float.class);
|
||||
PRIMITIVE_CLASSES.put(double.class, Double.class);
|
||||
PRIMITIVE_CLASSES.put(void.class, Void.class);
|
||||
}
|
||||
|
||||
public static Font initBoldFont(Workbook workbook) {
|
||||
Font font = workbook.createFont();
|
||||
font.setBold(true);
|
||||
return font;
|
||||
}
|
||||
|
||||
|
||||
public static int convertCellType(Object value) {
|
||||
if (value == null) {
|
||||
return Cell.CELL_TYPE_BLANK;
|
||||
}
|
||||
Class<?> valueClass = value.getClass();
|
||||
if (valueClass.isPrimitive()) {
|
||||
valueClass = PRIMITIVE_CLASSES.get(valueClass);
|
||||
}
|
||||
if (Number.class.isAssignableFrom(valueClass)) {
|
||||
return Cell.CELL_TYPE_NUMERIC;
|
||||
}
|
||||
if (Date.class.isAssignableFrom(valueClass) || Temporal.class.isAssignableFrom(valueClass)) {
|
||||
return Cell.CELL_TYPE_NUMERIC;
|
||||
}
|
||||
if (Boolean.class.isAssignableFrom(valueClass)) {
|
||||
return Cell.CELL_TYPE_BOOLEAN;
|
||||
}
|
||||
return Cell.CELL_TYPE_STRING;
|
||||
}
|
||||
|
||||
public static void setCellValue(Cell cell, Object value) {
|
||||
if (value == null) {
|
||||
return;
|
||||
}
|
||||
Class valueClass = value.getClass();
|
||||
if (valueClass.isPrimitive()) {
|
||||
valueClass = PRIMITIVE_CLASSES.get(valueClass);
|
||||
}
|
||||
if (Number.class.isAssignableFrom(valueClass)) {
|
||||
cell.setCellValue(((Number)value).doubleValue());
|
||||
return;
|
||||
}
|
||||
if (Date.class.isAssignableFrom(valueClass)) {
|
||||
cell.setCellValue(((Date) value).getTime() / 1000);
|
||||
return;
|
||||
}
|
||||
if (Boolean.class.isAssignableFrom(valueClass)) {
|
||||
cell.setCellValue((boolean) value);
|
||||
return;
|
||||
}
|
||||
cell.setCellValue(value.toString());
|
||||
}
|
||||
}
|
Loading…
Reference in new issue