/**
* @Description
* @Author
* @date 2020.02.18 16:56
*/
public abstract class XSSFProperties {
/**
* 对象
*/
protected XSSFWorkbook workBook;
/**
* 头部样式
*/
protected XSSFCellStyle headStyle;
/**
* body样式
*/
protected XSSFCellStyle bodyStyle;
/**
* 默认宽度
*/
protected final Integer defaultWidth = 23;
/**
* 默认高度
*/
protected final Float defaultHeight = 23F;
/**
* 宽度系数
*/
protected static final int COEFFICIENT_WITH = 256;
/**
* 设置主题
*
* @param headColor
*/
abstract void theme(short headColor);
}
/**
* @Descriptions Excel生成类
* @Author
* @date 2020.02.18 10:46
*/
public class XSSFBuilder extends XSSFProperties {
/***
* 初始化数据
*/
public XSSFBuilder() {
workBook = new XSSFWorkbook();
headStyle = super.workBook.createCellStyle();
bodyStyle = super.workBook.createCellStyle();
defaultHeadStyle();
defaultBodyStyle();
}
/**
* 设置主题颜色
*
* @param headColor
* @link org.apache.poi.ss.usermodel
*/
@Override
public void theme(short headColor) {
//主题
headStyle.setFillForegroundColor(headColor);
}
/**
* 一级sheet构造
*
* @param
*/
@SneakyThrows
public void construct(List param) {
}
/**
* 多级sheet构造
*
* @param map
*/
@SneakyThrows
public void construct(Map<String, List<Object>> map) {
for (String key : map.keySet()) {
init(map.get(key));
}
File file = new File("/Users/xiaocai/Downloads/test1113333.xlsx");
FileOutputStream outStream = new FileOutputStream(file);
workBook.write(outStream);
outStream.flush();
outStream.close();
}
/**
* 初始化数据表格
*
* @param param
*/
@SneakyThrows
private void init(List param) {
int index = 0;
Class<?> clazz = param.get(index).getClass();
Assert.isTrue(clazz.isAnnotationPresent(Sheet.class), "sheet name must not be null");
Sheet sheetAnnotation = clazz.getAnnotation(Sheet.class);
XSSFSheet sheet = workBook.createSheet(sheetAnnotation.name());
defaultWideHighPoints(sheetAnnotation.name());
//构造head
Field[] attFields = clazz.getDeclaredFields();
XSSFRow headRow = sheet.createRow(index);
int count = -1;
for (int i = 0; i < attFields.length; i++) {
Field field = attFields[i];
if (field.isAnnotationPresent(Anno.class)) {
XSSFCell xssfCell = headRow.createCell(++count);
Anno anno = field.getAnnotation(Anno.class);
xssfCell.setCellStyle(headStyle);
xssfCell.setCellValue(anno.name());
sheet.setColumnWidth(i, anno.width() * COEFFICIENT_WITH);
}
//下拉数据填充
if (field.isAnnotationPresent(Fold.class)) {
Fold fold = field.getAnnotation(Fold.class);
Method method = clazz.getMethod(fold.data());
Object o = method.invoke(clazz.newInstance());
Object[] info = new Object[0];
if (o instanceof String[]) {
info = (Object[]) o;
} else if (o instanceof List) {
info = ((List) o).toArray();
}
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint)
dvHelper.createExplicitListConstraint(Arrays.asList(info).toArray(new String[0]));
CellRangeAddressList addressList = new CellRangeAddressList(index + 1, param.size(), i, i);
DataValidation validation = dvHelper.createValidation(dvConstraint, addressList);
validation.createErrorBox("输入值有误", "请从下拉框中选择");
validation.setShowErrorBox(true);
sheet.addValidationData(validation);
}
}
//构造body
for (int i = 0; i < param.size(); i++) {
Object t = param.get(i);
Field[] fields = t.getClass().getDeclaredFields();
XSSFRow xssfRow = sheet.createRow(i + 1);
count = -1;
for (int k = 0; k < fields.length; k++) {
Field field = fields[k];
field.setAccessible(true);
Object val = field.get(t);
if (field.isAnnotationPresent(Anno.class)) {
XSSFCell xssfCell = xssfRow.createCell(++count);
xssfCell.setCellValue(Objects.isNull(val) ? null : String.valueOf(val));
xssfCell.setCellStyle(bodyStyle);
}
}
}
}
public void defaultHeadStyle() {
//上边框
headStyle.setBorderTop(BorderStyle.THIN);
//左边框
headStyle.setBorderLeft(BorderStyle.THIN);
//下边框
headStyle.setBorderBottom(BorderStyle.THIN);
//右边框
headStyle.setBorderRight(BorderStyle.THIN);
//水平居中
headStyle.setAlignment(HorizontalAlignment.CENTER);
//垂直居中
headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//填充
headStyle.setFillPattern(FillPatternType.FINE_DOTS);
//颜色
headStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
}
public void defaultBodyStyle() {
//水平居中
bodyStyle.setAlignment(HorizontalAlignment.CENTER);
//垂直居中
bodyStyle.setVerticalAlignment(VerticalAlignment.CENTER);
}
public void defaultWideHighPoints(String sheetName) {
XSSFSheet sheet = workBook.getSheet(sheetName);
sheet.setDefaultColumnWidth(defaultWidth);
sheet.setDefaultRowHeightInPoints(defaultHeight);
}
}
相关注解
/**
* @Description poi注解工具类 作用域字段上
* @Author
* @date 2020.02.18 13:43
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
public @interface Anno {
/**
* 标题
*
* @return
*/
String name() default "";
/**
* 参数索引位置(宽度默认23)
*/
int width() default 23;
}
/**
* @Description 作用域下拉框
* @Author
* @date 2020.02.20 09:41
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
public @interface Fold {
//下拉选择数据
String data() default "";
}
/**
* @Description 作用域方法上 创建sheet
* @Author
* @date 2020.02.18 19:19
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
public @interface Sheet {
/**
* 参数索引位置(宽度默认23)
*/
int index() default 0;
/**
* 标题
*
* @return
*/
String name() default "";
}