use of com.alibaba.excel.metadata.Head in project easyexcel by alibaba.
the class FillStyleAnnotatedTest method fillStyleHandler.
private void fillStyleHandler(File file, File template) throws Exception {
EasyExcel.write(file, FillData.class).withTemplate(template).sheet().registerWriteHandler(new AbstractVerticalCellStyleStrategy() {
@Override
protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
WriteCellStyle writeCellStyle = new WriteCellStyle();
WriteFont writeFont = new WriteFont();
writeCellStyle.setWriteFont(writeFont);
writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
writeFont.setBold(true);
if (context.getColumnIndex() == 0) {
writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
writeFont.setColor(IndexedColors.DARK_YELLOW.getIndex());
}
if (context.getColumnIndex() == 1) {
writeCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
writeFont.setColor(IndexedColors.DARK_RED.getIndex());
}
if (context.getColumnIndex() == 2) {
writeCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
writeFont.setColor(IndexedColors.DARK_GREEN.getIndex());
}
if (context.getColumnIndex() == 3) {
writeCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
writeFont.setColor(IndexedColors.DARK_BLUE.getIndex());
}
if (context.getColumnIndex() == 4) {
writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
writeFont.setColor(IndexedColors.DARK_YELLOW.getIndex());
}
if (context.getColumnIndex() == 5) {
writeCellStyle.setFillForegroundColor(IndexedColors.TEAL.getIndex());
writeFont.setColor(IndexedColors.DARK_TEAL.getIndex());
}
return writeCellStyle;
}
@Override
protected WriteCellStyle headCellStyle(Head head) {
return null;
}
}).doFill(data());
}
use of com.alibaba.excel.metadata.Head in project easyexcel by alibaba.
the class ExcelHeadProperty method initHeadRowNumber.
private void initHeadRowNumber() {
headRowNumber = 0;
for (Head head : headMap.values()) {
List<String> list = head.getHeadNameList();
if (list != null && list.size() > headRowNumber) {
headRowNumber = list.size();
}
}
for (Head head : headMap.values()) {
List<String> list = head.getHeadNameList();
if (list != null && !list.isEmpty() && list.size() < headRowNumber) {
int lack = headRowNumber - list.size();
int last = list.size() - 1;
for (int i = 0; i < lack; i++) {
list.add(list.get(last));
}
}
}
}
use of com.alibaba.excel.metadata.Head in project easyexcel by alibaba.
the class ModelBuildEventListener method buildUserModel.
private Object buildUserModel(Map<Integer, ReadCellData<?>> cellDataMap, ReadSheetHolder readSheetHolder, AnalysisContext context) {
ExcelReadHeadProperty excelReadHeadProperty = readSheetHolder.excelReadHeadProperty();
Object resultModel;
try {
resultModel = excelReadHeadProperty.getHeadClazz().newInstance();
} catch (Exception e) {
throw new ExcelDataConvertException(context.readRowHolder().getRowIndex(), 0, new ReadCellData<>(CellDataTypeEnum.EMPTY), null, "Can not instance class: " + excelReadHeadProperty.getHeadClazz().getName(), e);
}
Map<Integer, Head> headMap = excelReadHeadProperty.getHeadMap();
BeanMap dataMap = BeanMapUtils.create(resultModel);
for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
Integer index = entry.getKey();
Head head = entry.getValue();
String fieldName = head.getFieldName();
if (!cellDataMap.containsKey(index)) {
continue;
}
ReadCellData<?> cellData = cellDataMap.get(index);
Object value = ConverterUtils.convertToJavaObject(cellData, head.getField(), ClassUtils.declaredExcelContentProperty(dataMap, readSheetHolder.excelReadHeadProperty().getHeadClazz(), fieldName), readSheetHolder.converterMap(), context, context.readRowHolder().getRowIndex(), index);
if (value != null) {
dataMap.put(fieldName, value);
}
}
return resultModel;
}
use of com.alibaba.excel.metadata.Head in project diboot by dibo-software.
the class ReadExcelListener method invokeHead.
/**
* excel表头数据
*/
@Override
public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
this.headMap.clear();
fieldHeadMap.clear();
fieldNameMap.clear();
headNameMap.clear();
ExcelReadHeadProperty excelReadHeadProperty = context.currentReadHolder().excelReadHeadProperty();
for (Map.Entry<Integer, Head> entry : excelReadHeadProperty.getHeadMap().entrySet()) {
Integer index = entry.getKey();
Head head = entry.getValue();
String fieldName = head.getFieldName();
List<String> headNameList = head.getHeadNameList();
String name = headNameList.get(headNameList.size() - 1);
this.headMap.put(index, name);
fieldHeadMap.put(fieldName, name);
fieldNameMap.put(index, fieldName);
headNameMap.put(index, headNameList);
}
}
use of com.alibaba.excel.metadata.Head in project diboot by dibo-software.
the class CommentWriteHandler method afterCellCreate.
/**
* 头部批注处理
*/
@Override
public void afterCellCreate(CellWriteHandlerContext context) {
Head head = context.getHeadData();
List<String> headNames = head.getHeadNameList();
int lastHeadIndex = headNames.size() - 1;
Integer rowIndex = context.getRowIndex();
if (!context.getHead() || rowIndex != lastHeadIndex && !headNames.get(rowIndex).equals(headNames.get(lastHeadIndex))) {
return;
}
ExcelComment comment = AnnotationUtils.getAnnotation(head.getField(), ExcelComment.class);
// 空批注不写入
if (comment == null || V.isEmpty(comment.value())) {
return;
}
Sheet sheet = context.getWriteSheetHolder().getSheet();
Cell cell = context.getCell();
cell.setCellComment(buildComment(sheet.createDrawingPatriarch(), cell.getColumnIndex(), sheet.getLastRowNum(), comment.value()));
}
Aggregations