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