|
|
|
@ -8,8 +8,7 @@ import com.alibaba.excel.converters.ConverterKeyBuild;
|
|
|
|
|
import com.alibaba.excel.converters.NullableObjectConverter; |
|
|
|
|
import com.alibaba.excel.converters.WriteConverterContext; |
|
|
|
|
import com.alibaba.excel.enums.CellDataTypeEnum; |
|
|
|
|
import com.alibaba.excel.exception.ExcelDataConvertException; |
|
|
|
|
import com.alibaba.excel.metadata.Head; |
|
|
|
|
import com.alibaba.excel.exception.ExcelWriteDataConvertException; |
|
|
|
|
import com.alibaba.excel.metadata.data.CommentData; |
|
|
|
|
import com.alibaba.excel.metadata.data.FormulaData; |
|
|
|
|
import com.alibaba.excel.metadata.data.HyperlinkData; |
|
|
|
@ -19,10 +18,11 @@ import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
|
|
|
|
import com.alibaba.excel.support.ExcelTypeEnum; |
|
|
|
|
import com.alibaba.excel.util.DateUtils; |
|
|
|
|
import com.alibaba.excel.util.FileTypeUtils; |
|
|
|
|
import com.alibaba.excel.util.ListUtils; |
|
|
|
|
import com.alibaba.excel.util.StyleUtil; |
|
|
|
|
import com.alibaba.excel.util.WorkBookUtil; |
|
|
|
|
import com.alibaba.excel.util.WriteHandlerUtils; |
|
|
|
|
import com.alibaba.excel.write.metadata.holder.WriteHolder; |
|
|
|
|
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; |
|
|
|
|
|
|
|
|
|
import org.apache.commons.collections4.CollectionUtils; |
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFClientAnchor; |
|
|
|
@ -33,6 +33,7 @@ import org.apache.poi.ss.usermodel.CreationHelper;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Drawing; |
|
|
|
|
import org.apache.poi.ss.usermodel.Hyperlink; |
|
|
|
|
import org.apache.poi.ss.usermodel.Sheet; |
|
|
|
|
import org.apache.poi.ss.usermodel.Workbook; |
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFClientAnchor; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -47,124 +48,140 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
|
|
|
|
|
this.writeContext = writeContext; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected WriteCellData<?> converterAndSet(WriteHolder currentWriteHolder, Class<?> clazz, |
|
|
|
|
CellDataTypeEnum targetType, Cell cell, Object value, ExcelContentProperty excelContentProperty, Head head, |
|
|
|
|
Integer relativeRowIndex, int rowIndex, int columnIndex) { |
|
|
|
|
boolean needTrim = value != null && (value instanceof String && currentWriteHolder.globalConfiguration() |
|
|
|
|
.getAutoTrim()); |
|
|
|
|
if (needTrim) { |
|
|
|
|
value = ((String)value).trim(); |
|
|
|
|
} |
|
|
|
|
WriteCellData<?> cellData = convert(currentWriteHolder, clazz, targetType, cell, value, excelContentProperty); |
|
|
|
|
WriteHandlerUtils.afterCellDataConverted(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, |
|
|
|
|
excelContentProperty); |
|
|
|
|
/** |
|
|
|
|
* Transform the data and then to set into the cell |
|
|
|
|
* |
|
|
|
|
* @param cellWriteHandlerContext context |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
protected void converterAndSet(CellWriteHandlerContext cellWriteHandlerContext) { |
|
|
|
|
|
|
|
|
|
WriteCellData<?> cellData = convert(cellWriteHandlerContext); |
|
|
|
|
cellWriteHandlerContext.setCellDataList(ListUtils.newArrayList(cellData)); |
|
|
|
|
cellWriteHandlerContext.setFirstCellData(cellData); |
|
|
|
|
|
|
|
|
|
WriteHandlerUtils.afterCellDataConverted(cellWriteHandlerContext); |
|
|
|
|
|
|
|
|
|
// Fill in picture information
|
|
|
|
|
fillImage(cell, cellData.getImageDataList()); |
|
|
|
|
fillImage(cellWriteHandlerContext, cellData.getImageDataList()); |
|
|
|
|
|
|
|
|
|
// Fill in comment information
|
|
|
|
|
fillComment(cell, cellData.getCommentData()); |
|
|
|
|
fillComment(cellWriteHandlerContext, cellData.getCommentData()); |
|
|
|
|
|
|
|
|
|
// Fill in hyper link information
|
|
|
|
|
fillHyperLink(cell, cellData.getHyperlinkData()); |
|
|
|
|
fillHyperLink(cellWriteHandlerContext, cellData.getHyperlinkData()); |
|
|
|
|
|
|
|
|
|
// Fill in formula information
|
|
|
|
|
fillFormula(cell, cellData.getFormulaData()); |
|
|
|
|
fillFormula(cellWriteHandlerContext, cellData.getFormulaData()); |
|
|
|
|
|
|
|
|
|
// Fill index
|
|
|
|
|
cellData.setRowIndex(rowIndex); |
|
|
|
|
cellData.setColumnIndex(columnIndex); |
|
|
|
|
cellData.setRowIndex(cellWriteHandlerContext.getRowIndex()); |
|
|
|
|
cellData.setColumnIndex(cellWriteHandlerContext.getColumnIndex()); |
|
|
|
|
|
|
|
|
|
if (cellData.getType() == null) { |
|
|
|
|
cellData.setType(CellDataTypeEnum.EMPTY); |
|
|
|
|
} |
|
|
|
|
Cell cell = cellWriteHandlerContext.getCell(); |
|
|
|
|
switch (cellData.getType()) { |
|
|
|
|
case STRING: |
|
|
|
|
cell.setCellValue(cellData.getStringValue()); |
|
|
|
|
return cellData; |
|
|
|
|
return; |
|
|
|
|
case BOOLEAN: |
|
|
|
|
cell.setCellValue(cellData.getBooleanValue()); |
|
|
|
|
return cellData; |
|
|
|
|
return; |
|
|
|
|
case NUMBER: |
|
|
|
|
cell.setCellValue(cellData.getNumberValue().doubleValue()); |
|
|
|
|
return cellData; |
|
|
|
|
return; |
|
|
|
|
case DATE: |
|
|
|
|
cell.setCellValue(cellData.getDateValue()); |
|
|
|
|
return cellData; |
|
|
|
|
return; |
|
|
|
|
case RICH_TEXT_STRING: |
|
|
|
|
cell.setCellValue(StyleUtil |
|
|
|
|
.buildRichTextString(writeContext.writeWorkbookHolder(), cellData.getRichTextStringDataValue())); |
|
|
|
|
return cellData; |
|
|
|
|
return; |
|
|
|
|
case EMPTY: |
|
|
|
|
return cellData; |
|
|
|
|
return; |
|
|
|
|
default: |
|
|
|
|
throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), cellData, |
|
|
|
|
excelContentProperty, "Not supported data:" + value + " return type:" + cell.getCellType() |
|
|
|
|
+ "at row:" + cell.getRow().getRowNum()); |
|
|
|
|
throw new ExcelWriteDataConvertException(cellWriteHandlerContext, |
|
|
|
|
"Not supported data:" + cellWriteHandlerContext.getOriginalValue() + " return type:" |
|
|
|
|
+ cellData.getType() |
|
|
|
|
+ "at row:" + cellWriteHandlerContext.getRowIndex()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void fillFormula(Cell cell, FormulaData formulaData) { |
|
|
|
|
private void fillFormula(CellWriteHandlerContext cellWriteHandlerContext, FormulaData formulaData) { |
|
|
|
|
if (formulaData == null) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
Cell cell = cellWriteHandlerContext.getCell(); |
|
|
|
|
if (formulaData.getFormulaValue() != null) { |
|
|
|
|
cell.setCellFormula(formulaData.getFormulaValue()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void fillHyperLink(Cell cell, HyperlinkData hyperlinkData) { |
|
|
|
|
private void fillHyperLink(CellWriteHandlerContext cellWriteHandlerContext, HyperlinkData hyperlinkData) { |
|
|
|
|
if (hyperlinkData == null) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
CreationHelper helper = cell.getSheet().getWorkbook().getCreationHelper(); |
|
|
|
|
Integer rowIndex = cellWriteHandlerContext.getRowIndex(); |
|
|
|
|
Integer columnIndex = cellWriteHandlerContext.getColumnIndex(); |
|
|
|
|
Workbook workbook = cellWriteHandlerContext.getWriteWorkbookHolder().getWorkbook(); |
|
|
|
|
Cell cell = cellWriteHandlerContext.getCell(); |
|
|
|
|
|
|
|
|
|
CreationHelper helper = workbook.getCreationHelper(); |
|
|
|
|
Hyperlink hyperlink = helper.createHyperlink(StyleUtil.getHyperlinkType(hyperlinkData.getHyperlinkType())); |
|
|
|
|
hyperlink.setAddress(hyperlinkData.getAddress()); |
|
|
|
|
hyperlink.setFirstRow(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), hyperlinkData.getFirstRowIndex(), |
|
|
|
|
hyperlink.setFirstRow(StyleUtil.getCellCoordinate(rowIndex, hyperlinkData.getFirstRowIndex(), |
|
|
|
|
hyperlinkData.getRelativeFirstRowIndex())); |
|
|
|
|
hyperlink.setFirstColumn(StyleUtil.getCellCoordinate(cell.getColumnIndex(), hyperlinkData.getFirstColumnIndex(), |
|
|
|
|
hyperlink.setFirstColumn(StyleUtil.getCellCoordinate(columnIndex, hyperlinkData.getFirstColumnIndex(), |
|
|
|
|
hyperlinkData.getRelativeFirstColumnIndex())); |
|
|
|
|
hyperlink.setLastRow(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), hyperlinkData.getLastRowIndex(), |
|
|
|
|
hyperlink.setLastRow(StyleUtil.getCellCoordinate(rowIndex, hyperlinkData.getLastRowIndex(), |
|
|
|
|
hyperlinkData.getRelativeLastRowIndex())); |
|
|
|
|
hyperlink.setLastColumn(StyleUtil.getCellCoordinate(cell.getColumnIndex(), hyperlinkData.getLastColumnIndex(), |
|
|
|
|
hyperlink.setLastColumn(StyleUtil.getCellCoordinate(columnIndex, hyperlinkData.getLastColumnIndex(), |
|
|
|
|
hyperlinkData.getRelativeLastColumnIndex())); |
|
|
|
|
cell.setHyperlink(hyperlink); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void fillComment(Cell cell, CommentData commentData) { |
|
|
|
|
private void fillComment(CellWriteHandlerContext cellWriteHandlerContext, CommentData commentData) { |
|
|
|
|
if (commentData == null) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
ClientAnchor anchor; |
|
|
|
|
Integer rowIndex = cellWriteHandlerContext.getRowIndex(); |
|
|
|
|
Integer columnIndex = cellWriteHandlerContext.getColumnIndex(); |
|
|
|
|
Sheet sheet = cellWriteHandlerContext.getWriteSheetHolder().getSheet(); |
|
|
|
|
Cell cell = cellWriteHandlerContext.getCell(); |
|
|
|
|
|
|
|
|
|
if (writeContext.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.XLSX) { |
|
|
|
|
anchor = new XSSFClientAnchor(StyleUtil.getCoordinate(commentData.getLeft()), |
|
|
|
|
StyleUtil.getCoordinate(commentData.getTop()), |
|
|
|
|
StyleUtil.getCoordinate(commentData.getRight()), |
|
|
|
|
StyleUtil.getCoordinate(commentData.getBottom()), |
|
|
|
|
StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getFirstColumnIndex(), |
|
|
|
|
StyleUtil.getCellCoordinate(columnIndex, commentData.getFirstColumnIndex(), |
|
|
|
|
commentData.getRelativeFirstColumnIndex()), |
|
|
|
|
StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getFirstRowIndex(), |
|
|
|
|
StyleUtil.getCellCoordinate(rowIndex, commentData.getFirstRowIndex(), |
|
|
|
|
commentData.getRelativeFirstRowIndex()), |
|
|
|
|
StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getLastColumnIndex(), |
|
|
|
|
StyleUtil.getCellCoordinate(columnIndex, commentData.getLastColumnIndex(), |
|
|
|
|
commentData.getRelativeLastColumnIndex()) + 1, |
|
|
|
|
StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getLastRowIndex(), |
|
|
|
|
StyleUtil.getCellCoordinate(rowIndex, commentData.getLastRowIndex(), |
|
|
|
|
commentData.getRelativeLastRowIndex()) + 1); |
|
|
|
|
} else { |
|
|
|
|
anchor = new HSSFClientAnchor(StyleUtil.getCoordinate(commentData.getLeft()), |
|
|
|
|
StyleUtil.getCoordinate(commentData.getTop()), |
|
|
|
|
StyleUtil.getCoordinate(commentData.getRight()), |
|
|
|
|
StyleUtil.getCoordinate(commentData.getBottom()), |
|
|
|
|
(short)StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getFirstColumnIndex(), |
|
|
|
|
(short)StyleUtil.getCellCoordinate(columnIndex, commentData.getFirstColumnIndex(), |
|
|
|
|
commentData.getRelativeFirstColumnIndex()), |
|
|
|
|
StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getFirstRowIndex(), |
|
|
|
|
StyleUtil.getCellCoordinate(rowIndex, commentData.getFirstRowIndex(), |
|
|
|
|
commentData.getRelativeFirstRowIndex()), |
|
|
|
|
(short)(StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getLastColumnIndex(), |
|
|
|
|
(short)(StyleUtil.getCellCoordinate(columnIndex, commentData.getLastColumnIndex(), |
|
|
|
|
commentData.getRelativeLastColumnIndex()) + 1), |
|
|
|
|
StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getLastRowIndex(), |
|
|
|
|
StyleUtil.getCellCoordinate(rowIndex, commentData.getLastRowIndex(), |
|
|
|
|
commentData.getRelativeLastRowIndex()) + 1); |
|
|
|
|
} |
|
|
|
|
Comment comment = cell.getSheet().createDrawingPatriarch().createCellComment(anchor); |
|
|
|
|
|
|
|
|
|
Comment comment = sheet.createDrawingPatriarch().createCellComment(anchor); |
|
|
|
|
if (commentData.getRichTextStringData() != null) { |
|
|
|
|
comment.setString( |
|
|
|
|
StyleUtil.buildRichTextString(writeContext.writeWorkbookHolder(), commentData.getRichTextStringData())); |
|
|
|
@ -175,18 +192,22 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
|
|
|
|
|
cell.setCellComment(comment); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected void fillImage(Cell cell, List<ImageData> imageDataList) { |
|
|
|
|
protected void fillImage(CellWriteHandlerContext cellWriteHandlerContext, List<ImageData> imageDataList) { |
|
|
|
|
if (CollectionUtils.isEmpty(imageDataList)) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
Sheet sheet = cell.getSheet(); |
|
|
|
|
Integer rowIndex = cellWriteHandlerContext.getRowIndex(); |
|
|
|
|
Integer columnIndex = cellWriteHandlerContext.getColumnIndex(); |
|
|
|
|
Sheet sheet = cellWriteHandlerContext.getWriteSheetHolder().getSheet(); |
|
|
|
|
Workbook workbook = cellWriteHandlerContext.getWriteWorkbookHolder().getWorkbook(); |
|
|
|
|
|
|
|
|
|
Drawing<?> drawing = sheet.getDrawingPatriarch(); |
|
|
|
|
if (drawing == null) { |
|
|
|
|
drawing = sheet.createDrawingPatriarch(); |
|
|
|
|
} |
|
|
|
|
CreationHelper helper = sheet.getWorkbook().getCreationHelper(); |
|
|
|
|
for (ImageData imageData : imageDataList) { |
|
|
|
|
int index = sheet.getWorkbook().addPicture(imageData.getImage(), |
|
|
|
|
int index = workbook.addPicture(imageData.getImage(), |
|
|
|
|
FileTypeUtils.getImageTypeFormat(imageData.getImage())); |
|
|
|
|
ClientAnchor anchor = helper.createClientAnchor(); |
|
|
|
|
if (imageData.getTop() != null) { |
|
|
|
@ -201,13 +222,13 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
|
|
|
|
|
if (imageData.getLeft() != null) { |
|
|
|
|
anchor.setDx1(StyleUtil.getCoordinate(imageData.getLeft())); |
|
|
|
|
} |
|
|
|
|
anchor.setRow1(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), imageData.getFirstRowIndex(), |
|
|
|
|
anchor.setRow1(StyleUtil.getCellCoordinate(rowIndex, imageData.getFirstRowIndex(), |
|
|
|
|
imageData.getRelativeFirstRowIndex())); |
|
|
|
|
anchor.setCol1(StyleUtil.getCellCoordinate(cell.getColumnIndex(), imageData.getFirstColumnIndex(), |
|
|
|
|
anchor.setCol1(StyleUtil.getCellCoordinate(columnIndex, imageData.getFirstColumnIndex(), |
|
|
|
|
imageData.getRelativeFirstColumnIndex())); |
|
|
|
|
anchor.setRow2(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), imageData.getLastRowIndex(), |
|
|
|
|
anchor.setRow2(StyleUtil.getCellCoordinate(rowIndex, imageData.getLastRowIndex(), |
|
|
|
|
imageData.getRelativeLastRowIndex()) + 1); |
|
|
|
|
anchor.setCol2(StyleUtil.getCellCoordinate(cell.getColumnIndex(), imageData.getLastColumnIndex(), |
|
|
|
|
anchor.setCol2(StyleUtil.getCellCoordinate(columnIndex, imageData.getLastColumnIndex(), |
|
|
|
|
imageData.getRelativeLastColumnIndex()) + 1); |
|
|
|
|
if (imageData.getAnchorType() != null) { |
|
|
|
|
anchor.setAnchorType(imageData.getAnchorType().getValue()); |
|
|
|
@ -216,17 +237,16 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected WriteCellData<?> convert(WriteHolder currentWriteHolder, Class<?> clazz, CellDataTypeEnum targetType, |
|
|
|
|
Cell cell, Object value, ExcelContentProperty excelContentProperty) { |
|
|
|
|
protected WriteCellData<?> convert(CellWriteHandlerContext cellWriteHandlerContext) { |
|
|
|
|
// This means that the user has defined the data.
|
|
|
|
|
if (clazz == WriteCellData.class) { |
|
|
|
|
if (value == null) { |
|
|
|
|
if (cellWriteHandlerContext.getOriginalFieldClass() == WriteCellData.class) { |
|
|
|
|
if (cellWriteHandlerContext.getOriginalValue() == null) { |
|
|
|
|
return new WriteCellData<>(CellDataTypeEnum.EMPTY); |
|
|
|
|
} |
|
|
|
|
WriteCellData<?> cellDataValue = (WriteCellData<?>)value; |
|
|
|
|
WriteCellData<?> cellDataValue = (WriteCellData<?>)cellWriteHandlerContext.getOriginalValue(); |
|
|
|
|
if (cellDataValue.getType() != null) { |
|
|
|
|
// Configuration information may not be read here
|
|
|
|
|
fillProperty(cellDataValue, excelContentProperty); |
|
|
|
|
fillProperty(cellDataValue, cellWriteHandlerContext.getExcelContentProperty()); |
|
|
|
|
|
|
|
|
|
return cellDataValue; |
|
|
|
|
} else { |
|
|
|
@ -235,8 +255,7 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
|
|
|
|
|
return cellDataValue; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
WriteCellData<?> cellDataReturn = doConvert(currentWriteHolder, cellDataValue.getData().getClass(), |
|
|
|
|
targetType, cell, cellDataValue.getData(), excelContentProperty); |
|
|
|
|
WriteCellData<?> cellDataReturn = doConvert(cellWriteHandlerContext); |
|
|
|
|
|
|
|
|
|
if (cellDataValue.getImageDataList() != null) { |
|
|
|
|
cellDataReturn.setImageDataList(cellDataValue.getImageDataList()); |
|
|
|
@ -256,7 +275,7 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
|
|
|
|
|
} |
|
|
|
|
return cellDataReturn; |
|
|
|
|
} |
|
|
|
|
return doConvert(currentWriteHolder, clazz, targetType, cell, value, excelContentProperty); |
|
|
|
|
return doConvert(cellWriteHandlerContext); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void fillProperty(WriteCellData<?> cellDataValue, ExcelContentProperty excelContentProperty) { |
|
|
|
@ -280,8 +299,9 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private WriteCellData<?> doConvert(WriteHolder currentWriteHolder, Class<?> clazz, CellDataTypeEnum targetType, |
|
|
|
|
Cell cell, Object value, ExcelContentProperty excelContentProperty) { |
|
|
|
|
private WriteCellData<?> doConvert(CellWriteHandlerContext cellWriteHandlerContext) { |
|
|
|
|
ExcelContentProperty excelContentProperty = cellWriteHandlerContext.getExcelContentProperty(); |
|
|
|
|
|
|
|
|
|
Converter<?> converter = null; |
|
|
|
|
if (excelContentProperty != null) { |
|
|
|
|
converter = excelContentProperty.getConverter(); |
|
|
|
@ -289,31 +309,34 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
|
|
|
|
|
if (converter == null) { |
|
|
|
|
// csv is converted to string by default
|
|
|
|
|
if (writeContext.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.CSV) { |
|
|
|
|
targetType = CellDataTypeEnum.STRING; |
|
|
|
|
cellWriteHandlerContext.setTargetCellDataType(CellDataTypeEnum.STRING); |
|
|
|
|
} |
|
|
|
|
converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz, targetType)); |
|
|
|
|
converter = writeContext.currentWriteHolder().converterMap().get( |
|
|
|
|
ConverterKeyBuild.buildKey(cellWriteHandlerContext.getOriginalFieldClass(), |
|
|
|
|
cellWriteHandlerContext.getTargetCellDataType())); |
|
|
|
|
} |
|
|
|
|
if (value == null && !(converter instanceof NullableObjectConverter)) { |
|
|
|
|
if (cellWriteHandlerContext.getOriginalValue() == null && !(converter instanceof NullableObjectConverter)) { |
|
|
|
|
return new WriteCellData<>(CellDataTypeEnum.EMPTY); |
|
|
|
|
} |
|
|
|
|
if (converter == null) { |
|
|
|
|
throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), |
|
|
|
|
new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty, |
|
|
|
|
"Can not find 'Converter' support class " + clazz.getSimpleName() + "."); |
|
|
|
|
throw new ExcelWriteDataConvertException(cellWriteHandlerContext, |
|
|
|
|
"Can not find 'Converter' support class " + cellWriteHandlerContext.getOriginalFieldClass() |
|
|
|
|
.getSimpleName() + "."); |
|
|
|
|
} |
|
|
|
|
WriteCellData<?> cellData; |
|
|
|
|
try { |
|
|
|
|
cellData = ((Converter<Object>)converter).convertToExcelData( |
|
|
|
|
new WriteConverterContext<>(value, excelContentProperty, writeContext)); |
|
|
|
|
new WriteConverterContext<>(cellWriteHandlerContext.getOriginalValue(), excelContentProperty, |
|
|
|
|
writeContext)); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), |
|
|
|
|
new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty, |
|
|
|
|
"Convert data:" + value + " error, at row:" + cell.getRow().getRowNum(), e); |
|
|
|
|
throw new ExcelWriteDataConvertException(cellWriteHandlerContext, |
|
|
|
|
"Convert data:" + cellWriteHandlerContext.getOriginalValue() + " error, at row:" |
|
|
|
|
+ cellWriteHandlerContext.getRowIndex(), e); |
|
|
|
|
} |
|
|
|
|
if (cellData == null || cellData.getType() == null) { |
|
|
|
|
throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), |
|
|
|
|
new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty, |
|
|
|
|
"Convert data:" + value + " return null, at row:" + cell.getRow().getRowNum()); |
|
|
|
|
throw new ExcelWriteDataConvertException(cellWriteHandlerContext, |
|
|
|
|
"Convert data:" + cellWriteHandlerContext.getOriginalValue() + " return null, at row:" |
|
|
|
|
+ cellWriteHandlerContext.getRowIndex()); |
|
|
|
|
} |
|
|
|
|
return cellData; |
|
|
|
|
} |
|
|
|
|