Browse Source

写入支持Collection [Issue #1834]

developing
Jiaju Zhuang 4 years ago
parent
commit
e1c9081c41
  1. 18
      src/main/java/com/alibaba/excel/ExcelWriter.java
  2. 7
      src/main/java/com/alibaba/excel/write/ExcelBuilder.java
  3. 5
      src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
  4. 4
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java
  5. 4
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java
  6. 28
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java
  7. 32
      src/main/java/com/alibaba/excel/write/metadata/CollectionRowData.java
  8. 33
      src/main/java/com/alibaba/excel/write/metadata/MapRowData.java
  9. 31
      src/main/java/com/alibaba/excel/write/metadata/RowData.java
  10. 70
      src/test/java/com/alibaba/easyexcel/test/core/nomodel/NoModelDataTest.java
  11. 1
      update.md

18
src/main/java/com/alibaba/excel/ExcelWriter.java

@ -3,6 +3,8 @@ package com.alibaba.excel;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
@ -142,7 +144,7 @@ public class ExcelWriter {
* Write to this sheet
* @return this current writer
*/
public ExcelWriter write(List data, WriteSheet writeSheet) {
public ExcelWriter write(Collection<?> data, WriteSheet writeSheet) {
return write(data, writeSheet, null);
}
@ -157,7 +159,7 @@ public class ExcelWriter {
* Write to this table
* @return this
*/
public ExcelWriter write(List data, WriteSheet writeSheet, WriteTable writeTable) {
public ExcelWriter write(Collection<?> data, WriteSheet writeSheet, WriteTable writeTable) {
excelBuilder.addContent(data, writeSheet, writeTable);
return this;
}
@ -194,7 +196,7 @@ public class ExcelWriter {
* @param sheet
* Write to this sheet
* @return this current writer
* @deprecated please use {@link ExcelWriter#write(List, WriteSheet)}
* @deprecated please use {@link ExcelWriter#write(Collection, WriteSheet)}
*/
@Deprecated
public ExcelWriter write(List data, Sheet sheet) {
@ -211,7 +213,7 @@ public class ExcelWriter {
* @param table
* Write to this table
* @return this
* @deprecated * @deprecated please use {@link ExcelWriter#write(List, WriteSheet,WriteTable)}
* @deprecated * @deprecated please use {@link ExcelWriter#write(Collection, WriteSheet,WriteTable)}
*/
@Deprecated
public ExcelWriter write(List data, Sheet sheet, Table table) {
@ -246,7 +248,7 @@ public class ExcelWriter {
* @param sheet
* Write to this sheet
* @return this current writer
* @deprecated please use {@link ExcelWriter#write(List, WriteSheet)}
* @deprecated please use {@link ExcelWriter#write(Collection, WriteSheet)}
*/
@Deprecated
public ExcelWriter write0(List data, Sheet sheet) {
@ -263,7 +265,7 @@ public class ExcelWriter {
* @param table
* Write to this table
* @return this
* @deprecated * @deprecated please use {@link ExcelWriter#write(List, WriteSheet,WriteTable)}
* @deprecated * @deprecated please use {@link ExcelWriter#write(Collection, WriteSheet,WriteTable)}
*/
@Deprecated
public ExcelWriter write0(List data, Sheet sheet, Table table) {
@ -278,7 +280,7 @@ public class ExcelWriter {
* @param sheet
* Write to this sheet
* @return this current writer
* @deprecated please use {@link ExcelWriter#write(List, WriteSheet)}
* @deprecated please use {@link ExcelWriter#write(Collection, WriteSheet)}
*/
@Deprecated
public ExcelWriter write1(List data, Sheet sheet) {
@ -295,7 +297,7 @@ public class ExcelWriter {
* @param table
* Write to this table
* @return this
* @deprecated * @deprecated please use {@link ExcelWriter#write(List, WriteSheet,WriteTable)}
* @deprecated * @deprecated please use {@link ExcelWriter#write(Collection, WriteSheet,WriteTable)}
*/
@Deprecated
public ExcelWriter write1(List data, Sheet sheet, Table table) {

7
src/main/java/com/alibaba/excel/write/ExcelBuilder.java

@ -1,5 +1,6 @@
package com.alibaba.excel.write;
import java.util.Collection;
import java.util.List;
import com.alibaba.excel.context.WriteContext;
@ -20,10 +21,10 @@ public interface ExcelBuilder {
* java basic type or java model extend BaseModel
* @param writeSheet
* Write the sheet
* @deprecated please use{@link ExcelBuilder#addContent(List, WriteSheet, WriteTable)}
* @deprecated please use{@link ExcelBuilder#addContent(Collection, WriteSheet, WriteTable)}
*/
@Deprecated
void addContent(List data, WriteSheet writeSheet);
void addContent(Collection<?> data, WriteSheet writeSheet);
/**
* WorkBook increase value
@ -35,7 +36,7 @@ public interface ExcelBuilder {
* @param writeTable
* Write the table
*/
void addContent(List data, WriteSheet writeSheet, WriteTable writeTable);
void addContent(Collection<?> data, WriteSheet writeSheet, WriteTable writeTable);
/**
* WorkBook fill value

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

@ -1,6 +1,7 @@
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;
@ -48,12 +49,12 @@ public class ExcelBuilderImpl implements ExcelBuilder {
}
@Override
public void addContent(List data, WriteSheet writeSheet) {
public void addContent(Collection<?> data, WriteSheet writeSheet) {
addContent(data, writeSheet, null);
}
@Override
public void addContent(List data, WriteSheet writeSheet, WriteTable writeTable) {
public void addContent(Collection<?> data, WriteSheet writeSheet, WriteTable writeTable) {
try {
context.currentSheet(writeSheet, WriteTypeEnum.ADD);
context.currentTable(writeTable);

4
src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java

@ -1,6 +1,6 @@
package com.alibaba.excel.write.builder;
import java.util.List;
import java.util.Collection;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.exception.ExcelGenerateException;
@ -54,7 +54,7 @@ public class ExcelWriterSheetBuilder extends AbstractExcelWriterParameterBuilder
return writeSheet;
}
public void doWrite(List data) {
public void doWrite(Collection<?> data) {
if (excelWriter == null) {
throw new ExcelGenerateException("Must use 'EasyExcelFactory.write().sheet()' to call this method");
}

4
src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java

@ -1,6 +1,6 @@
package com.alibaba.excel.write.builder;
import java.util.List;
import java.util.Collection;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.exception.ExcelGenerateException;
@ -47,7 +47,7 @@ public class ExcelWriterTableBuilder extends AbstractExcelWriterParameterBuilder
return writeTable;
}
public void doWrite(List data) {
public void doWrite(Collection<?> data) {
if (excelWriter == null) {
throw new ExcelGenerateException("Must use 'EasyExcelFactory.write().sheet().table()' to call this method");
}

28
src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java

@ -2,8 +2,8 @@ package com.alibaba.excel.write.executor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
@ -17,6 +17,9 @@ import com.alibaba.excel.util.ClassUtils;
import com.alibaba.excel.util.FieldUtils;
import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.util.WriteHandlerUtils;
import com.alibaba.excel.write.metadata.CollectionRowData;
import com.alibaba.excel.write.metadata.MapRowData;
import com.alibaba.excel.write.metadata.RowData;
import com.alibaba.excel.write.metadata.holder.WriteHolder;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
@ -37,9 +40,9 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
super(writeContext);
}
public void add(List data) {
public void add(Collection<?> data) {
if (CollectionUtils.isEmpty(data)) {
data = new ArrayList();
data = new ArrayList<>();
}
WriteSheetHolder writeSheetHolder = writeContext.writeSheetHolder();
int newRowIndex = writeSheetHolder.getNewRowIndexAndStartDoWrite();
@ -47,11 +50,10 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
newRowIndex += writeContext.currentWriteHolder().relativeHeadRowIndex();
}
// BeanMap is out of order,so use sortedAllFiledMap
Map<Integer, Field> sortedAllFiledMap = new TreeMap<Integer, Field>();
Map<Integer, Field> sortedAllFiledMap = new TreeMap<>();
int relativeRowIndex = 0;
int lastRowIndex = 0;
for (Object oneRowData : data) {
lastRowIndex = relativeRowIndex + newRowIndex;
int lastRowIndex = relativeRowIndex + newRowIndex;
addOneRowOfDataToExcel(oneRowData, lastRowIndex, relativeRowIndex, sortedAllFiledMap);
relativeRowIndex++;
}
@ -65,16 +67,19 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
WriteHandlerUtils.beforeRowCreate(writeContext, n, relativeRowIndex, Boolean.FALSE);
Row row = WorkBookUtil.createRow(writeContext.writeSheetHolder().getSheet(), n);
WriteHandlerUtils.afterRowCreate(writeContext, row, relativeRowIndex, Boolean.FALSE);
if (oneRowData instanceof List) {
addBasicTypeToExcel((List)oneRowData, row, relativeRowIndex);
if (oneRowData instanceof Collection<?>) {
addBasicTypeToExcel(new CollectionRowData((Collection<?>)oneRowData), row, relativeRowIndex);
} else if (oneRowData instanceof Map) {
addBasicTypeToExcel(new MapRowData((Map<Integer, ?>)oneRowData), row, relativeRowIndex);
} else {
addJavaObjectToExcel(oneRowData, row, relativeRowIndex, sortedAllFiledMap);
}
WriteHandlerUtils.afterRowDispose(writeContext, row, relativeRowIndex, Boolean.FALSE);
}
private void addBasicTypeToExcel(List<Object> oneRowData, Row row, int relativeRowIndex) {
if (CollectionUtils.isEmpty(oneRowData)) {
private void addBasicTypeToExcel(RowData oneRowData, Row row, int relativeRowIndex) {
if (oneRowData.isEmpty()) {
return;
}
Map<Integer, Head> headMap = writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadMap();
@ -101,7 +106,8 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
}
}
private void doAddBasicTypeToExcel(List<Object> oneRowData, Head head, Row row, int relativeRowIndex, int dataIndex,
private void doAddBasicTypeToExcel(RowData oneRowData, Head head, Row row, int relativeRowIndex,
int dataIndex,
int cellIndex) {
WriteHandlerUtils.beforeCellCreate(writeContext, row, head, cellIndex, relativeRowIndex, Boolean.FALSE);
Cell cell = WorkBookUtil.createCell(row, cellIndex);

32
src/main/java/com/alibaba/excel/write/metadata/CollectionRowData.java

@ -0,0 +1,32 @@
package com.alibaba.excel.write.metadata;
import java.util.Collection;
/**
* A collection row of data.
*
* @author Jiaju Zhuang
*/
public class CollectionRowData implements RowData {
private final Object[] array;
public CollectionRowData(Collection<?> collection) {
this.array = collection.toArray();
}
@Override
public Object get(int index) {
return array[index];
}
@Override
public int size() {
return array.length;
}
@Override
public boolean isEmpty() {
return array.length == 0;
}
}

33
src/main/java/com/alibaba/excel/write/metadata/MapRowData.java

@ -0,0 +1,33 @@
package com.alibaba.excel.write.metadata;
import java.util.Map;
/**
* A map row of data.
*
* @author Jiaju Zhuang
*/
public class MapRowData implements RowData {
private final Map<Integer, ?> map;
public MapRowData(Map<Integer, ?> map) {
this.map = map;
}
@Override
public Object get(int index) {
return map.get(index);
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
}

31
src/main/java/com/alibaba/excel/write/metadata/RowData.java

@ -0,0 +1,31 @@
package com.alibaba.excel.write.metadata;
/**
* A row of data.
*
* @author Jiaju Zhuang
*/
public interface RowData {
/**
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
*/
Object get(int index);
/**
* Returns the number of elements in this collection. If this collection
* contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
* <tt>Integer.MAX_VALUE</tt>.
*
* @return the number of elements in this collection
*/
int size();
/**
* Returns <tt>true</tt> if this collection contains no elements.
*
* @return <tt>true</tt> if this collection contains no elements
*/
boolean isEmpty();
}

70
src/test/java/com/alibaba/easyexcel/test/core/nomodel/NoModelDataTest.java

@ -0,0 +1,70 @@
package com.alibaba.easyexcel.test.core.nomodel;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
/**
* @author Jiaju Zhuang
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class NoModelDataTest {
private static File file07;
private static File file03;
private static File fileRepeat07;
private static File fileRepeat03;
@BeforeClass
public static void init() {
file07 = TestFileUtil.createNewFile("noModel07.xlsx");
file03 = TestFileUtil.createNewFile("noModel03.xls");
fileRepeat07 = TestFileUtil.createNewFile("noModelRepeat07.xlsx");
fileRepeat03 = TestFileUtil.createNewFile("noModelRepeat03.xls");
}
@Test
public void t01ReadAndWrite07() {
readAndWrite(file07, fileRepeat07);
}
@Test
public void t02ReadAndWrite03() {
readAndWrite(file03, fileRepeat03);
}
private void readAndWrite(File file, File fileRepeat) {
EasyExcel.write(file).sheet().doWrite(data());
List<Map<Integer, String>> result = EasyExcel.read(file).headRowNumber(0).sheet().doReadSync();
Assert.assertEquals(10, result.size());
Map<Integer, String> data10 = result.get(9);
Assert.assertEquals("string19", data10.get(0));
EasyExcel.write(fileRepeat).sheet().doWrite(result);
result = EasyExcel.read(fileRepeat).headRowNumber(0).sheet().doReadSync();
Assert.assertEquals(10, result.size());
data10 = result.get(9);
Assert.assertEquals("string19", data10.get(0));
}
private List<List<String>> data() {
List<List<String>> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
List<String> data = new ArrayList<>();
data.add("string1" + i);
data.add("string2" + i);
data.add("string3" + i);
list.add(data);
}
return list;
}
}

1
update.md

@ -12,6 +12,7 @@
* 修改异常抛出逻辑 [Issue #1618](https://github.com/alibaba/easyexcel/issues/1618)
* 兼容一些非官方excel的情况 [Issue #1527](https://github.com/alibaba/easyexcel/issues/1527)
* 修改读的关闭流无效 [Issue #1840](https://github.com/alibaba/easyexcel/issues/1840)
* 写入支持Collection [Issue #1834](https://github.com/alibaba/easyexcel/issues/1834)
# 2.2.8

Loading…
Cancel
Save