package com.lxyer.excel; import net.tccn.kit.poi.ExcelKit; import org.apache.poi.ss.usermodel.Workbook; import org.junit.Test; 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可以是map,也可以是某个Javabean List list = asList( Map.of("name", "张三", "age", 12), //jdk9+语法,创建Map并初始化数据 Map.of("name", "李四", "age", 11) ); // 表头数据 k-v,k:map的数据key,v:表头展示的名称 LinkedHashMap heads = new LinkedHashMap(); heads.put("name", "姓名"); heads.put("age", "年龄"); // 将List 数据写入到新创建的一个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 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 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(); }); } }