diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java index 96f8eda2..031ef6b2 100644 --- a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java @@ -61,7 +61,7 @@ public class AnalysisContextImpl implements AnalysisContext { currentReadHolder = readSheetHolder; selectSheet(excelExecutor); if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Began to read:{}", readSheet); + LOGGER.debug("Began to read:{}", readSheetHolder); } } diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index 2b1f7d7c..5ab62ad6 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -13,6 +13,7 @@ import org.apache.poi.ss.util.CellRangeAddress; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.alibaba.excel.enums.WriteLastRowType; import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.util.WorkBookUtil; @@ -198,6 +199,11 @@ public class WriteContextImpl implements WriteContext { return; } int lastRowNum = writeSheetHolder.getSheet().getLastRowNum(); + // 'lastRowNum' doesn't matter if it has one or zero,is's zero + if (lastRowNum == 0 && WriteLastRowType.HAVE_DATA == writeSheetHolder.getWriteLastRowType()) { + lastRowNum = 1; + } + writeSheetHolder.setWriteLastRowType(WriteLastRowType.HAVE_DATA); int rowIndex = lastRowNum + currentWriteHolder.relativeHeadRowIndex(); // Combined head addMergedRegionToCurrentSheet(excelWriteHeadProperty, rowIndex); diff --git a/src/main/java/com/alibaba/excel/enums/WriteLastRowType.java b/src/main/java/com/alibaba/excel/enums/WriteLastRowType.java new file mode 100644 index 00000000..090af3a5 --- /dev/null +++ b/src/main/java/com/alibaba/excel/enums/WriteLastRowType.java @@ -0,0 +1,17 @@ +package com.alibaba.excel.enums; + +/** + * The types of write last row + * + * @author zhuangjiaju + **/ +public enum WriteLastRowType { + /** + * Tables are created without templates ,And any data has been written; + */ + EMPTY, + /** + * It's supposed to have data in it.Tables are created with templates ,or any data has been written; + */ + HAVE_DATA,; +} diff --git a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java index cdbdeed1..2f117e15 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java @@ -2,7 +2,7 @@ package com.alibaba.excel.metadata.property; import java.lang.reflect.Field; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -113,7 +113,7 @@ public class ExcelHeadProperty { // When the parent class is null, it indicates that the parent class (Object class) has reached the top // level. while (tempClass != null) { - fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields())); + Collections.addAll(fieldList, tempClass.getDeclaredFields()); // Get the parent class and give it to yourself tempClass = tempClass.getSuperclass(); } @@ -167,7 +167,7 @@ public class ExcelHeadProperty { if (notForceName) { tmpHeadList.add(field.getName()); } else { - tmpHeadList = Arrays.asList(excelProperty.value()); + Collections.addAll(tmpHeadList, excelProperty.value()); } Head head = new Head(index, field.getName(), tmpHeadList, forceIndex, !notForceName); ExcelContentProperty excelContentProperty = new ExcelContentProperty(); 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 a84f6471..9769b39a 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 @@ -60,25 +60,28 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH getGlobalConfiguration().setUse1904windowing(readBasicParameter.getUse1904windowing()); } + // Initialization property + this.excelReadHeadProperty = new ExcelReadHeadProperty(getClazz(), getHead(), convertAllFiled); if (readBasicParameter.getHeadRowNumber() == null) { if (parentAbstractReadHolder == null) { - this.headRowNumber = 1; + if (excelReadHeadProperty.hasHead()) { + this.headRowNumber = excelReadHeadProperty.getHeadRowNumber(); + } else { + this.headRowNumber = 1; + } } else { this.headRowNumber = parentAbstractReadHolder.getHeadRowNumber(); } } else { this.headRowNumber = readBasicParameter.getHeadRowNumber(); } - // Initialization property - this.excelReadHeadProperty = new ExcelReadHeadProperty(getClazz(), getHead(), convertAllFiled); if (parentAbstractReadHolder == null) { this.readListenerList = new ArrayList(); } else { this.readListenerList = new ArrayList(parentAbstractReadHolder.getReadListenerList()); } - if (HolderEnum.WORKBOOK.equals(holderType()) - && HeadKindEnum.CLASS.equals(excelReadHeadProperty.getHeadKind())) { + if (HolderEnum.WORKBOOK.equals(holderType())) { readListenerList.add(new ModelBuildEventListener()); } if (readBasicParameter.getCustomReadListenerList() != null diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java index 5895b20a..2141e288 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java @@ -36,7 +36,7 @@ public class ReadSheetHolder extends AbstractReadHolder { this.readSheet = readSheet; this.parentReadWorkbookHolder = readWorkbookHolder; this.sheetNo = readSheet.getSheetNo(); - this.sheetName=readSheet.getSheetName(); + this.sheetName = readSheet.getSheetName(); } public ReadSheet getReadSheet() { @@ -83,4 +83,9 @@ public class ReadSheetHolder extends AbstractReadHolder { public HolderEnum holderType() { return HolderEnum.SHEET; } + + @Override + public String toString() { + return "ReadSheetHolder{" + "sheetNo=" + sheetNo + ", sheetName='" + sheetName + '\'' + "} " + super.toString(); + } } diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java index e4181f95..faa93d4f 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java @@ -15,6 +15,7 @@ import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.context.WriteContextImpl; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.ConverterKeyBuild; +import com.alibaba.excel.enums.WriteLastRowType; import com.alibaba.excel.exception.ExcelDataConvertException; import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.metadata.CellData; @@ -30,6 +31,7 @@ import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteTable; import com.alibaba.excel.write.metadata.WriteWorkbook; import com.alibaba.excel.write.metadata.holder.WriteHolder; +import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import net.sf.cglib.beans.BeanMap; @@ -58,13 +60,21 @@ public class ExcelBuilderImpl implements ExcelBuilder { if (CollectionUtils.isEmpty(data)) { return; } - Sheet currentSheet = context.writeSheetHolder().getSheet(); - int rowNum = currentSheet.getLastRowNum(); + WriteSheetHolder writeSheetHolder = context.writeSheetHolder(); + Sheet currentSheet = writeSheetHolder.getSheet(); + int lastRowNum = currentSheet.getLastRowNum(); + // 'lastRowNum' doesn't matter if it has one or zero,is's zero + if (lastRowNum == 0 && WriteLastRowType.EMPTY == writeSheetHolder.getWriteLastRowType()) { + lastRowNum--; + } + if (!data.isEmpty()) { + context.writeSheetHolder().setWriteLastRowType(WriteLastRowType.HAVE_DATA); + } if (context.currentWriteHolder().isNew()) { - rowNum += context.currentWriteHolder().relativeHeadRowIndex(); + lastRowNum += context.currentWriteHolder().relativeHeadRowIndex(); } for (int relativeRowIndex = 0; relativeRowIndex < data.size(); relativeRowIndex++) { - int n = relativeRowIndex + rowNum + 1; + int n = relativeRowIndex + lastRowNum + 1; addOneRowOfDataToExcel(data.get(relativeRowIndex), n, relativeRowIndex); } } diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java index 792f5e89..82fdbe0d 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java @@ -6,6 +6,7 @@ import java.util.Map; import org.apache.poi.ss.usermodel.Sheet; import com.alibaba.excel.enums.HolderEnum; +import com.alibaba.excel.enums.WriteLastRowType; import com.alibaba.excel.write.metadata.WriteSheet; /** @@ -39,6 +40,14 @@ public class WriteSheetHolder extends AbstractWriteHolder { */ private Map hasBeenInitializedTable; + /** + * last column type + * + * @param writeSheet + * @param writeWorkbookHolder + */ + private WriteLastRowType writeLastRowType; + public WriteSheetHolder(WriteSheet writeSheet, WriteWorkbookHolder writeWorkbookHolder) { super(writeSheet, writeWorkbookHolder, writeWorkbookHolder.getWriteWorkbook().getConvertAllFiled()); this.writeSheet = writeSheet; @@ -50,6 +59,11 @@ public class WriteSheetHolder extends AbstractWriteHolder { } this.parentWriteWorkbookHolder = writeWorkbookHolder; this.hasBeenInitializedTable = new HashMap(); + if (writeWorkbookHolder.getTemplateInputStream() == null && writeWorkbookHolder.getTemplateFile() == null) { + writeLastRowType = WriteLastRowType.EMPTY; + } else { + writeLastRowType = WriteLastRowType.HAVE_DATA; + } } public WriteSheet getWriteSheet() { @@ -100,6 +114,14 @@ public class WriteSheetHolder extends AbstractWriteHolder { this.hasBeenInitializedTable = hasBeenInitializedTable; } + public WriteLastRowType getWriteLastRowType() { + return writeLastRowType; + } + + public void setWriteLastRowType(WriteLastRowType writeLastRowType) { + this.writeLastRowType = writeLastRowType; + } + @Override public HolderEnum holderType() { return HolderEnum.SHEET; diff --git a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java index 900013c8..736611b0 100644 --- a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java +++ b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java @@ -84,6 +84,7 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty { if (alreadyRangeSet.contains(i + "-" + j)) { continue; } + alreadyRangeSet.add(i + "-" + j); String headName = headNameList.get(j); int endX = i; int endY = j; @@ -95,16 +96,12 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty { break; } } - // The current cells are not merged - if (endX == i) { - continue; - } Set tempAlreadyRangeSet = new HashSet(); outer: for (int k = j + 1; k < headNameList.size(); k++) { - for (int l = i; l < endX; l++) { + for (int l = i; l <= endX; l++) { if (headList.get(l).getHeadNameList().get(k).equals(headName)) { - tempAlreadyRangeSet.add(k + "-" + j); + tempAlreadyRangeSet.add(l + "-" + k); } else { break outer; } @@ -112,7 +109,7 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty { endY = k; alreadyRangeSet.addAll(tempAlreadyRangeSet); } - cellRangeList.add(new CellRange(i, endY, j, endX)); + cellRangeList.add(new CellRange(j, endY, i, endX)); } } return cellRangeList; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java index 3061dcc3..a01693d1 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java @@ -12,7 +12,6 @@ import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.util.DateUtils; /** - * Annotation data test * * @author zhuangjiaju */ diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java index d97901bc..7602e177 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java @@ -16,7 +16,6 @@ import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.util.DateUtils; /** - * Annotation data test * * @author zhuangjiaju */ diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexDataListener.java new file mode 100644 index 00000000..e7d67f7e --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexDataListener.java @@ -0,0 +1,33 @@ +package com.alibaba.easyexcel.test.core.head; + +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.fastjson.JSON; + +/** + * @author zhuangjiaju + */ +public class ComplexDataListener extends AnalysisEventListener { + private static final Logger LOGGER = LoggerFactory.getLogger(ComplexHeadData.class); + List list = new ArrayList(); + + @Override + public void invoke(ComplexHeadData data, AnalysisContext context) { + list.add(data); + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + Assert.assertEquals(list.size(), 1); + ComplexHeadData data = list.get(0); + Assert.assertEquals(data.getString4(), "字符串4"); + LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadData.java b/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadData.java new file mode 100644 index 00000000..8c292cfc --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadData.java @@ -0,0 +1,22 @@ +package com.alibaba.easyexcel.test.core.head; + +import com.alibaba.excel.annotation.ExcelProperty; + +import lombok.Data; + +/** + * @author zhuangjiaju + */ +@Data +public class ComplexHeadData { + @ExcelProperty({"顶格", "顶格", "两格"}) + private String string0; + @ExcelProperty({"顶格", "顶格", "两格"}) + private String string1; + @ExcelProperty({"顶格", "四联", "四联"}) + private String string2; + @ExcelProperty({"顶格", "四联", "四联"}) + private String string3; + @ExcelProperty({"顶格"}) + private String string4; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadDataTest.java new file mode 100644 index 00000000..9460d129 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadDataTest.java @@ -0,0 +1,54 @@ +package com.alibaba.easyexcel.test.core.head; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.alibaba.easyexcel.test.util.TestFileUtil; +import com.alibaba.excel.EasyExcelFactory; + +/** + * + * @author zhuangjiaju + */ +public class ComplexHeadDataTest { + + private static File file07; + private static File file03; + + @BeforeClass + public static void init() { + file07 = TestFileUtil.createNewFile("complexHead07.xlsx"); + file03 = TestFileUtil.createNewFile("complexHead03.xls"); + } + + @Test + public void T01ReadAndWrite07() { + readAndWrite(file07); + } + + @Test + public void T02ReadAndWrite03() { + readAndWrite(file03); + } + + private void readAndWrite(File file) { + EasyExcelFactory.write(file, ComplexHeadData.class).sheet().doWrite(data()).finish(); + EasyExcelFactory.read(file, ComplexHeadData.class, new ComplexDataListener()).sheet().doRead().finish(); + } + + private List data() { + List list = new ArrayList(); + ComplexHeadData data = new ComplexHeadData(); + data.setString0("字符串0"); + data.setString1("字符串1"); + data.setString2("字符串2"); + data.setString3("字符串3"); + data.setString4("字符串4"); + list.add(data); + return list; + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataListener.java new file mode 100644 index 00000000..9852af2b --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataListener.java @@ -0,0 +1,38 @@ +package com.alibaba.easyexcel.test.core.head; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +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.fastjson.JSON; + +/** + * @author zhuangjiaju + */ +public class ListHeadDataListener extends AnalysisEventListener> { + + private static final Logger LOGGER = LoggerFactory.getLogger(NoHeadData.class); + List> list = new ArrayList>(); + + @Override + public void invoke(Map data, AnalysisContext context) { + list.add(data); + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + Assert.assertEquals(list.size(), 1); + Map data = list.get(0); + Assert.assertEquals(data.get(0), "字符串0"); + Assert.assertEquals(data.get(1), "1.0"); + Assert.assertEquals(data.get(2), "2020-01-01 01:01:01"); + Assert.assertEquals(data.get(3), "额外数据"); + LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataTest.java new file mode 100644 index 00000000..df58cff7 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataTest.java @@ -0,0 +1,69 @@ +package com.alibaba.easyexcel.test.core.head; + +import java.io.File; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.alibaba.easyexcel.test.util.TestFileUtil; +import com.alibaba.excel.EasyExcelFactory; +import com.alibaba.excel.util.DateUtils; + +/** + * + * @author zhuangjiaju + */ +public class ListHeadDataTest { + + private static File file07; + private static File file03; + + @BeforeClass + public static void init() { + file07 = TestFileUtil.createNewFile("listHead07.xlsx"); + file03 = TestFileUtil.createNewFile("listHead03.xls"); + } + + @Test + public void T01ReadAndWrite07() throws Exception { + readAndWrite(file07); + } + + @Test + public void T02ReadAndWrite03() throws Exception { + readAndWrite(file03); + } + + private void readAndWrite(File file) throws Exception { + EasyExcelFactory.write(file).head(head()).sheet().doWrite(data()).finish(); + EasyExcelFactory.read(file).registerReadListener(new ListHeadDataListener()).sheet().doRead().finish(); + } + + private List> head() { + List> list = new ArrayList>(); + List head0 = new ArrayList(); + head0.add("字符串"); + List head1 = new ArrayList(); + head1.add("数字"); + List head2 = new ArrayList(); + head2.add("日期"); + list.add(head0); + list.add(head1); + list.add(head2); + return list; + } + + private List> data() throws ParseException { + List> list = new ArrayList>(); + List data0 = new ArrayList(); + data0.add("字符串0"); + data0.add(1); + data0.add(DateUtils.parseDate("2020-01-01 01:01:01")); + data0.add("额外数据"); + list.add(data0); + return list; + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData.java b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData.java new file mode 100644 index 00000000..b043417c --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData.java @@ -0,0 +1,14 @@ +package com.alibaba.easyexcel.test.core.head; + +import com.alibaba.excel.annotation.ExcelProperty; + +import lombok.Data; + +/** + * @author zhuangjiaju + */ +@Data +public class NoHeadData { + @ExcelProperty("字符串") + private String string; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData07Test.java b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData07Test.java deleted file mode 100644 index bab3927f..00000000 --- a/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData07Test.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.alibaba.easyexcel.test.core.head; - -import org.junit.Test; - -/** - * Order data test - * - * @author zhuangjiaju - */ -public class NoHeadData07Test { - - @Test - public void simple() { - // ExcelWriter writer = EasyExcelFactory.writerBuilder().outputFile(TestFileUtil.createNewWriteFile("order07.xlsx")) - // .head(OrderData.class).build(); - // Sheet sheet = EasyExcelFactory.writerSheetBuilder().sheetNo(0).sheetName("order").build(); - // writer.write(createData(10000 * 100), sheet); - // writer.finish(); - } -} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataListener.java new file mode 100644 index 00000000..767e6da1 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataListener.java @@ -0,0 +1,33 @@ +package com.alibaba.easyexcel.test.core.head; + +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.fastjson.JSON; + +/** + * @author zhuangjiaju + */ +public class NoHeadDataListener extends AnalysisEventListener { + private static final Logger LOGGER = LoggerFactory.getLogger(NoHeadData.class); + List list = new ArrayList(); + + @Override + public void invoke(NoHeadData data, AnalysisContext context) { + list.add(data); + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + Assert.assertEquals(list.size(), 1); + NoHeadData data = list.get(0); + Assert.assertEquals(data.getString(), "字符串0"); + LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataTest.java new file mode 100644 index 00000000..ad714d41 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataTest.java @@ -0,0 +1,51 @@ +package com.alibaba.easyexcel.test.core.head; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.alibaba.easyexcel.test.util.TestFileUtil; +import com.alibaba.excel.EasyExcelFactory; + +/** + * + * @author zhuangjiaju + */ +public class NoHeadDataTest { + + private static File file07; + private static File file03; + + @BeforeClass + public static void init() { + file07 = TestFileUtil.createNewFile("noHead07.xlsx"); + file03 = TestFileUtil.createNewFile("noHead03.xls"); + } + + @Test + public void T01ReadAndWrite07() { + readAndWrite(file07); + } + + @Test + public void T02ReadAndWrite03() { + readAndWrite(file03); + } + + private void readAndWrite(File file) { + EasyExcelFactory.write(file, NoHeadData.class).needHead(Boolean.FALSE).sheet().doWrite(data()).finish(); + EasyExcelFactory.read(file, NoHeadData.class, new NoHeadDataListener()).headRowNumber(0).sheet().doRead() + .finish(); + } + + private List data() { + List list = new ArrayList(); + NoHeadData data = new NoHeadData(); + data.setString("字符串0"); + list.add(data); + return list; + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java index 173eec75..c37e1d28 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java @@ -10,8 +10,7 @@ import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcelFactory; /** - * Large data test - * + * * @author zhuangjiaju */ public class LargeDataTest { diff --git a/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java index f66b55b0..6a3f8346 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java @@ -14,7 +14,6 @@ import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcelFactory; /** - * Simple data test * * @author zhuangjiaju */