From 2fb54ca91e86a6474024f51180c4cf05a6f2a01d Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Mon, 13 Sep 2021 15:17:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=A9=BA=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../analysis/csv/CsvExcelReadExecutor.java | 16 +++++++++--- .../excel/metadata/AbstractHolder.java | 8 ++++++ .../excel/metadata/csv/CsvDataFormat.java | 4 +-- .../alibaba/excel/metadata/csv/CsvSheet.java | 4 ++- .../metadata/holder/AbstractReadHolder.java | 9 ------- .../holder/csv/CsvReadWorkbookHolder.java | 2 +- .../com/alibaba/excel/util/DateUtils.java | 2 +- .../executor/AbstractExcelWriteExecutor.java | 26 +++++++++++++++++++ .../metadata/holder/AbstractWriteHolder.java | 8 ------ .../core/celldata/CellDataDataListener.java | 9 +++++-- .../test/temp/csv/CsvDataListeer.java | 20 ++++++++++++++ .../easyexcel/test/temp/csv/CsvReadTest.java | 9 ++++--- 12 files changed, 85 insertions(+), 32 deletions(-) create mode 100644 src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvDataListeer.java diff --git a/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java b/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java index 6f1353ca..e98b3580 100644 --- a/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java +++ b/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java @@ -20,6 +20,7 @@ import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.holder.ReadRowHolder; import com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder; import com.alibaba.excel.util.SheetUtils; +import com.alibaba.excel.util.StringUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.MapUtils; @@ -27,7 +28,7 @@ import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVRecord; /** - * azz + * read executor * * @author zhuangjiaju */ @@ -66,10 +67,11 @@ public class CsvExcelReadExecutor implements ExcelReadExecutor { csvReadContext.currentSheet(readSheet); int rowIndex = 0; - for (CSVRecord record : parseRecords) { + for (CSVRecord record : parseRecords) { dealRecord(record, rowIndex++); } + // The last sheet is read csvReadContext.analysisEventProcessor().endSheet(csvReadContext); } @@ -95,8 +97,14 @@ public class CsvExcelReadExecutor implements ExcelReadExecutor { while (cellIterator.hasNext()) { String cellString = cellIterator.next(); ReadCellData readCellData = new ReadCellData<>(); - readCellData.setType(CellDataTypeEnum.STRING); - readCellData.setStringValue(cellString); + + // csv is an empty string of whether ,, is read or ,"", + if (StringUtils.isNotBlank(cellString)) { + readCellData.setType(CellDataTypeEnum.STRING); + readCellData.setStringValue(cellString); + } else { + readCellData.setType(CellDataTypeEnum.EMPTY); + } cellMap.put(cellIndex++, readCellData); } diff --git a/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java b/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java index 90a1bbbe..735a0623 100644 --- a/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java +++ b/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java @@ -61,6 +61,14 @@ public abstract class AbstractHolder implements ConfigurationHolder { globalConfiguration.setAutoTrim(basicParameter.getAutoTrim()); } + if (basicParameter.getUse1904windowing() == null) { + if (prentAbstractHolder != null) { + globalConfiguration.setUse1904windowing( + prentAbstractHolder.getGlobalConfiguration().getUse1904windowing()); + } + } else { + globalConfiguration.setUse1904windowing(basicParameter.getUse1904windowing()); + } if (basicParameter.getLocale() == null) { if (prentAbstractHolder != null) { diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvDataFormat.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvDataFormat.java index 1dcc7c2e..42d3f043 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvDataFormat.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvDataFormat.java @@ -45,8 +45,8 @@ public class CsvDataFormat implements DataFormat { if (index != null) { return index; } + index = (short)(formatList.size() + BuiltinFormats.MIN_CUSTOM_DATA_FORMAT_INDEX); formatList.add(format); - index = (short)formatList.size(); formatMap.put(format, index); return index; } @@ -57,7 +57,7 @@ public class CsvDataFormat implements DataFormat { return builtinFormats[index]; } int actualIndex = index - BuiltinFormats.MIN_CUSTOM_DATA_FORMAT_INDEX; - if (actualIndex >= 0 && actualIndex < formatList.size()) { + if (actualIndex < formatList.size()) { return formatList.get(actualIndex); } return null; diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java index a54d0681..fe35ab82 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java @@ -748,7 +748,6 @@ public class CsvSheet implements Sheet, Closeable { switch (csvCell.getCellType()) { case STRING: case ERROR: - case FORMULA: return csvCell.getStringCellValue(); case NUMERIC: Short dataFormat = null; @@ -782,6 +781,9 @@ public class CsvSheet implements Sheet, Closeable { dataFormat = BuiltinFormats.GENERAL; dataFormatString = csvWorkbook.createDataFormat().getFormat(dataFormat); } + if (dataFormatString == null) { + dataFormatString = csvWorkbook.createDataFormat().getFormat(dataFormat); + } return NumberDataFormatterUtils.format(csvCell.getNumericCellValue(), dataFormat, dataFormatString, csvWorkbook.getUse1904windowing(), csvWorkbook.getLocale(), csvWorkbook.getUseScientificFormat()); diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java index 14a708b6..1b313939 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java @@ -50,15 +50,6 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH Boolean convertAllFiled) { super(readBasicParameter, parentAbstractReadHolder); - if (readBasicParameter.getUse1904windowing() == null) { - if (parentAbstractReadHolder != null) { - getGlobalConfiguration() - .setUse1904windowing(parentAbstractReadHolder.getGlobalConfiguration().getUse1904windowing()); - } - } else { - getGlobalConfiguration().setUse1904windowing(readBasicParameter.getUse1904windowing()); - } - if (readBasicParameter.getUseScientificFormat() == null) { if (parentAbstractReadHolder != null) { getGlobalConfiguration().setUseScientificFormat( diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java index 4e8de41f..8066fab6 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java @@ -20,6 +20,6 @@ public class CsvReadWorkbookHolder extends ReadWorkbookHolder { public CsvReadWorkbookHolder(ReadWorkbook readWorkbook) { super(readWorkbook); setExcelType(ExcelTypeEnum.CSV); - csvFormat = CSVFormat.DEFAULT; + this.csvFormat = CSVFormat.DEFAULT; } } diff --git a/src/main/java/com/alibaba/excel/util/DateUtils.java b/src/main/java/com/alibaba/excel/util/DateUtils.java index bc85adf6..68eb01c0 100644 --- a/src/main/java/com/alibaba/excel/util/DateUtils.java +++ b/src/main/java/com/alibaba/excel/util/DateUtils.java @@ -133,7 +133,7 @@ public class DateUtils { */ public static String format(Date date, String dateFormat) { if (date == null) { - return ""; + return null; } if (StringUtils.isEmpty(dateFormat)) { dateFormat = defaultDateFormat; 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 44ba04ee..0c632b6e 100644 --- a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java @@ -17,8 +17,10 @@ import com.alibaba.excel.metadata.data.ImageData; import com.alibaba.excel.metadata.data.WriteCellData; 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.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.metadata.style.WriteCellStyle; @@ -231,6 +233,9 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { } WriteCellData cellDataValue = (WriteCellData)value; if (cellDataValue.getType() != null) { + // Configuration information may not be read here + fillProperty(cellDataValue, excelContentProperty); + return cellDataValue; } else { if (cellDataValue.getData() == null) { @@ -262,6 +267,27 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { return doConvert(currentWriteHolder, clazz, targetType, cell, value, excelContentProperty); } + private void fillProperty(WriteCellData cellDataValue, ExcelContentProperty excelContentProperty) { + switch (cellDataValue.getType()) { + case DATE: + String dateFormat = null; + if (excelContentProperty != null && excelContentProperty.getDateTimeFormatProperty() != null) { + dateFormat = excelContentProperty.getDateTimeFormatProperty().getFormat(); + } + WorkBookUtil.fillDataFormat(cellDataValue, dateFormat, DateUtils.defaultDateFormat); + return; + case NUMBER: + String numberFormat = null; + if (excelContentProperty != null && excelContentProperty.getNumberFormatProperty() != null) { + numberFormat = excelContentProperty.getNumberFormatProperty().getFormat(); + } + WorkBookUtil.fillDataFormat(cellDataValue, numberFormat, null); + return; + default: + return; + } + } + private WriteCellData doConvert(WriteHolder currentWriteHolder, Class clazz, CellDataTypeEnum targetType, Cell cell, Object value, ExcelContentProperty excelContentProperty) { Converter converter = null; diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java index 4be4907b..96d55396 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java @@ -97,14 +97,6 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ Boolean convertAllField) { super(writeBasicParameter, parentAbstractWriteHolder); - if (writeBasicParameter.getUse1904windowing() == null) { - if (parentAbstractWriteHolder != null) { - getGlobalConfiguration() - .setUse1904windowing(parentAbstractWriteHolder.getGlobalConfiguration().getUse1904windowing()); - } - } else { - getGlobalConfiguration().setUse1904windowing(writeBasicParameter.getUse1904windowing()); - } if (writeBasicParameter.getUseScientificFormat() != null) { throw new UnsupportedOperationException("Currently does not support setting useScientificFormat."); diff --git a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java index cb6a1105..626f801e 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java @@ -5,6 +5,7 @@ import java.util.List; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; +import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.fastjson.JSON; import org.junit.Assert; @@ -28,10 +29,14 @@ public class CellDataDataListener extends AnalysisEventListener { + @Override + public void invoke(CsvData data, AnalysisContext context) { + log.info("data:{}", JSON.toJSONString(data)); + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java b/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java index d6f8fb2a..ebc910d1 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java @@ -5,7 +5,6 @@ import java.io.FileInputStream; import java.io.FileReader; import java.io.PrintWriter; import java.util.ArrayList; -import java.util.Date; import java.util.List; import com.alibaba.easyexcel.test.util.TestFileUtil; @@ -38,8 +37,8 @@ public class CsvReadTest { @Test public void read1() throws Exception { - Iterable records = CSVFormat.DEFAULT.parse( - new FileReader("/Users/zhuangjiaju/IdeaProjects/easyexcel/target/test-classes/csvWrite1.csv")); + Iterable records = CSVFormat.DEFAULT.withNullString("").parse( + new FileReader("/Users/zhuangjiaju/IdeaProjects/easyexcel/target/test-classes/t1.csv")); for (CSVRecord record : records) { String lastName = record.get(0); String firstName = record.get(1); @@ -71,6 +70,8 @@ public class CsvReadTest { // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 如果这里想使用03 则 传入excelType参数即可 EasyExcel.write(fileName, CsvData.class).sheet().doWrite(data()); + + EasyExcel.read(fileName, CsvData.class, new CsvDataListeer()).sheet().doRead(); } @Test @@ -85,7 +86,7 @@ public class CsvReadTest { for (int i = 0; i < 10; i++) { CsvData data = new CsvData(); data.setString("字符,串" + i); - data.setDate(new Date()); + //data.setDate(new Date()); data.setDoubleData(0.56); data.setIgnore("忽略" + i); list.add(data);