Browse Source

完成部分csv测试案例

developing
Jiaju Zhuang 3 years ago
parent
commit
729bda5d0e
  1. 6
      src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java
  2. 11
      src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java
  3. 9
      src/main/java/com/alibaba/excel/util/ConverterUtils.java
  4. 14
      src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
  5. 6
      src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
  6. 2
      src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java
  7. 23
      src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java
  8. 17
      src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java
  9. 13
      src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatTest.java
  10. 14
      src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataTest.java
  11. 51
      src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeDataTest.java
  12. 39
      src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java
  13. 2
      src/test/resources/converter/converterCsv.csv

6
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) { private void dealRecord(CSVRecord record, int rowIndex) {
Map<Integer, Cell> cellMap = new LinkedHashMap<>(); Map<Integer, Cell> cellMap = new LinkedHashMap<>();
Iterator<String> cellIterator = record.iterator(); Iterator<String> cellIterator = record.iterator();
int cellIndex = 0; int columnIndex = 0;
while (cellIterator.hasNext()) { while (cellIterator.hasNext()) {
String cellString = cellIterator.next(); String cellString = cellIterator.next();
ReadCellData<String> readCellData = new ReadCellData<>(); ReadCellData<String> readCellData = new ReadCellData<>();
readCellData.setRowIndex(rowIndex);
readCellData.setColumnIndex(columnIndex);
// csv is an empty string of whether <code>,,</code> is read or <code>,"",</code> // csv is an empty string of whether <code>,,</code> is read or <code>,"",</code>
if (StringUtils.isNotBlank(cellString)) { if (StringUtils.isNotBlank(cellString)) {
@ -105,7 +107,7 @@ public class CsvExcelReadExecutor implements ExcelReadExecutor {
} else { } else {
readCellData.setType(CellDataTypeEnum.EMPTY); readCellData.setType(CellDataTypeEnum.EMPTY);
} }
cellMap.put(cellIndex++, readCellData); cellMap.put(columnIndex++, readCellData);
} }
RowTypeEnum rowType = MapUtils.isEmpty(cellMap) ? RowTypeEnum.EMPTY : RowTypeEnum.DATA; RowTypeEnum rowType = MapUtils.isEmpty(cellMap) ? RowTypeEnum.EMPTY : RowTypeEnum.DATA;

11
src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java

@ -53,10 +53,6 @@ public class ModelBuildEventListener implements ReadListener<Map<Integer, ReadCe
index++; index++;
} }
index++; index++;
if (cellData.getType() == CellDataTypeEnum.EMPTY) {
map.put(key, null);
continue;
}
map.put(key, map.put(key,
(String)ConverterUtils.convertToJavaObject(cellData, null, null, readSheetHolder.converterMap(), (String)ConverterUtils.convertToJavaObject(cellData, null, null, readSheetHolder.converterMap(),
context, context.readRowHolder().getRowIndex(), key)); context, context.readRowHolder().getRowIndex(), key));
@ -78,10 +74,6 @@ public class ModelBuildEventListener implements ReadListener<Map<Integer, ReadCe
index++; index++;
} }
index++; index++;
if (cellData.getType() == CellDataTypeEnum.EMPTY) {
list.add(null);
continue;
}
list.add( list.add(
(String)ConverterUtils.convertToJavaObject(cellData, null, null, readSheetHolder.converterMap(), (String)ConverterUtils.convertToJavaObject(cellData, null, null, readSheetHolder.converterMap(),
context, context.readRowHolder().getRowIndex(), key)); context, context.readRowHolder().getRowIndex(), key));
@ -115,9 +107,6 @@ public class ModelBuildEventListener implements ReadListener<Map<Integer, ReadCe
continue; continue;
} }
ReadCellData<?> cellData = cellDataMap.get(index); ReadCellData<?> cellData = cellDataMap.get(index);
if (cellData.getType() == CellDataTypeEnum.EMPTY) {
continue;
}
ExcelContentProperty excelContentProperty = contentPropertyMap.get(index); ExcelContentProperty excelContentProperty = contentPropertyMap.get(index);
Object value = ConverterUtils.convertToJavaObject(cellData, excelContentProperty.getField(), Object value = ConverterUtils.convertToJavaObject(cellData, excelContentProperty.getField(),
excelContentProperty, readSheetHolder.converterMap(), context, excelContentProperty, readSheetHolder.converterMap(), context,

9
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.context.AnalysisContext;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ConverterKeyBuild; import com.alibaba.excel.converters.ConverterKeyBuild;
import com.alibaba.excel.converters.NullableObjectConverter;
import com.alibaba.excel.converters.ReadConverterContext; import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.exception.ExcelDataConvertException; import com.alibaba.excel.exception.ExcelDataConvertException;
@ -131,6 +132,13 @@ public class ConverterUtils {
if (contentProperty != null) { if (contentProperty != null) {
converter = contentProperty.getConverter(); converter = contentProperty.getConverter();
} }
boolean canNotConverterEmpty = cellData.getType() == CellDataTypeEnum.EMPTY
&& !(converter instanceof NullableObjectConverter);
if (canNotConverterEmpty) {
return null;
}
if (converter == null) { if (converter == null) {
converter = converterMap.get(ConverterKeyBuild.buildKey(clazz, cellData.getType())); converter = converterMap.get(ConverterKeyBuild.buildKey(clazz, cellData.getType()));
} }
@ -138,6 +146,7 @@ public class ConverterUtils {
throw new ExcelDataConvertException(rowIndex, columnIndex, cellData, contentProperty, throw new ExcelDataConvertException(rowIndex, columnIndex, cellData, contentProperty,
"Converter not found, convert " + cellData.getType() + " to " + clazz.getName()); "Converter not found, convert " + cellData.getType() + " to " + clazz.getName());
} }
try { try {
return converter.convertToJavaData(new ReadConverterContext<>(cellData, contentProperty, context)); return converter.convertToJavaData(new ReadConverterContext<>(cellData, contentProperty, context));
} catch (Exception e) { } catch (Exception e) {

14
src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java

@ -1,19 +1,12 @@
package com.alibaba.excel.write; package com.alibaba.excel.write;
import java.lang.reflect.Field;
import java.util.Collection; 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.WriteContext;
import com.alibaba.excel.context.WriteContextImpl; import com.alibaba.excel.context.WriteContextImpl;
import com.alibaba.excel.enums.WriteTypeEnum; import com.alibaba.excel.enums.WriteTypeEnum;
import com.alibaba.excel.exception.ExcelGenerateException; 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.util.FileUtils;
import com.alibaba.excel.write.executor.ExcelWriteAddExecutor; import com.alibaba.excel.write.executor.ExcelWriteAddExecutor;
import com.alibaba.excel.write.executor.ExcelWriteFillExecutor; 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.WriteWorkbook;
import com.alibaba.excel.write.metadata.fill.FillConfig; import com.alibaba.excel.write.metadata.fill.FillConfig;
import org.apache.poi.ss.util.CellRangeAddress;
/** /**
* @author jipengfei * @author jipengfei
*/ */
@ -77,6 +72,9 @@ public class ExcelBuilderImpl implements ExcelBuilder {
if (context.writeWorkbookHolder().getTempTemplateInputStream() == null) { if (context.writeWorkbookHolder().getTempTemplateInputStream() == null) {
throw new ExcelGenerateException("Calling the 'fill' method must use a template."); 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); context.currentSheet(writeSheet, WriteTypeEnum.FILL);
if (excelWriteFillExecutor == null) { if (excelWriteFillExecutor == null) {
excelWriteFillExecutor = new ExcelWriteFillExecutor(context); excelWriteFillExecutor = new ExcelWriteFillExecutor(context);

6
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)); converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz, targetType));
} }
if (value == null && !(converter instanceof NullableObjectConverter)) {
return new WriteCellData<>(CellDataTypeEnum.EMPTY);
}
if (converter == null) { if (converter == null) {
throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(),
new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty, new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty,
"Can not find 'Converter' support class " + clazz.getSimpleName() + "."); "Can not find 'Converter' support class " + clazz.getSimpleName() + ".");
} }
if (value == null && !(converter instanceof NullableObjectConverter)) {
return new WriteCellData<>(CellDataTypeEnum.EMPTY);
}
WriteCellData<?> cellData; WriteCellData<?> cellData;
try { try {
cellData = ((Converter<Object>)converter).convertToExcelData( cellData = ((Converter<Object>)converter).convertToExcelData(

2
src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java

@ -35,7 +35,7 @@ public class CellDataDataListener extends AnalysisEventListener<CellDataReadData
if (context.readWorkbookHolder().getExcelType() != ExcelTypeEnum.CSV) { if (context.readWorkbookHolder().getExcelType() != ExcelTypeEnum.CSV) {
Assert.assertEquals(cellDataData.getFormulaValue().getFormulaData().getFormulaValue(), "B2+C2"); Assert.assertEquals(cellDataData.getFormulaValue().getFormulaData().getFormulaValue(), "B2+C2");
} else { } else {
Assert.assertEquals(cellDataData.getFormulaValue().getData(), ""); Assert.assertNull(cellDataData.getFormulaValue().getData());
} }
LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0)));
} }

