加入非poi的excel导出,导出数据类型灵活(得益于jdk8+的 BiFuntion使用)
This commit is contained in:
76
test/excel/ExportTest.java
Normal file
76
test/excel/ExportTest.java
Normal file
@@ -0,0 +1,76 @@
|
||||
package excel;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2018/7/6 10:29.
|
||||
*/
|
||||
public class ExportTest {
|
||||
|
||||
@Test
|
||||
public void exportBean(){
|
||||
Map<String, String> heads = new LinkedHashMap();
|
||||
heads.put("姓名", "name");
|
||||
heads.put("年龄", "age");
|
||||
|
||||
int total = 5_0000;
|
||||
List rows = new ArrayList<>(total);
|
||||
Random random = new Random();
|
||||
|
||||
for (int i = 0; i < total; i++) {
|
||||
String name = UUID.randomUUID().toString();
|
||||
int age = random.nextInt(100);
|
||||
rows.add(new Person(name, age));
|
||||
}
|
||||
File file = new File("target/user.zip");
|
||||
if (file.exists()) file.delete();
|
||||
|
||||
try {
|
||||
ExcelExportKit.exportExcelZip(heads, rows, file, ExcelExportKit.TC.BEAN);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exportMapToZip(){
|
||||
|
||||
Map<String, String> heads = new LinkedHashMap();
|
||||
heads.put("姓名", "name");
|
||||
heads.put("姓名1", "name1");
|
||||
heads.put("姓名2", "name2");
|
||||
heads.put("年龄", "age");
|
||||
|
||||
int total = 5_0000;
|
||||
List<Map> rows = new ArrayList<>(total);
|
||||
|
||||
Map r1 = new HashMap();
|
||||
r1.put("name", "张三");
|
||||
r1.put("age", 12);
|
||||
rows.add(r1);
|
||||
|
||||
Random random = new Random();
|
||||
|
||||
//使用uuid 模拟复杂数据导出
|
||||
for (int i = 0; i < total; i++) {
|
||||
Map r2 = new HashMap();
|
||||
r2.put("name", UUID.randomUUID());
|
||||
r2.put("name1", UUID.randomUUID());
|
||||
r2.put("name2", UUID.randomUUID());
|
||||
r2.put("age", random.nextInt(100));
|
||||
rows.add(r2);
|
||||
}
|
||||
File file = new File("target/hello.zip");
|
||||
if (file.exists()) file.delete();
|
||||
|
||||
try {
|
||||
ExcelExportKit.exportExcelZip(heads, rows, file, ExcelExportKit.TC.MAP);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
33
test/excel/Person.java
Normal file
33
test/excel/Person.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package excel;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2018/7/6 10:28.
|
||||
*/
|
||||
public class Person {
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user