mirror of https://github.com/alibaba/easyexcel
Jiaju Zhuang
4 years ago
11 changed files with 205 additions and 28 deletions
@ -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; |
||||
} |
||||
} |
@ -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(); |
||||
} |
||||
|
||||
} |
@ -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(); |
||||
|
||||
} |
@ -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; |
||||
} |
||||
} |
Loading…
Reference in new issue