use of com.alibaba.excel.metadata.Head in project diboot by dibo-software.
the class FreezePaneWriteHandler method afterSheetCreate.
@Override
public void afterSheetCreate(SheetWriteHandlerContext context) {
Sheet sheet = context.getWriteSheetHolder().getSheet();
if (this.colSplit == null) {
WriteHolder writeHolder = context.getWriteContext().currentWriteHolder();
Map<Integer, Head> headMap = writeHolder.excelWriteHeadProperty().getHeadMap();
sheet.createFreezePane(0, headMap.get(0).getHeadNameList().size());
} else if (this.leftmostColumn == null) {
sheet.createFreezePane(this.colSplit, this.rowSplit);
} else {
sheet.createFreezePane(this.colSplit, this.rowSplit, this.leftmostColumn, this.topRow);
}
}
use of com.alibaba.excel.metadata.Head in project diboot by dibo-software.
the class OptionWriteHandler method beforeCellCreate.
/**
* 设置单元格下拉框选项
*/
@Override
public void beforeCellCreate(CellWriteHandlerContext context) {
Sheet sheet = context.getWriteSheetHolder().getSheet();
if (sheet.getLastRowNum() != 0) {
return;
}
Head head = context.getHeadData();
ExcelOption option = AnnotationUtils.getAnnotation(head.getField(), ExcelOption.class);
if (option == null) {
return;
}
String[] options = null;
String dictType = option.dict();
if (S.isNotEmpty(dictType)) {
options = getDictOptions(dictType);
}
if (V.isEmpty(options)) {
options = option.options();
}
// 下拉框选为空时不添加 单元格验证(单元下拉选项)
if (V.isEmpty(options)) {
return;
}
// 单元格范围地址
int col = context.getColumnIndex();
int rows = option.rows();
int firstRow = -1;
int lastRow = rows > 0 ? (firstRow = head.getHeadNameList().size()) - 1 + rows : -1;
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, col, col);
// 设置数据验证
DataValidationHelper helper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = helper.createExplicitListConstraint(options);
DataValidation dataValidation = helper.createValidation(constraint, addressList);
dataValidation.setErrorStyle(option.errorStyle());
// 处理Excel兼容性问题
if (dataValidation instanceof XSSFDataValidation) {
dataValidation.setSuppressDropDownArrow(true);
dataValidation.setShowErrorBox(true);
} else {
dataValidation.setSuppressDropDownArrow(false);
}
sheet.addValidationData(dataValidation);
}
use of com.alibaba.excel.metadata.Head in project easyexcel by alibaba.
the class ExcelWriteAddExecutor method addBasicTypeToExcel.
private void addBasicTypeToExcel(RowData oneRowData, Row row, int rowIndex, int relativeRowIndex) {
if (oneRowData.isEmpty()) {
return;
}
Map<Integer, Head> headMap = writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadMap();
int dataIndex = 0;
int maxCellIndex = -1;
for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
if (dataIndex >= oneRowData.size()) {
return;
}
int columnIndex = entry.getKey();
Head head = entry.getValue();
doAddBasicTypeToExcel(oneRowData, head, row, rowIndex, relativeRowIndex, dataIndex++, columnIndex);
maxCellIndex = Math.max(maxCellIndex, columnIndex);
}
// Finish
if (dataIndex >= oneRowData.size()) {
return;
}
// fix https://github.com/alibaba/easyexcel/issues/1702
// If there is data, it is written to the next cell
maxCellIndex++;
int size = oneRowData.size() - dataIndex;
for (int i = 0; i < size; i++) {
doAddBasicTypeToExcel(oneRowData, null, row, rowIndex, relativeRowIndex, dataIndex++, maxCellIndex++);
}
}
use of com.alibaba.excel.metadata.Head in project easyexcel by alibaba.
the class ExcelWriteAddExecutor method addJavaObjectToExcel.
private void addJavaObjectToExcel(Object oneRowData, Row row, int rowIndex, int relativeRowIndex, Map<Integer, Field> sortedAllFieldMap) {
WriteHolder currentWriteHolder = writeContext.currentWriteHolder();
BeanMap beanMap = BeanMapUtils.create(oneRowData);
// Bean the contains of the Map Key method with poor performance,So to create a keySet here
Set<String> beanKeySet = new HashSet<>(beanMap.keySet());
Set<String> beanMapHandledSet = new HashSet<>();
int maxCellIndex = -1;
// If it's a class it needs to be cast by type
if (HeadKindEnum.CLASS.equals(writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadKind())) {
Map<Integer, Head> headMap = writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadMap();
for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
int columnIndex = entry.getKey();
Head head = entry.getValue();
String name = head.getFieldName();
if (!beanKeySet.contains(name)) {
continue;
}
ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), name);
CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(writeContext, row, rowIndex, head, columnIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty);
WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext);
Cell cell = WorkBookUtil.createCell(row, columnIndex);
cellWriteHandlerContext.setCell(cell);
WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext);
cellWriteHandlerContext.setOriginalValue(beanMap.get(name));
cellWriteHandlerContext.setOriginalFieldClass(head.getField().getType());
converterAndSet(cellWriteHandlerContext);
WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
beanMapHandledSet.add(name);
maxCellIndex = Math.max(maxCellIndex, columnIndex);
}
}
// Finish
if (beanMapHandledSet.size() == beanMap.size()) {
return;
}
maxCellIndex++;
Map<String, Field> ignoreMap = writeContext.currentWriteHolder().excelWriteHeadProperty().getIgnoreMap();
initSortedAllFieldMapFieldList(oneRowData.getClass(), sortedAllFieldMap);
for (Map.Entry<Integer, Field> entry : sortedAllFieldMap.entrySet()) {
Field field = entry.getValue();
String fieldName = FieldUtils.resolveCglibFieldName(field);
boolean uselessData = !beanKeySet.contains(fieldName) || beanMapHandledSet.contains(fieldName) || ignoreMap.containsKey(fieldName);
if (uselessData) {
continue;
}
Object value = beanMap.get(fieldName);
ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), fieldName);
CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(writeContext, row, rowIndex, null, maxCellIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty);
WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext);
// fix https://github.com/alibaba/easyexcel/issues/1870
// If there is data, it is written to the next cell
Cell cell = WorkBookUtil.createCell(row, maxCellIndex);
cellWriteHandlerContext.setCell(cell);
WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext);
cellWriteHandlerContext.setOriginalValue(value);
cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(beanMap, fieldName, value));
converterAndSet(cellWriteHandlerContext);
WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
maxCellIndex++;
}
}
use of com.alibaba.excel.metadata.Head in project easyexcel by alibaba.
the class DefaultAnalysisEventProcessor method buildHead.
private void buildHead(AnalysisContext analysisContext, Map<Integer, ReadCellData<?>> cellDataMap) {
if (!HeadKindEnum.CLASS.equals(analysisContext.currentReadHolder().excelReadHeadProperty().getHeadKind())) {
return;
}
Map<Integer, String> dataMap = ConverterUtils.convertToStringMap(cellDataMap, analysisContext);
ExcelReadHeadProperty excelHeadPropertyData = analysisContext.readSheetHolder().excelReadHeadProperty();
Map<Integer, Head> headMapData = excelHeadPropertyData.getHeadMap();
Map<Integer, Head> tmpHeadMap = new HashMap<Integer, Head>(headMapData.size() * 4 / 3 + 1);
for (Map.Entry<Integer, Head> entry : headMapData.entrySet()) {
Head headData = entry.getValue();
if (headData.getForceIndex() || !headData.getForceName()) {
tmpHeadMap.put(entry.getKey(), headData);
continue;
}
List<String> headNameList = headData.getHeadNameList();
String headName = headNameList.get(headNameList.size() - 1);
for (Map.Entry<Integer, String> stringEntry : dataMap.entrySet()) {
if (stringEntry == null) {
continue;
}
String headString = stringEntry.getValue();
Integer stringKey = stringEntry.getKey();
if (StringUtils.isEmpty(headString)) {
continue;
}
if (analysisContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
headString = headString.trim();
}
if (headName.equals(headString)) {
headData.setColumnIndex(stringKey);
tmpHeadMap.put(stringKey, headData);
break;
}
}
}
excelHeadPropertyData.setHeadMap(tmpHeadMap);
}
Aggregations