Search in sources :

Example 1 with ExcelWriteDataConvertException

use of com.alibaba.excel.exception.ExcelWriteDataConvertException in project easyexcel by alibaba.

the class AbstractExcelWriteExecutor method doConvert.

private WriteCellData<?> doConvert(CellWriteHandlerContext cellWriteHandlerContext) {
    ExcelContentProperty excelContentProperty = cellWriteHandlerContext.getExcelContentProperty();
    Converter<?> converter = null;
    if (excelContentProperty != null) {
        converter = excelContentProperty.getConverter();
    }
    if (converter == null) {
        // csv is converted to string by default
        if (writeContext.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.CSV) {
            cellWriteHandlerContext.setTargetCellDataType(CellDataTypeEnum.STRING);
        }
        converter = writeContext.currentWriteHolder().converterMap().get(ConverterKeyBuild.buildKey(cellWriteHandlerContext.getOriginalFieldClass(), cellWriteHandlerContext.getTargetCellDataType()));
    }
    if (cellWriteHandlerContext.getOriginalValue() == null && !(converter instanceof NullableObjectConverter)) {
        return new WriteCellData<>(CellDataTypeEnum.EMPTY);
    }
    if (converter == null) {
        throw new ExcelWriteDataConvertException(cellWriteHandlerContext, "Can not find 'Converter' support class " + cellWriteHandlerContext.getOriginalFieldClass().getSimpleName() + ".");
    }
    WriteCellData<?> cellData;
    try {
        cellData = ((Converter<Object>) converter).convertToExcelData(new WriteConverterContext<>(cellWriteHandlerContext.getOriginalValue(), excelContentProperty, writeContext));
    } catch (Exception e) {
        throw new ExcelWriteDataConvertException(cellWriteHandlerContext, "Convert data:" + cellWriteHandlerContext.getOriginalValue() + " error, at row:" + cellWriteHandlerContext.getRowIndex(), e);
    }
    if (cellData == null || cellData.getType() == null) {
        throw new ExcelWriteDataConvertException(cellWriteHandlerContext, "Convert data:" + cellWriteHandlerContext.getOriginalValue() + " return is null or return type is null, at row:" + cellWriteHandlerContext.getRowIndex());
    }
    return cellData;
}
Also used : ExcelWriteDataConvertException(com.alibaba.excel.exception.ExcelWriteDataConvertException) WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) WriteConverterContext(com.alibaba.excel.converters.WriteConverterContext) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) NullableObjectConverter(com.alibaba.excel.converters.NullableObjectConverter) ExcelWriteDataConvertException(com.alibaba.excel.exception.ExcelWriteDataConvertException)

Example 2 with ExcelWriteDataConvertException

use of com.alibaba.excel.exception.ExcelWriteDataConvertException in project easyexcel by alibaba.

the class AbstractExcelWriteExecutor method converterAndSet.

/**
 * Transform the data and then to set into the cell
 *
 * @param cellWriteHandlerContext context
 */
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(cellWriteHandlerContext, cellData.getImageDataList());
    // Fill in comment information
    fillComment(cellWriteHandlerContext, cellData.getCommentData());
    // Fill in hyper link information
    fillHyperLink(cellWriteHandlerContext, cellData.getHyperlinkData());
    // Fill in formula information
    fillFormula(cellWriteHandlerContext, cellData.getFormulaData());
    // Fill index
    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;
        case BOOLEAN:
            cell.setCellValue(cellData.getBooleanValue());
            return;
        case NUMBER:
            cell.setCellValue(cellData.getNumberValue().doubleValue());
            return;
        case DATE:
            cell.setCellValue(cellData.getDateValue());
            return;
        case RICH_TEXT_STRING:
            cell.setCellValue(StyleUtil.buildRichTextString(writeContext.writeWorkbookHolder(), cellData.getRichTextStringDataValue()));
            return;
        case EMPTY:
            return;
        default:
            throw new ExcelWriteDataConvertException(cellWriteHandlerContext, "Not supported data:" + cellWriteHandlerContext.getOriginalValue() + " return type:" + cellData.getType() + "at row:" + cellWriteHandlerContext.getRowIndex());
    }
}
Also used : ExcelWriteDataConvertException(com.alibaba.excel.exception.ExcelWriteDataConvertException) Cell(org.apache.poi.ss.usermodel.Cell)

Aggregations

ExcelWriteDataConvertException (com.alibaba.excel.exception.ExcelWriteDataConvertException)2 NullableObjectConverter (com.alibaba.excel.converters.NullableObjectConverter)1 WriteConverterContext (com.alibaba.excel.converters.WriteConverterContext)1 WriteCellData (com.alibaba.excel.metadata.data.WriteCellData)1 ExcelContentProperty (com.alibaba.excel.metadata.property.ExcelContentProperty)1 Cell (org.apache.poi.ss.usermodel.Cell)1