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 e98b3580..d411ef0d 100644 --- a/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java +++ b/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java @@ -93,10 +93,12 @@ public class CsvExcelReadExecutor implements ExcelReadExecutor { private void dealRecord(CSVRecord record, int rowIndex) { Map cellMap = new LinkedHashMap<>(); Iterator cellIterator = record.iterator(); - int cellIndex = 0; + int columnIndex = 0; while (cellIterator.hasNext()) { String cellString = cellIterator.next(); ReadCellData readCellData = new ReadCellData<>(); + readCellData.setRowIndex(rowIndex); + readCellData.setColumnIndex(columnIndex); // csv is an empty string of whether ,, is read or ,"", if (StringUtils.isNotBlank(cellString)) { @@ -105,7 +107,7 @@ public class CsvExcelReadExecutor implements ExcelReadExecutor { } else { readCellData.setType(CellDataTypeEnum.EMPTY); } - cellMap.put(cellIndex++, readCellData); + cellMap.put(columnIndex++, readCellData); } RowTypeEnum rowType = MapUtils.isEmpty(cellMap) ? RowTypeEnum.EMPTY : RowTypeEnum.DATA; diff --git a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java index 9feb1515..adfa6589 100644 --- a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java +++ b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java @@ -53,10 +53,6 @@ public class ModelBuildEventListener implements ReadListener cellData = cellDataMap.get(index); - if (cellData.getType() == CellDataTypeEnum.EMPTY) { - continue; - } ExcelContentProperty excelContentProperty = contentPropertyMap.get(index); Object value = ConverterUtils.convertToJavaObject(cellData, excelContentProperty.getField(), excelContentProperty, readSheetHolder.converterMap(), context, diff --git a/src/main/java/com/alibaba/excel/util/ConverterUtils.java b/src/main/java/com/alibaba/excel/util/ConverterUtils.java index e0e139f7..b666be6e 100644 --- a/src/main/java/com/alibaba/excel/util/ConverterUtils.java +++ b/src/main/java/com/alibaba/excel/util/ConverterUtils.java @@ -8,6 +8,7 @@ import java.util.Map; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.ConverterKeyBuild; +import com.alibaba.excel.converters.NullableObjectConverter; import com.alibaba.excel.converters.ReadConverterContext; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.exception.ExcelDataConvertException; @@ -131,6 +132,13 @@ public class ConverterUtils { if (contentProperty != null) { converter = contentProperty.getConverter(); } + + boolean canNotConverterEmpty = cellData.getType() == CellDataTypeEnum.EMPTY + && !(converter instanceof NullableObjectConverter); + if (canNotConverterEmpty) { + return null; + } + if (converter == null) { converter = converterMap.get(ConverterKeyBuild.buildKey(clazz, cellData.getType())); } @@ -138,6 +146,7 @@ public class ConverterUtils { throw new ExcelDataConvertException(rowIndex, columnIndex, cellData, contentProperty, "Converter not found, convert " + cellData.getType() + " to " + clazz.getName()); } + try { return converter.convertToJavaData(new ReadConverterContext<>(cellData, contentProperty, context)); } catch (Exception e) { diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java index e172140d..d6503c6c 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java @@ -1,19 +1,12 @@ package com.alibaba.excel.write; -import java.lang.reflect.Field; import java.util.Collection; -import java.util.List; - -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.util.CellRangeAddress; -import org.apache.poi.xssf.streaming.SXSSFSheet; -import org.apache.poi.xssf.usermodel.XSSFSheet; import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.context.WriteContextImpl; import com.alibaba.excel.enums.WriteTypeEnum; import com.alibaba.excel.exception.ExcelGenerateException; -import com.alibaba.excel.util.FieldUtils; +import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.write.executor.ExcelWriteAddExecutor; import com.alibaba.excel.write.executor.ExcelWriteFillExecutor; @@ -22,6 +15,8 @@ import com.alibaba.excel.write.metadata.WriteTable; import com.alibaba.excel.write.metadata.WriteWorkbook; import com.alibaba.excel.write.metadata.fill.FillConfig; +import org.apache.poi.ss.util.CellRangeAddress; + /** * @author jipengfei */ @@ -77,6 +72,9 @@ public class ExcelBuilderImpl implements ExcelBuilder { if (context.writeWorkbookHolder().getTempTemplateInputStream() == null) { throw new ExcelGenerateException("Calling the 'fill' method must use a template."); } + if (context.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.CSV) { + throw new ExcelGenerateException("csv does not support filling data."); + } context.currentSheet(writeSheet, WriteTypeEnum.FILL); if (excelWriteFillExecutor == null) { excelWriteFillExecutor = new ExcelWriteFillExecutor(context); 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 0c632b6e..dfd327c1 100644 --- a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java @@ -301,14 +301,14 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { } converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz, targetType)); } + if (value == 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() + "."); } - if (value == null && !(converter instanceof NullableObjectConverter)) { - return new WriteCellData<>(CellDataTypeEnum.EMPTY); - } WriteCellData cellData; try { cellData = ((Converter)converter).convertToExcelData( 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 626f801e..35829e0e 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 @@ -35,7 +35,7 @@ public class CellDataDataListener extends AnalysisEventListener list = new ArrayList(); + List list = new ArrayList<>(); ImageData imageData = new ImageData(); list.add(imageData); String imagePath = TestFileUtil.getPath() + "converter" + File.separator + "img.jpg"; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java index d5da81ad..4a11f8b8 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java @@ -5,16 +5,17 @@ import java.text.ParseException; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.exception.ExcelCommonException; +import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.util.DateUtils; import com.alibaba.fastjson.JSON; +import org.junit.Assert; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ @@ -61,10 +62,14 @@ public class ReadAllConverterDataListener extends AnalysisEventListener excludeColumnIndexes = new HashSet(); @@ -147,7 +171,6 @@ public class ExcludeOrIncludeDataTest { Assert.assertEquals("column3", record.get(1)); } - private List data() { List list = new ArrayList(); ExcludeOrIncludeData excludeOrIncludeData = new ExcludeOrIncludeData(); diff --git a/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java index a96d133e..a3354f35 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java @@ -1,22 +1,17 @@ package com.alibaba.easyexcel.test.core.handler; import java.io.File; -import java.text.ParseException; import java.util.ArrayList; import java.util.List; +import com.alibaba.easyexcel.test.util.TestFileUtil; +import com.alibaba.excel.EasyExcel; + import org.junit.BeforeClass; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; -import com.alibaba.easyexcel.test.core.head.ListHeadDataListener; -import com.alibaba.easyexcel.test.core.simple.SimpleData; -import com.alibaba.easyexcel.test.util.TestFileUtil; -import com.alibaba.excel.EasyExcel; -import com.alibaba.excel.util.DateUtils; -import com.alibaba.excel.write.handler.WorkbookWriteHandler; - /** * * @author Jiaju Zhuang @@ -26,11 +21,14 @@ public class WriteHandlerTest { private static File file07; private static File file03; + private static File fileCsv; + @BeforeClass public static void init() { file07 = TestFileUtil.createNewFile("writeHandler07.xlsx"); file03 = TestFileUtil.createNewFile("writeHandler03.xls"); + fileCsv = TestFileUtil.createNewFile("writeHandlerCsv.csv"); } @Test @@ -44,27 +42,42 @@ public class WriteHandlerTest { } @Test - public void t03SheetWrite07() throws Exception { + public void t03WorkbookWriteCsv() throws Exception { + workbookWrite(fileCsv); + } + + + @Test + public void t11SheetWrite07() throws Exception { sheetWrite(file07); } @Test - public void t04SheetWrite03() throws Exception { + public void t12SheetWrite03() throws Exception { sheetWrite(file03); } @Test - public void t05TableWrite07() throws Exception { - workbookWrite(file07); + public void t13SheetWriteCsv() throws Exception { + sheetWrite(fileCsv); + } + + @Test + public void t21TableWrite07() throws Exception { tableWrite(file07); } @Test - public void t06TableWrite03() throws Exception { + public void t22TableWrite03() throws Exception { tableWrite(file03); } + @Test + public void t23TableWriteCsv() throws Exception { + tableWrite(fileCsv); + } + private void workbookWrite(File file) { WriteHandler writeHandler = new WriteHandler(); EasyExcel.write(file).head(WriteHandlerData.class).registerWriteHandler(writeHandler).sheet().doWrite(data()); diff --git a/src/test/resources/converter/converterCsv.csv b/src/test/resources/converter/converterCsv.csv new file mode 100644 index 00000000..c61f06ca --- /dev/null +++ b/src/test/resources/converter/converterCsv.csv @@ -0,0 +1,2 @@ +大数的布尔(不支持),大数的数字,大数的字符串,布尔的布尔,布尔的数字(不支持),布尔的字符串,字节的布尔(不支持),字节的数字,字节的字符串,日期的数字,日期的字符串,双精度浮点的布尔(不支持),双精度浮点的数字,双精度浮点的字符串,浮点的布尔(不支持),浮点的数字,浮点的字符串,整型的布尔(不支持),整型的数字,整型的字符串,长整型的布尔(不支持),长整型的数字,长整型的字符串,短整型的布尔(不支持),短整型的数字,短整型的字符串,字符串的布尔,字符串的数字,字符串的字符串,字符串的错误,字符串的数字公式,字符串的字符串公式,字符串的数字日期 +1,1,1,TRUE,TRUE,TRUE,1,1,1,2020-01-01 01:01:01,2020-01-01 01:01:01,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,TRUE,1,测试,#VALUE!,2,1测试,2020-01-01 01:01:01 \ No newline at end of file