Browse Source

修改空数据的时候异常

developing
Jiaju Zhuang 3 years ago
parent
commit
2fb54ca91e
  1. 12
      src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java
  2. 8
      src/main/java/com/alibaba/excel/metadata/AbstractHolder.java
  3. 4
      src/main/java/com/alibaba/excel/metadata/csv/CsvDataFormat.java
  4. 4
      src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java
  5. 9
      src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java
  6. 2
      src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java
  7. 2
      src/main/java/com/alibaba/excel/util/DateUtils.java
  8. 26
      src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
  9. 8
      src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
  10. 7
      src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java
  11. 20
      src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvDataListeer.java
  12. 9
      src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java

12
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<String> readCellData = new ReadCellData<>();
// csv is an empty string of whether <code>,,</code> is read or <code>,"",</code>
if (StringUtils.isNotBlank(cellString)) {
readCellData.setType(CellDataTypeEnum.STRING);
readCellData.setStringValue(cellString);
} else {
readCellData.setType(CellDataTypeEnum.EMPTY);
}
cellMap.put(cellIndex++, readCellData);
}

8
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) {

4
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;

4
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());

9
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(

2
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;
}
}

2
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;

26
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;

8
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.");

7
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<CellDataReadData
Assert.assertEquals(list.size(), 1);
CellDataReadData cellDataData = list.get(0);
Assert.assertEquals(cellDataData.getDate().getData(), "2020年01月01日");
Assert.assertEquals("2020年01月01日", cellDataData.getDate().getData());
Assert.assertEquals((long)cellDataData.getInteger1().getData(), 2L);
Assert.assertEquals((long)cellDataData.getInteger2(), 2L);
if (context.readWorkbookHolder().getExcelType() != ExcelTypeEnum.CSV) {
Assert.assertEquals(cellDataData.getFormulaValue().getFormulaData().getFormulaValue(), "B2+C2");
} else {
Assert.assertEquals(cellDataData.getFormulaValue().getData(), "");
}
LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0)));
}
}

20
src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvDataListeer.java

@ -0,0 +1,20 @@
package com.alibaba.easyexcel.test.temp.csv;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class CsvDataListeer extends AnalysisEventListener<CsvData> {
@Override
public void invoke(CsvData data, AnalysisContext context) {
log.info("data:{}", JSON.toJSONString(data));
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}
}

9
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<CSVRecord> records = CSVFormat.DEFAULT.parse(
new FileReader("/Users/zhuangjiaju/IdeaProjects/easyexcel/target/test-classes/csvWrite1.csv"));
Iterable<CSVRecord> 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);

Loading…
Cancel
Save