.
This commit is contained in:
162
src/test/java/test/ExcelKitTest.java
Normal file
162
src/test/java/test/ExcelKitTest.java
Normal file
@@ -0,0 +1,162 @@
|
||||
package test;
|
||||
|
||||
import net.tccn.kit.poi.ExcelKit;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
/**
|
||||
* @author: liangxianyou at 2018/9/15 7:52.
|
||||
*/
|
||||
public class ExcelKitTest {
|
||||
|
||||
//@Test
|
||||
public void mapExport() {
|
||||
// 数据List<T> T可以是map,也可以是某个Javabean
|
||||
Map m1 = new HashMap();
|
||||
m1.put("name", "张三");
|
||||
m1.put("age", 12);
|
||||
|
||||
Map m2 = new HashMap();
|
||||
m2.put("name", "李四");
|
||||
m2.put("age", 11);
|
||||
|
||||
List list = asList(m1, m2);
|
||||
// 表头数据 k-v,k:map的数据key,v:表头展示的名称
|
||||
LinkedHashMap heads = new LinkedHashMap();
|
||||
heads.put("name", "姓名");
|
||||
heads.put("age", "年龄");
|
||||
|
||||
|
||||
// 将List<T> 数据写入到新创建的一个Excel工作薄对象中
|
||||
//Workbook workbook = ExcelKit.getWorkbook(new File("target/abx2.xls"));
|
||||
|
||||
Workbook wb = ExcelKit.exportExcel(list, heads);
|
||||
|
||||
try {
|
||||
// 存贮数据到文件中
|
||||
wb.write(new FileOutputStream(new File("target/abx.xls")));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//read excel by fields
|
||||
//@Test
|
||||
public void readTest() throws IOException {
|
||||
/*String sql1 = "select a, b, c from Sheet1";
|
||||
String sql2 = "select a, b, c from Sheet1 where a=1 order by b";
|
||||
String sql3 = "select a, b, c from Sheet1 s1, Sheet2 s2 order by";
|
||||
String sql4 = "select a, b, c from Sheet1 s1, Sheet2 s2 where order by";
|
||||
String select = sql1.substring(0,sql1.indexOf("from")).replace("select", "").replace(" ", "");
|
||||
|
||||
System.out.println(sql1.substring(0,sql1.indexOf("from")).replace("select", "").replace(" ", ""));
|
||||
*/
|
||||
|
||||
String[] fields = {"name", "age"};
|
||||
File file = new File("target/abx.xls");
|
||||
List<Map> list = ExcelKit.readExcel(file, fields);
|
||||
System.out.println("list.size: " + list.size());
|
||||
|
||||
|
||||
list.forEach(x -> {
|
||||
x.forEach((k, v) -> {
|
||||
System.out.print(String.format("%s:%s \t", k, v));
|
||||
});
|
||||
System.out.println();
|
||||
});
|
||||
}
|
||||
|
||||
//read excel no fields
|
||||
//@Test
|
||||
public void readTest2() throws IOException {
|
||||
// 在不传 String[] fields 会把值从左到右分别赋值给 a...z,这样的key上
|
||||
List<Map> list = ExcelKit.readExcel(new File("target/abx.xls"));
|
||||
|
||||
list.forEach(x -> {
|
||||
x.forEach((k, v) -> {
|
||||
System.out.print(String.format("%s:%s \t", k, v));
|
||||
});
|
||||
System.out.println();
|
||||
});
|
||||
}
|
||||
|
||||
//--------------------------- 通用导入案例 --------------------------------
|
||||
|
||||
static Properties properties = new Properties();
|
||||
|
||||
static {
|
||||
try {
|
||||
// 读取导入配置文件
|
||||
properties.load(new FileReader(new File("test/resource/import.txt")));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建入库语句
|
||||
*
|
||||
* @param list 数据list<Map>
|
||||
* @param heads 数据库需要入库的字段
|
||||
* @param table 实体表
|
||||
* @return
|
||||
*/
|
||||
private String buildSql(List<Map> list, String[] heads, String table) {
|
||||
StringBuilder bufKs = new StringBuilder();
|
||||
StringBuilder bufVs = new StringBuilder();
|
||||
|
||||
for (String k : heads) {
|
||||
if (!k.isEmpty()) {
|
||||
bufKs.append("`" + k + "`,");
|
||||
}
|
||||
}
|
||||
bufKs.deleteCharAt(bufKs.length() - 1);
|
||||
|
||||
list.forEach(m -> {
|
||||
bufVs.append("(");
|
||||
for (String k : heads) {
|
||||
if (!k.isEmpty()) {
|
||||
bufVs.append("'" + m.get(k) + "',");
|
||||
}
|
||||
}
|
||||
bufVs.deleteCharAt(bufVs.length() - 1); //去除最后多余的 逗号
|
||||
bufVs.append("),");
|
||||
});
|
||||
bufVs.deleteCharAt(bufVs.length() - 1); //去除最后多余的 逗号
|
||||
return String.format("INSERT INTO %s (%s) VALUES %s;", table, bufKs, bufVs); // table, ks, vs
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用数据导入
|
||||
*/
|
||||
//@Test
|
||||
public void importTest() {
|
||||
String key = "user"; // 假定使用业务实体表名作为key
|
||||
|
||||
// 获取需要导入的列
|
||||
String cfg = properties.getProperty(key);
|
||||
String[] heads = cfg.split(",");
|
||||
for (int i = 0; i < heads.length; i++) {
|
||||
heads[i] = heads[i].trim();
|
||||
}
|
||||
|
||||
// 通过配置读取数据,userData.xlsx为待导入数据
|
||||
List<Map> list = ExcelKit.readExcel(new File("test/resource/userData.xlsx"), heads);
|
||||
list.remove(0); // 去除多余的行首两行
|
||||
list.remove(0);
|
||||
|
||||
System.out.println(list); // 打印出数据看看是否ok
|
||||
|
||||
// 创建入库语句
|
||||
String sql = buildSql(list, heads, key);
|
||||
System.out.println(sql);
|
||||
}
|
||||
|
||||
}
|
256
src/test/java/test/ExportKitTest.java
Normal file
256
src/test/java/test/ExportKitTest.java
Normal file
@@ -0,0 +1,256 @@
|
||||
package test;
|
||||
|
||||
import net.tccn.kit.poi.ExcelExportKit;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* 以下均为非poi导出使用示例
|
||||
* 1、导出List<javabean>示例
|
||||
* 2、导出List<Map> 示例
|
||||
* 3、javax.servlet web项目中开发使用示例
|
||||
* 4、导出压缩的excel示例
|
||||
* 5、redkale web项目导出示例
|
||||
* <p>
|
||||
* Created by liangxianyou at 2018/7/6 10:29.
|
||||
*/
|
||||
public class ExportKitTest {
|
||||
|
||||
//1、导出List<javabean>示例
|
||||
//@Test
|
||||
public void exportBean() {
|
||||
Map<String, String> heads = new LinkedHashMap();
|
||||
heads.put("姓名", "name");
|
||||
heads.put("年龄", "age");
|
||||
heads.put("生日", "bir");
|
||||
|
||||
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, new Date()));
|
||||
}
|
||||
File file = new File("target/user.xls");
|
||||
if (file.exists()) file.delete();
|
||||
|
||||
try {
|
||||
ExcelExportKit.exportExceltoFile(heads, rows, file, ExcelExportKit.TC.BEAN);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//2、导出List<Map> 示例
|
||||
//@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 {
|
||||
exportExcelZip(heads, rows, file, ExcelExportKit.TC.MAP);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//==================项目中使用示例,实际项目根据实际情况拷贝修改使用========================
|
||||
/**
|
||||
* 3、javax.servlet web项目中开发使用示例
|
||||
* @param head 表头
|
||||
* @param rows 数据
|
||||
* @param request
|
||||
* @param response
|
||||
* @param xlsName 导出的文件名称
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
/*
|
||||
public <T> boolean exportExcel(Map head, List<T> rows, HttpServletRequest request, HttpServletResponse response, String xlsName, BiFunction fun) throws IOException {
|
||||
if(request.getHeader("user-agent") != null && request.getHeader("user-agent").indexOf("MSIE") != -1) {
|
||||
xlsName = java.net.URLEncoder.encode(xlsName,"utf-8") + ".xls";
|
||||
} else {
|
||||
xlsName = new String(xlsName.getBytes("utf-8"),"iso-8859-1")+ ".xls";
|
||||
}
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(187);
|
||||
|
||||
|
||||
latch.countDown();
|
||||
|
||||
OutputStream os = response.getOutputStream();
|
||||
response.setContentType("application/vnd.ms-excel");
|
||||
response.setHeader("Content-disposition", "attachment;filename="+xlsName);
|
||||
|
||||
os.write(ExcelExportKit.exportExcel(head, rows, fun).toString().getBytes());
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* 4、导出压缩的excel示例
|
||||
*
|
||||
* @param head
|
||||
* @param rows
|
||||
* @param file
|
||||
* @param fun
|
||||
* @param <T>
|
||||
* @throws IOException
|
||||
*/
|
||||
public <T, U, R> void exportExcelZip(Map<String, String> head, List<T> rows, File file, BiFunction<T, U, R> fun) throws IOException {
|
||||
|
||||
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
|
||||
BufferedOutputStream bos = new BufferedOutputStream(zos);
|
||||
|
||||
ZipEntry zipEntry = new ZipEntry(file.getName().split("[.]")[0] + ".xls");
|
||||
zos.putNextEntry(zipEntry);
|
||||
|
||||
StringBuilder buf = ExcelExportKit.exportExcel(head, rows, fun);
|
||||
bos.write(buf.toString().getBytes());
|
||||
bos.flush();
|
||||
|
||||
bos.close();
|
||||
zos.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 5、redkale web项目导出示例
|
||||
*
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
/*
|
||||
public static <T,U,R> void exportExcel(Map head, List<T> rows, HttpResponse response, String xlsName, BiFunction<T,U,R> fun) throws IOException {
|
||||
|
||||
xlsName = new String(xlsName.getBytes("utf-8"),"iso-8859-1")+ ".xls";
|
||||
response.setContentType("application/vnd.ms-excel");
|
||||
response.setHeader("Content-disposition", "attachment;filename="+xlsName);
|
||||
|
||||
StringBuilder buf = ExcelExportKit.exportExcel(head, rows, fun);
|
||||
response.finish(buf.toString().getBytes());
|
||||
}
|
||||
*/
|
||||
//@Test
|
||||
public void t() {
|
||||
Object t = new Person();
|
||||
((Person) t).setAge(20);
|
||||
((Person) t).setName("'zhangsan'");
|
||||
Class<?> clazz = t.getClass();
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
try {
|
||||
if (field.getType() == String.class) {
|
||||
field.setAccessible(true);
|
||||
String v = (String) field.get(t);
|
||||
v = v.replace("'", "");
|
||||
field.set(t, v);
|
||||
System.out.println(field.getName() + "\t|" + field.getType() + "\t|" + v);
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("--------------------------------");
|
||||
|
||||
System.out.println(((Person) t).getName());
|
||||
|
||||
//Field field = fields[0];
|
||||
/*
|
||||
|
||||
field.getGenericType();
|
||||
|
||||
|
||||
field.setAccessible(true); //暴力反射
|
||||
Object o = field.get(t);//得到字段数据的值
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void t2() {
|
||||
Person person = new Person();
|
||||
/*person.setName("dsafd''s'f");
|
||||
person.setAge(123);*/
|
||||
|
||||
//HashMap<Object, Object> person = new HashMap<>();
|
||||
//person.put("name", "sdfdsdfad''sa");
|
||||
|
||||
|
||||
dealField(person, (s) -> {
|
||||
String v = s.replace("'", "");
|
||||
|
||||
return v;
|
||||
});
|
||||
|
||||
System.out.println(person.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象属性处理,
|
||||
*
|
||||
* @param o
|
||||
* @param function
|
||||
* @auther lxyer
|
||||
*/
|
||||
public void dealField(Object o, Function<String, String> function) {
|
||||
try {
|
||||
Class<?> clazz = o.getClass();
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
if (field.getType() == String.class) {
|
||||
field.setAccessible(true);
|
||||
String v = (String) field.get(o);
|
||||
//v = v.replace("'", "");
|
||||
if (v != null) {
|
||||
v = function.apply(v);
|
||||
field.set(o, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
45
src/test/java/test/Person.java
Normal file
45
src/test/java/test/Person.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package test;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2018/7/6 10:28.
|
||||
*/
|
||||
public class Person {
|
||||
private String name;
|
||||
private int age;
|
||||
private Date bir;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String name, int age, Date bir) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.bir = bir;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public Date getBir() {
|
||||
return bir;
|
||||
}
|
||||
|
||||
public void setBir(Date bir) {
|
||||
this.bir = bir;
|
||||
}
|
||||
}
|
2
src/test/resource/import.txt
Normal file
2
src/test/resource/import.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
# 假定我们就使用数据库对应的 “列名”作为导入配置,如果excel中间有需要跳过的列 配置中用空号占位
|
||||
user= ,username, , sex,phone,email
|
BIN
src/test/resource/userData.xlsx
Normal file
BIN
src/test/resource/userData.xlsx
Normal file
Binary file not shown.
Reference in New Issue
Block a user