23
src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java

@ -18,7 +18,6 @@ import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
/** /**
*
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ -26,6 +25,7 @@ public class ConverterDataTest {
private static File file07; private static File file07;
private static File file03; private static File file03;
private static File fileCsv;
private static File fileImage07; private static File fileImage07;
private static File fileImage03; private static File fileImage03;
@ -33,6 +33,7 @@ public class ConverterDataTest {
public static void init() { public static void init() {
file07 = TestFileUtil.createNewFile("converter07.xlsx"); file07 = TestFileUtil.createNewFile("converter07.xlsx");
file03 = TestFileUtil.createNewFile("converter03.xls"); file03 = TestFileUtil.createNewFile("converter03.xls");
fileCsv = TestFileUtil.createNewFile("converterCsv.csv");
fileImage07 = TestFileUtil.createNewFile("converterImage07.xlsx"); fileImage07 = TestFileUtil.createNewFile("converterImage07.xlsx");
fileImage03 = TestFileUtil.createNewFile("converterImage03.xls"); fileImage03 = TestFileUtil.createNewFile("converterImage03.xls");
} }
@ -47,35 +48,45 @@ public class ConverterDataTest {
readAndWrite(file03); readAndWrite(file03);
} }
@Test
public void t03ReadAndWriteCsv() throws Exception {
readAndWrite(fileCsv);
}
private void readAndWrite(File file) throws Exception { private void readAndWrite(File file) throws Exception {
EasyExcel.write(file, ConverterWriteData.class).sheet().doWrite(data()); EasyExcel.write(file, ConverterWriteData.class).sheet().doWrite(data());
EasyExcel.read(file, ConverterReadData.class, new ConverterDataListener()).sheet().doRead(); EasyExcel.read(file, ConverterReadData.class, new ConverterDataListener()).sheet().doRead();
} }
@Test @Test
public void t03ReadAllConverter07() { public void t11ReadAllConverter07() {
readAllConverter("converter" + File.separator + "converter07.xlsx"); readAllConverter("converter" + File.separator + "converter07.xlsx");
} }
@Test @Test
public void t04ReadAllConverter03() { public void t12ReadAllConverter03() {
readAllConverter("converter" + File.separator + "converter03.xls"); readAllConverter("converter" + File.separator + "converter03.xls");
} }
@Test @Test
public void t05WriteImage07() throws Exception { public void t13ReadAllConverterCsv() {
readAllConverter("converter" + File.separator + "converterCsv.csv");
}
@Test
public void t21WriteImage07() throws Exception {
writeImage(fileImage07); writeImage(fileImage07);
} }
@Test @Test
public void t06WriteImage03() throws Exception { public void t22WriteImage03() throws Exception {
writeImage(fileImage03); writeImage(fileImage03);
} }
private void writeImage(File file) throws Exception { private void writeImage(File file) throws Exception {
InputStream inputStream = null; InputStream inputStream = null;
try { try {
List<ImageData> list = new ArrayList<ImageData>(); List<ImageData> list = new ArrayList<>();
ImageData imageData = new ImageData(); ImageData imageData = new ImageData();
list.add(imageData); list.add(imageData);
String imagePath = TestFileUtil.getPath() + "converter" + File.separator + "img.jpg"; String imagePath = TestFileUtil.getPath() + "converter" + File.separator + "img.jpg";

17
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.ArrayList;
import java.util.List; 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.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelCommonException; import com.alibaba.excel.exception.ExcelCommonException;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.DateUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@ -61,10 +62,14 @@ public class ReadAllConverterDataListener extends AnalysisEventListener<ReadAllC
Assert.assertEquals((long)data.getShortBoolean(), 1L); Assert.assertEquals((long)data.getShortBoolean(), 1L);
Assert.assertEquals((long)data.getShortNumber(), 1L); Assert.assertEquals((long)data.getShortNumber(), 1L);
Assert.assertEquals((long)data.getShortString(), 1L); Assert.assertEquals((long)data.getShortString(), 1L);
Assert.assertEquals(data.getStringBoolean(), "true"); Assert.assertEquals(data.getStringBoolean().toLowerCase(), "true");
Assert.assertEquals(data.getStringString(), "测试"); Assert.assertEquals(data.getStringString(), "测试");
Assert.assertEquals(data.getStringError(), "#VALUE!"); Assert.assertEquals(data.getStringError(), "#VALUE!");
Assert.assertEquals("2020-1-1 1:01", data.getStringNumberDate()); if (context.readWorkbookHolder().getExcelType() != ExcelTypeEnum.CSV) {
Assert.assertEquals("2020-1-1 1:01", data.getStringNumberDate());
} else {
Assert.assertEquals("2020-01-01 01:01:01", data.getStringNumberDate());
}
double doubleStringFormulaNumber = new BigDecimal(data.getStringFormulaNumber()).doubleValue(); double doubleStringFormulaNumber = new BigDecimal(data.getStringFormulaNumber()).doubleValue();
Assert.assertEquals(doubleStringFormulaNumber, 2.0, 0.0); Assert.assertEquals(doubleStringFormulaNumber, 2.0, 0.0);
Assert.assertEquals(data.getStringFormulaString(), "1测试"); Assert.assertEquals(data.getStringFormulaString(), "1测试");

13
src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatTest.java

@ -4,21 +4,20 @@ import java.io.File;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.junit.runners.MethodSorters;
import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
/** /**
*
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class DateFormatTest { public class DateFormatTest {
private static final Logger LOGGER = LoggerFactory.getLogger(DateFormatTest.class);
private static File file07; private static File file07;
private static File file03; private static File file03;

14
src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataTest.java

@ -6,16 +6,15 @@ import java.io.FileOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
/** /**
*
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ -23,11 +22,13 @@ public class ExceptionDataTest {
private static File file07; private static File file07;
private static File file03; private static File file03;
private static File fileCsv;
@BeforeClass @BeforeClass
public static void init() { public static void init() {
file07 = TestFileUtil.createNewFile("simple07.xlsx"); file07 = TestFileUtil.createNewFile("simple07.xlsx");
file03 = TestFileUtil.createNewFile("simple03.xls"); file03 = TestFileUtil.createNewFile("simple03.xls");
fileCsv = TestFileUtil.createNewFile("simpleCsv.csv");
} }
@Test @Test
@ -40,6 +41,11 @@ public class ExceptionDataTest {
readAndWrite(file03); readAndWrite(file03);
} }
@Test
public void t03ReadAndWriteCsv() throws Exception {
readAndWrite(fileCsv);
}
private void readAndWrite(File file) throws Exception { private void readAndWrite(File file) throws Exception {
EasyExcel.write(new FileOutputStream(file), ExceptionData.class).sheet().doWrite(data()); EasyExcel.write(new FileOutputStream(file), ExceptionData.class).sheet().doWrite(data());
EasyExcel.read(new FileInputStream(file), ExceptionData.class, new ExceptionDataListener()).sheet().doRead(); EasyExcel.read(new FileInputStream(file), ExceptionData.class, new ExceptionDataListener()).sheet().doRead();

51
src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeDataTest.java

@ -2,23 +2,20 @@ package com.alibaba.easyexcel.test.core.excludeorinclude;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.core.sort.SortData;
import com.alibaba.easyexcel.test.core.sort.SortDataListener;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@ -27,23 +24,31 @@ public class ExcludeOrIncludeDataTest {
private static File excludeIndex07; private static File excludeIndex07;
private static File excludeIndex03; private static File excludeIndex03;
private static File excludeIndexCsv;
private static File excludeFiledName07; private static File excludeFiledName07;
private static File excludeFiledName03; private static File excludeFiledName03;
private static File excludeFiledNameCsv;
private static File includeIndex07; private static File includeIndex07;
private static File includeIndex03; private static File includeIndex03;
private static File includeIndexCsv;
private static File includeFiledName07; private static File includeFiledName07;
private static File includeFiledName03; private static File includeFiledName03;
private static File includeFiledNameCsv;
@BeforeClass @BeforeClass
public static void init() { public static void init() {
excludeIndex07 = TestFileUtil.createNewFile("excludeIndex.xlsx"); excludeIndex07 = TestFileUtil.createNewFile("excludeIndex.xlsx");
excludeIndex03 = TestFileUtil.createNewFile("excludeIndex.xls"); excludeIndex03 = TestFileUtil.createNewFile("excludeIndex.xls");
excludeIndexCsv = TestFileUtil.createNewFile("excludeIndex.csv");
excludeFiledName07 = TestFileUtil.createNewFile("excludeFiledName.xlsx"); excludeFiledName07 = TestFileUtil.createNewFile("excludeFiledName.xlsx");
excludeFiledName03 = TestFileUtil.createNewFile("excludeFiledName.xls"); excludeFiledName03 = TestFileUtil.createNewFile("excludeFiledName.xls");
excludeFiledNameCsv = TestFileUtil.createNewFile("excludeFiledName.csv");
includeIndex07 = TestFileUtil.createNewFile("includeIndex.xlsx"); includeIndex07 = TestFileUtil.createNewFile("includeIndex.xlsx");
includeIndex03 = TestFileUtil.createNewFile("includeIndex.xls"); includeIndex03 = TestFileUtil.createNewFile("includeIndex.xls");
includeIndexCsv = TestFileUtil.createNewFile("includeIndex.csv");
includeFiledName07 = TestFileUtil.createNewFile("includeFiledName.xlsx"); includeFiledName07 = TestFileUtil.createNewFile("includeFiledName.xlsx");
includeFiledName03 = TestFileUtil.createNewFile("includeFiledName.xls"); includeFiledName03 = TestFileUtil.createNewFile("includeFiledName.xls");
includeFiledNameCsv = TestFileUtil.createNewFile("includeFiledName.csv");
} }
@Test @Test
@ -52,41 +57,60 @@ public class ExcludeOrIncludeDataTest {
} }
@Test @Test
public void t02ExcludeIndex07() { public void t02ExcludeIndex03() {
excludeIndex(excludeIndex03); excludeIndex(excludeIndex03);
} }
@Test @Test
public void t03ExcludeFiledName07() { public void t03ExcludeIndexCsv() {
excludeIndex(excludeIndexCsv);
}
@Test
public void t11ExcludeFiledName07() {
excludeFiledName(excludeFiledName07); excludeFiledName(excludeFiledName07);
} }
@Test @Test
public void t04ExcludeFiledName07() { public void t12ExcludeFiledName03() {
excludeFiledName(excludeFiledName03); excludeFiledName(excludeFiledName03);
} }
@Test
public void t13ExcludeFiledNameCsv() {
excludeFiledName(excludeFiledNameCsv);
}
@Test @Test
public void t05IncludeIndex07() { public void t21IncludeIndex07() {
includeIndex(includeIndex07); includeIndex(includeIndex07);
} }
@Test @Test
public void t06IncludeIndex07() { public void t22IncludeIndex03() {
includeIndex(includeIndex03); includeIndex(includeIndex03);
} }
@Test @Test
public void t07IncludeFiledName07() { public void t23IncludeIndexCsv() {
includeIndex(includeIndexCsv);
}
@Test
public void t31IncludeFiledName07() {
includeFiledName(includeFiledName07); includeFiledName(includeFiledName07);
} }
@Test @Test
public void t08IncludeFiledName07() { public void t32IncludeFiledName03() {
includeFiledName(includeFiledName03); includeFiledName(includeFiledName03);
} }
@Test
public void t33IncludeFiledNameCsv() {
includeFiledName(includeFiledNameCsv);
}
private void excludeIndex(File file) { private void excludeIndex(File file) {
Set<Integer> excludeColumnIndexes = new HashSet<Integer>(); Set<Integer> excludeColumnIndexes = new HashSet<Integer>();
@ -147,7 +171,6 @@ public class ExcludeOrIncludeDataTest {
Assert.assertEquals("column3", record.get(1)); Assert.assertEquals("column3", record.get(1));
} }
private List<ExcludeOrIncludeData> data() { private List<ExcludeOrIncludeData> data() {
List<ExcludeOrIncludeData> list = new ArrayList<ExcludeOrIncludeData>(); List<ExcludeOrIncludeData> list = new ArrayList<ExcludeOrIncludeData>();
ExcludeOrIncludeData excludeOrIncludeData = new ExcludeOrIncludeData(); ExcludeOrIncludeData excludeOrIncludeData = new ExcludeOrIncludeData();

39
src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java

@ -1,22 +1,17 @@
package com.alibaba.easyexcel.test.core.handler; package com.alibaba.easyexcel.test.core.handler;
import java.io.File; import java.io.File;
import java.text.ParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.runners.MethodSorters; 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 * @author Jiaju Zhuang
@ -26,11 +21,14 @@ public class WriteHandlerTest {
private static File file07; private static File file07;
private static File file03; private static File file03;
private static File fileCsv;
@BeforeClass @BeforeClass
public static void init() { public static void init() {
file07 = TestFileUtil.createNewFile("writeHandler07.xlsx"); file07 = TestFileUtil.createNewFile("writeHandler07.xlsx");
file03 = TestFileUtil.createNewFile("writeHandler03.xls"); file03 = TestFileUtil.createNewFile("writeHandler03.xls");
fileCsv = TestFileUtil.createNewFile("writeHandlerCsv.csv");
} }
@Test @Test
@ -44,27 +42,42 @@ public class WriteHandlerTest {
} }
@Test @Test
public void t03SheetWrite07() throws Exception { public void t03WorkbookWriteCsv() throws Exception {
workbookWrite(fileCsv);
}
@Test
public void t11SheetWrite07() throws Exception {
sheetWrite(file07); sheetWrite(file07);
} }
@Test @Test
public void t04SheetWrite03() throws Exception { public void t12SheetWrite03() throws Exception {
sheetWrite(file03); sheetWrite(file03);
} }
@Test @Test
public void t05TableWrite07() throws Exception { public void t13SheetWriteCsv() throws Exception {
workbookWrite(file07); sheetWrite(fileCsv);
}
@Test
public void t21TableWrite07() throws Exception {
tableWrite(file07); tableWrite(file07);
} }
@Test @Test
public void t06TableWrite03() throws Exception { public void t22TableWrite03() throws Exception {
tableWrite(file03); tableWrite(file03);
} }
@Test
public void t23TableWriteCsv() throws Exception {
tableWrite(fileCsv);
}
private void workbookWrite(File file) { private void workbookWrite(File file) {
WriteHandler writeHandler = new WriteHandler(); WriteHandler writeHandler = new WriteHandler();
EasyExcel.write(file).head(WriteHandlerData.class).registerWriteHandler(writeHandler).sheet().doWrite(data()); EasyExcel.write(file).head(WriteHandlerData.class).registerWriteHandler(writeHandler).sheet().doWrite(data());

2
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
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
Loading…
Cancel
Save