Skip to content

Commit

Permalink
Merge branch 'bugfix' into developing
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuangjiaju committed Oct 29, 2021
2 parents 2ff8735 + 4ea45e6 commit 10fc17f
Show file tree
Hide file tree
Showing 23 changed files with 564 additions and 351 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都
* 读的时候`invoke`里面抛出异常,不会再额外封装一层`ExcelAnalysisException` (不会编译报错)
* 样式等注解涉及到 `boolean` or 一些枚举 值的 有变动,新增默认值(会编译报错,注解改就行)
* 大版本升级后建议相关内容重新测试下
* 要升级涉及到

### 最新版本
```xml
Expand Down
68 changes: 47 additions & 21 deletions src/main/java/com/alibaba/excel/context/WriteContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
import com.alibaba.excel.util.ClassUtils;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.util.NumberDataFormatterUtils;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.util.WriteHandlerUtils;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.handler.context.RowWriteHandlerContext;
import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext;
import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.WriteWorkbook;
Expand Down Expand Up @@ -83,13 +88,16 @@ public WriteContextImpl(WriteWorkbook writeWorkbook) {
LOGGER.debug("Begin to Initialization 'WriteContextImpl'");
}
initCurrentWorkbookHolder(writeWorkbook);
WriteHandlerUtils.beforeWorkbookCreate(this);

WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext(
this);
WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext);
try {
WorkBookUtil.createWorkBook(writeWorkbookHolder);
} catch (Exception e) {
throw new ExcelGenerateException("Create workbook failure", e);
}
WriteHandlerUtils.afterWorkbookCreate(this);
WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Initialization 'WriteContextImpl' complete");
}
Expand Down Expand Up @@ -118,8 +126,10 @@ public void currentSheet(WriteSheet writeSheet, WriteTypeEnum writeType) {
initCurrentSheetHolder(writeSheet);

// Workbook handler need to supplementary execution
WriteHandlerUtils.beforeWorkbookCreate(this, true);
WriteHandlerUtils.afterWorkbookCreate(this, true);
WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext(
this, true);
WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext, true);
WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext, true);

// Initialization current sheet
initSheet(writeType);
Expand Down Expand Up @@ -162,7 +172,8 @@ private void initCurrentSheetHolder(WriteSheet writeSheet) {
}

private void initSheet(WriteTypeEnum writeType) {
WriteHandlerUtils.beforeSheetCreate(this);
SheetWriteHandlerContext sheetWriteHandlerContext = WriteHandlerUtils.createSheetWriteHandlerContext(this);
WriteHandlerUtils.beforeSheetCreate(sheetWriteHandlerContext);
Sheet currentSheet;
try {
if (writeSheetHolder.getSheetNo() != null) {
Expand Down Expand Up @@ -192,7 +203,7 @@ private void initSheet(WriteTypeEnum writeType) {
currentSheet = createSheet();
}
writeSheetHolder.setSheet(currentSheet);
WriteHandlerUtils.afterSheetCreate(this);
WriteHandlerUtils.afterSheetCreate(sheetWriteHandlerContext);
if (WriteTypeEnum.ADD.equals(writeType)) {
// Initialization head
initHead(writeSheetHolder.excelWriteHeadProperty());
Expand Down Expand Up @@ -226,11 +237,17 @@ public void initHead(ExcelWriteHeadProperty excelWriteHeadProperty) {
}
for (int relativeRowIndex = 0, i = newRowIndex; i < excelWriteHeadProperty.getHeadRowNumber()
+ newRowIndex; i++, relativeRowIndex++) {
WriteHandlerUtils.beforeRowCreate(this, newRowIndex, relativeRowIndex, Boolean.TRUE);

RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(this,
newRowIndex, relativeRowIndex, Boolean.TRUE);
WriteHandlerUtils.beforeRowCreate(rowWriteHandlerContext);

Row row = WorkBookUtil.createRow(writeSheetHolder.getSheet(), i);
WriteHandlerUtils.afterRowCreate(this, row, relativeRowIndex, Boolean.TRUE);
addOneRowOfHeadDataToExcel(row, excelWriteHeadProperty.getHeadMap(), relativeRowIndex);
WriteHandlerUtils.afterRowDispose(this, row, relativeRowIndex, Boolean.TRUE);
rowWriteHandlerContext.setRow(row);

WriteHandlerUtils.afterRowCreate(rowWriteHandlerContext);
addOneRowOfHeadDataToExcel(row, i, excelWriteHeadProperty.getHeadMap(), relativeRowIndex);
WriteHandlerUtils.afterRowDispose(rowWriteHandlerContext);
}
}

Expand All @@ -242,25 +259,29 @@ private void addMergedRegionToCurrentSheet(ExcelWriteHeadProperty excelWriteHead
}
}

private void addOneRowOfHeadDataToExcel(Row row, Map<Integer, Head> headMap, int relativeRowIndex) {
private void addOneRowOfHeadDataToExcel(Row row, Integer rowIndex, Map<Integer, Head> headMap,
int relativeRowIndex) {
for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
Head head = entry.getValue();
int columnIndex = entry.getKey();
ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null,
currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), head.getFieldName());

WriteHandlerUtils.beforeCellCreate(this, row, head, columnIndex, relativeRowIndex, Boolean.TRUE,
excelContentProperty);
CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(this, row,
rowIndex, head, columnIndex, relativeRowIndex, Boolean.TRUE, excelContentProperty);
WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext);

Cell cell = row.createCell(columnIndex);
cellWriteHandlerContext.setCell(cell);

WriteHandlerUtils.afterCellCreate(this, cell, head, relativeRowIndex, Boolean.TRUE, excelContentProperty);
WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext);

WriteCellData<String> writeCellData = new WriteCellData<>(head.getHeadNameList().get(relativeRowIndex));
cell.setCellValue(writeCellData.getStringValue());
cellWriteHandlerContext.setCellDataList(ListUtils.newArrayList(writeCellData));
cellWriteHandlerContext.setFirstCellData(writeCellData);

WriteHandlerUtils.afterCellDispose(this, writeCellData, cell, head, relativeRowIndex, Boolean.TRUE,
excelContentProperty);
WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
}
}

Expand Down Expand Up @@ -288,10 +309,15 @@ public void currentTable(WriteTable writeTable) {
initCurrentTableHolder(writeTable);

// Workbook and sheet handler need to supplementary execution
WriteHandlerUtils.beforeWorkbookCreate(this, true);
WriteHandlerUtils.afterWorkbookCreate(this, true);
WriteHandlerUtils.beforeSheetCreate(this, true);
WriteHandlerUtils.afterSheetCreate(this, true);
WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext(
this, true);
WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext, true);
WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext, true);

SheetWriteHandlerContext sheetWriteHandlerContext = WriteHandlerUtils.createSheetWriteHandlerContext(this,
true);
WriteHandlerUtils.beforeSheetCreate(sheetWriteHandlerContext, true);
WriteHandlerUtils.afterSheetCreate(sheetWriteHandlerContext, true);

initHead(writeTableHolder.excelWriteHeadProperty());
}
Expand Down Expand Up @@ -331,7 +357,7 @@ public void finish(boolean onException) {
return;
}
finished = true;
WriteHandlerUtils.afterWorkbookDispose(this);
WriteHandlerUtils.afterWorkbookDispose(writeWorkbookHolder.getWorkbookWriteHandlerContext());
if (writeWorkbookHolder == null) {
return;
}
Expand Down
47 changes: 26 additions & 21 deletions src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,45 @@
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.util.MapUtils;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
* Converter unique key.Consider that you can just use class as the key.
*
* @author Jiaju Zhuang
*/
public class ConverterKeyBuild {

private static final Map<String, String> BOXING_MAP = MapUtils.newHashMap();
private static final Map<Class<?>, Class<?>> BOXING_MAP = MapUtils.newHashMap();

static {
BOXING_MAP.put(int.class.getName(), Integer.class.getName());
BOXING_MAP.put(byte.class.getName(), Byte.class.getName());
BOXING_MAP.put(long.class.getName(), Long.class.getName());
BOXING_MAP.put(double.class.getName(), Double.class.getName());
BOXING_MAP.put(float.class.getName(), Float.class.getName());
BOXING_MAP.put(char.class.getName(), Character.class.getName());
BOXING_MAP.put(short.class.getName(), Short.class.getName());
BOXING_MAP.put(boolean.class.getName(), Boolean.class.getName());
BOXING_MAP.put(int.class, Integer.class);
BOXING_MAP.put(byte.class, Byte.class);
BOXING_MAP.put(long.class, Long.class);
BOXING_MAP.put(double.class, Double.class);
BOXING_MAP.put(float.class, Float.class);
BOXING_MAP.put(char.class, Character.class);
BOXING_MAP.put(short.class, Short.class);
BOXING_MAP.put(boolean.class, Boolean.class);
}

public static String buildKey(Class<?> clazz) {
String className = clazz.getName();
String boxingClassName = BOXING_MAP.get(clazz.getName());
if (boxingClassName == null) {
return className;
public static ConverterKey buildKey(Class<?> clazz) {
Class<?> boxingClass = BOXING_MAP.get(clazz);
if (boxingClass != null) {
return new ConverterKey(boxingClass, null);
}
return boxingClassName;
return new ConverterKey(clazz, null);
}

public static String buildKey(Class<?> clazz, CellDataTypeEnum cellDataTypeEnum) {
String key = buildKey(clazz);
if (cellDataTypeEnum == null) {
return key;
}
return key + "-" + cellDataTypeEnum;
public static ConverterKey buildKey(Class<?> clazz, CellDataTypeEnum cellDataTypeEnum) {
return new ConverterKey(clazz, cellDataTypeEnum);
}

@Data
@AllArgsConstructor
public static class ConverterKey {
private Class<?> clazz;
private CellDataTypeEnum cellDataTypeEnum;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;

import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey;
import com.alibaba.excel.converters.bigdecimal.BigDecimalBooleanConverter;
import com.alibaba.excel.converters.bigdecimal.BigDecimalNumberConverter;
import com.alibaba.excel.converters.bigdecimal.BigDecimalStringConverter;
Expand Down Expand Up @@ -52,8 +53,8 @@
* @author Jiaju Zhuang
*/
public class DefaultConverterLoader {
private static Map<String, Converter<?>> defaultWriteConverter;
private static Map<String, Converter<?>> allConverter;
private static Map<ConverterKey, Converter<?>> defaultWriteConverter;
private static Map<ConverterKey, Converter<?>> allConverter;

static {
initDefaultWriteConverter();
Expand Down Expand Up @@ -153,7 +154,7 @@ private static void initDefaultWriteConverter() {
*
* @return
*/
public static Map<String, Converter<?>> loadDefaultWriteConverter() {
public static Map<ConverterKey, Converter<?>> loadDefaultWriteConverter() {
return defaultWriteConverter;
}

Expand All @@ -171,7 +172,7 @@ private static void putWriteStringConverter(Converter<?> converter) {
*
* @return
*/
public static Map<String, Converter<?>> loadDefaultReadConverter() {
public static Map<ConverterKey, Converter<?>> loadDefaultReadConverter() {
return loadAllConverter();
}

Expand All @@ -180,7 +181,7 @@ public static Map<String, Converter<?>> loadDefaultReadConverter() {
*
* @return
*/
public static Map<String, Converter<?>> loadAllConverter() {
public static Map<ConverterKey, Converter<?>> loadAllConverter() {
return allConverter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ExcelDataConvertException extends RuntimeException {
*/
private ExcelContentProperty excelContentProperty;

public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData cellData,
public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData<?> cellData,
ExcelContentProperty excelContentProperty, String message) {
super(message);
this.rowIndex = rowIndex;
Expand All @@ -41,7 +41,7 @@ public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData
this.excelContentProperty = excelContentProperty;
}

public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData cellData,
public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData<?> cellData,
ExcelContentProperty excelContentProperty, String message, Throwable cause) {
super(message, cause);
this.rowIndex = rowIndex;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.alibaba.excel.exception;

import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;

import lombok.Data;

/**
* Data convert exception
*
* @author Jiaju Zhuang
*/
@Data
public class ExcelWriteDataConvertException extends ExcelDataConvertException {
/**
* handler.
*/
private CellWriteHandlerContext cellWriteHandlerContext;

public ExcelWriteDataConvertException(CellWriteHandlerContext cellWriteHandlerContext, String message) {
super(cellWriteHandlerContext.getRowIndex(), cellWriteHandlerContext.getColumnIndex(),
cellWriteHandlerContext.getFirstCellData(), cellWriteHandlerContext.getExcelContentProperty(), message);
this.cellWriteHandlerContext = cellWriteHandlerContext;
}

public ExcelWriteDataConvertException(CellWriteHandlerContext cellWriteHandlerContext, String message,
Throwable cause) {
super(cellWriteHandlerContext.getRowIndex(), cellWriteHandlerContext.getColumnIndex(),
cellWriteHandlerContext.getFirstCellData(), cellWriteHandlerContext.getExcelContentProperty(), message,
cause);
this.cellWriteHandlerContext = cellWriteHandlerContext;
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/alibaba/excel/metadata/AbstractHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey;

import lombok.Data;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -38,7 +39,7 @@ public abstract class AbstractHolder implements ConfigurationHolder {
* <p>
* Write key:
*/
private Map<String, Converter<?>> converterMap;
private Map<ConverterKey, Converter<?>> converterMap;

public AbstractHolder(BasicParameter basicParameter, AbstractHolder prentAbstractHolder) {
this.newInitialization = Boolean.TRUE;
Expand Down Expand Up @@ -81,7 +82,7 @@ public AbstractHolder(BasicParameter basicParameter, AbstractHolder prentAbstrac
}

@Override
public Map<String, Converter<?>> converterMap() {
public Map<ConverterKey, Converter<?>> converterMap() {
return getConverterMap();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
import java.util.Map;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey;

/**
*
* Get the corresponding holder
*
* @author Jiaju Zhuang
**/
public interface ConfigurationHolder extends Holder {

/**
*
* Record whether it's new or from cache
*
* @return Record whether it's new or from cache
Expand All @@ -32,5 +31,5 @@ public interface ConfigurationHolder extends Holder {
*
* @return Converter
*/
Map<String, Converter<?>> converterMap();
Map<ConverterKey, Converter<?>> converterMap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.fill.AnalysisCell;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;

import lombok.Data;
Expand Down Expand Up @@ -56,10 +55,6 @@ public class WriteCellData<T> extends CellData<T> {
*/
private CellStyle originCellStyle;

/**
* Only in the case of the fill is not null
*/
private AnalysisCell analysisCell;

public WriteCellData(String stringValue) {
this(CellDataTypeEnum.STRING, stringValue);
Expand Down
Loading

0 comments on commit 10fc17f

Please sign in to comment.