From 2682a041eb5820c8914759f8c29f2ef716c07ca2 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 10 Sep 2021 17:58:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8D=95=E5=85=83=E6=A0=BC?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/excel/converters/ConverterKeyBuild.java | 2 +- .../java/com/alibaba/excel/metadata/csv/CsvCell.java | 12 ++++++++++++ .../alibaba/excel/metadata/data/WriteCellData.java | 2 +- .../write/executor/AbstractExcelWriteExecutor.java | 4 ++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java b/src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java index a92f3033..f1068591 100644 --- a/src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java +++ b/src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java @@ -39,6 +39,6 @@ public class ConverterKeyBuild { if (cellDataTypeEnum == null) { return key; } - return key + "-" + cellDataTypeEnum.toString(); + return key + "-" + cellDataTypeEnum; } } diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java index 4898ff2c..6128ebd4 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java @@ -86,6 +86,9 @@ public class CsvCell extends CellBase { this.csvRow = csvRow; this.columnIndex = columnIndex; this.cellType = cellType; + if (this.cellType == null) { + this.cellType = CellType._NONE; + } } @Override @@ -98,6 +101,7 @@ public class CsvCell extends CellBase { FormulaData formulaData = new FormulaData(); formulaData.setFormulaValue(formula); this.formulaData = formulaData; + this.cellType = CellType.FORMULA; } @Override @@ -108,6 +112,7 @@ public class CsvCell extends CellBase { @Override protected void setCellValueImpl(double value) { numberValue = BigDecimal.valueOf(value); + this.cellType = CellType.NUMERIC; } @Override @@ -116,11 +121,13 @@ public class CsvCell extends CellBase { return; } this.dateValue = LocalDateTime.ofInstant(value.toInstant(), ZoneId.systemDefault()); + this.cellType = CellType.NUMERIC; } @Override protected void setCellValueImpl(LocalDateTime value) { this.dateValue = value; + this.cellType = CellType.NUMERIC; } @Override @@ -129,16 +136,19 @@ public class CsvCell extends CellBase { return; } this.dateValue = LocalDateTime.ofInstant(value.toInstant(), ZoneId.systemDefault()); + this.cellType = CellType.NUMERIC; } @Override protected void setCellValueImpl(String value) { this.stringValue = value; + this.cellType = CellType.STRING; } @Override protected void setCellValueImpl(RichTextString value) { richTextString = value; + this.cellType = CellType.STRING; } @Override @@ -241,11 +251,13 @@ public class CsvCell extends CellBase { @Override public void setCellValue(boolean value) { this.booleanValue = value; + this.cellType = CellType.BOOLEAN; } @Override public void setCellErrorValue(byte value) { this.numberValue = BigDecimal.valueOf(value); + this.cellType = CellType.ERROR; } @Override diff --git a/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java b/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java index 82231e5f..4c54b6fa 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java @@ -40,7 +40,7 @@ public class WriteCellData extends CellData { */ private HyperlinkData hyperlinkData; /** - * sytle + * style */ private WriteCellStyle writeCellStyle; diff --git a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java index 9f32153e..44ba04ee 100644 --- a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java @@ -269,6 +269,10 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { converter = excelContentProperty.getConverter(); } if (converter == null) { + // csv is converted to string by default + if (writeContext.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.CSV) { + targetType = CellDataTypeEnum.STRING; + } converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz, targetType)); } if (converter == null) {