1、去除ExcelKit 类中方法上的无意义异常声明

2、重构ExcelKit 部分逻辑
This commit is contained in:
2019-06-10 23:58:01 +08:00
parent 352c00e6cf
commit 7e50af843e
3 changed files with 107 additions and 68 deletions

View File

@@ -1,19 +1,48 @@
package com.lxyer.excel;
import net.tccn.kit.poi.ExcelKit;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.io.*;
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
List list = asList(
Map.of("name", "张三", "age", 12), //jdk9+语法创建Map并初始化数据
Map.of("name", "李四", "age", 11)
);
// 表头数据 k-vkmap的数据keyv表头展示的名称
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
@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";
@@ -24,22 +53,31 @@ public class ExcelKitTest {
System.out.println(sql1.substring(0,sql1.indexOf("from")).replace("select", "").replace(" ", ""));
*/
String[] fields ={"a", "b", "c"};
File file = new File("res/test.xlsx");
String[] fields ={"name", "age"};
File file = new File("target/abx.xls");
List<Map> list = ExcelKit.readExcel(file, fields);
System.out.println(list.size());
System.out.println("list.size: " + list.size());
}
//read excel no fields
//@Test
public void readTest2() throws IOException {
List<Map> list = ExcelKit.readExcel(new File("res/test.xlsx"));
list.forEach(x->{
x.forEach((k,v)->{
System.out.println(String.format("%s:%s", 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();
});
}

View File

@@ -1,12 +1,11 @@
package com.lxyer.excel;
import net.tccn.kit.poi.ExcelExportKit;
import net.tccn.kit.poi.ExcelKit;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.*;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.BiFunction;
@@ -14,6 +13,8 @@ import java.util.function.Function;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static java.util.Arrays.asList;
/**
* 以下均为非poi导出使用示例
* 1、导出List<javabean>示例
@@ -152,11 +153,6 @@ public class ExportKitTest {
/**
* 5、redkale web项目导出示例
* @param head
* @param rows
* @param response
* @param xlsName
* @param fun
* @return
* @throws IOException
*/