diff --git a/src/net/tccn/kit/poi/ExcelKit.java b/src/net/tccn/kit/poi/ExcelKit.java index 91955dd..20f5d74 100644 --- a/src/net/tccn/kit/poi/ExcelKit.java +++ b/src/net/tccn/kit/poi/ExcelKit.java @@ -35,6 +35,15 @@ import java.util.*; */ public class ExcelKit { + // 在已有的excel中新增sheet并写入数据 + public static Workbook exportExcel(List list, LinkedHashMap headMap, Workbook workbook, String sheetName) { + return dataToWorkBook(list, headMap, workbook, sheetName); + } + + public static Workbook exportExcel(List list, LinkedHashMap headMap, Workbook workbook) { + return dataToWorkBook(list, headMap, workbook, null); + } + /** * Excels导出多个sheet * @@ -42,7 +51,7 @@ public class ExcelKit { * @date 2015-6-16 下午5:56:56 */ //map:data,sheetName,hds,hdNames, - public static Workbook exportExcels(List> list) throws Exception { + public static Workbook exportExcels(List> list) { Workbook wb = new SXSSFWorkbook(); //创建工作薄 for (int i = 0; i < list.size(); i++) { @@ -69,16 +78,27 @@ public class ExcelKit { } /** - * 参数说明: - * list:list数据集合 - * headMap:每一列的字段名和对应的表头名称 如:{name:"姓名", age:"年龄"} * 通过数据构建 Workbook 对象 - * + * @param list 数据 + * @param headMap 每一列的字段名和对应的表头名称 如:{name:"姓名", age:"年龄"} + * @param 数据泛型,支持javaBean 或Map + * @return * @throws Exception * @author LiangXianYou * @date 2015-3-13 上午11:00:30 */ - public static Workbook exportExcel(List list, LinkedHashMap headMap) throws Exception { + public static Workbook exportExcel(List list, LinkedHashMap headMap) { + return dataToWorkBook(list, headMap, null, null); + } + + private static Workbook dataToWorkBook(List list, LinkedHashMap headMap, Workbook wb, String sheetName) { + if (wb == null) { + wb = new SXSSFWorkbook(); + } + + Sheet sheet = sheetName != null && !sheetName.isEmpty() ? + wb.createSheet(sheetName) : wb.createSheet(); + String[] hdNames = new String[headMap.size()]; // String[] hds = new String[headMap.size()]; @@ -89,44 +109,25 @@ public class ExcelKit { tag[0] ++; }); - return exportExcel(list, hdNames, hds); - } - - /** - * 使用数据构建 excel 工作薄对象 - * @param list 数据 - * @param hdNames 表头 - * @param hds 每条记录中数据的属性名 - * @param 数据泛型,支持javaBean 或Map - * @return - * @throws Exception - * @author LiangXianYou - * @date 2015-3-13 上午11:00:30 - */ - public static Workbook exportExcel(List list, String[] hdNames, String[] hds) throws Exception { - - Workbook wb = new SXSSFWorkbook(); - Sheet sheet = wb.createSheet(); - //写入表头---Excel的第一行数据 Row nRow = sheet.createRow(0); for (int i = 0; i < hdNames.length; i++) { Cell nCell = nRow.createCell(i); nCell.setCellValue(hdNames[i]); } + // 写入每一条记录 dataToSheet(sheet, list, hds, 1); return wb; } - /** * 通过泛型实例对象得到某一字段值 * * @author LiangXianYou * @date 2015-3-13 上午10:53:32 */ - private static Object getFieldValue(T t, String fieldName) throws Exception { - Object v; + private static Object getFieldValue(T t, String fieldName) throws NoSuchFieldException, IllegalAccessException { + Object v = null; if (t == null) { v = null; @@ -184,49 +185,49 @@ public class ExcelKit { //======================= 读取excel =============================== //read excel head - public static Map readExcelHead(File file, String[] fields) throws IOException { + public static Map readExcelHead(File file, String[] fields) { List list = readExcel(file, fields, 1, null); return list.size() > 0 ? list.get(0) : new HashMap(); } //read excel head by sheetName - public static Map readExcelHead(File file, String[] fields, String sheetName) throws IOException { + public static Map readExcelHead(File file, String[] fields, String sheetName) { List list = readExcel(file, fields, 1, sheetName); return list.size() > 0 ? list.get(0) : new HashMap(); } //read excel sheet[0] - public static List readExcel(File file, String[] fields) throws IOException { + public static List readExcel(File file, String[] fields) { return readExcel(file, fields, -1, null); } //read excel sheet[0] no fields - public static List readExcel(File file) throws IOException { + public static List readExcel(File file) { return readExcel(file, null, -1, null); } //read excel by sheetName - public static List readExcel(File file, String[] fields, String sheetName) throws IOException { + public static List readExcel(File file, String[] fields, String sheetName) { return readExcel(file, fields, -1, sheetName); } //read excel by sheetName no fields - public static List readExcel(File file, String sheetName) throws IOException { + public static List readExcel(File file, String sheetName) { return readExcel(file, null, -1, sheetName); } //read excel all sheet - public static Map> readExcelAll(File file, String[] fields) throws IOException { + public static Map> readExcelAll(File file, String[] fields) { return readExcelAll(file, fields, -1); } //read all excel no fields - public static Map> readExcelAll(File file) throws IOException { + public static Map> readExcelAll(File file) { return readExcelAll(file, null, -1); } //read excel sheet[0] - private static List readExcel(File file, String[] fields, int lastRowNum, String sheetName) throws IOException { + private static List readExcel(File file, String[] fields, int lastRowNum, String sheetName) { Workbook wk = getWorkbook(file); Sheet sheet = sheetName == null ? wk.getSheetAt(0) : wk.getSheet(sheetName); if (sheet == null) throw new OfficeXmlFileException("sheet[" + sheetName + "] can't findList"); @@ -247,7 +248,7 @@ public class ExcelKit { * @return * @throws IOException */ - private static Map> readExcelAll(File file, String[] fields, int lastRowNum) throws IOException { + private static Map> readExcelAll(File file, String[] fields, int lastRowNum) { Workbook wk = getWorkbook(file); Map> data = new LinkedHashMap<>(); @@ -270,7 +271,7 @@ public class ExcelKit { * * @author Lxyer 2016/8/1 10:32. */ - private static List readExcel(Sheet sheet, String[] fields, int lastRowNum) throws OfficeXmlFileException { + private static List readExcel(Sheet sheet, String[] fields, int lastRowNum) { if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()) { lastRowNum = sheet.getLastRowNum(); } @@ -281,8 +282,7 @@ public class ExcelKit { List list = new ArrayList<>(); int t = 0; - r: - for (int i = 0; i <= lastRowNum; i++) { + r:for (int i = 0; i <= lastRowNum; i++) { Row row = sheet.getRow(i); if (row == null) continue; short cellNum = row.getLastCellNum(); @@ -369,7 +369,7 @@ public class ExcelKit { } //get all sheet names - public static List getSheetNames(File file) throws IOException { + public static List getSheetNames(File file) { Workbook wk = getWorkbook(file); List sheetNames = new ArrayList<>(); for (int i = 0; i < wk.getNumberOfSheets(); i++) { @@ -378,8 +378,13 @@ public class ExcelKit { return sheetNames; } - private static Workbook getWorkbook(File file) throws IOException { - return WorkbookFactory.create(file); + public static Workbook getWorkbook(File file) { + try { + return WorkbookFactory.create(file); + } catch (IOException e) { + e.printStackTrace(); + } + return null; /*Workbook wk; FileInputStream fis = new FileInputStream(file); try { diff --git a/test/com/lxyer/excel/ExcelKitTest.java b/test/com/lxyer/excel/ExcelKitTest.java index 365ab24..03e107e 100644 --- a/test/com/lxyer/excel/ExcelKitTest.java +++ b/test/com/lxyer/excel/ExcelKitTest.java @@ -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可以是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 + @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 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 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 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(); }); } diff --git a/test/com/lxyer/excel/ExportKitTest.java b/test/com/lxyer/excel/ExportKitTest.java index b680315..4855510 100644 --- a/test/com/lxyer/excel/ExportKitTest.java +++ b/test/com/lxyer/excel/ExportKitTest.java @@ -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示例 @@ -152,11 +153,6 @@ public class ExportKitTest { /** * 5、redkale web项目导出示例 - * @param head - * @param rows - * @param response - * @param xlsName - * @param fun * @return * @throws IOException */