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

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

View File

@ -35,6 +35,15 @@ import java.util.*;
*/
public class ExcelKit {
// 在已有的excel中新增sheet并写入数据
public static <T> Workbook exportExcel(List<T> list, LinkedHashMap<String, String> headMap, Workbook workbook, String sheetName) {
return dataToWorkBook(list, headMap, workbook, sheetName);
}
public static <T> Workbook exportExcel(List<T> list, LinkedHashMap<String, String> 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 <T> Workbook exportExcels(List<Map<String, Object>> list) throws Exception {
public static <T> Workbook exportExcels(List<Map<String, Object>> list) {
Workbook wb = new SXSSFWorkbook(); //创建工作薄
for (int i = 0; i < list.size(); i++) {
@ -69,16 +78,27 @@ public class ExcelKit {
}
/**
* 参数说明
* listlist数据集合
* headMap每一列的字段名和对应的表头名称 {name:"姓名" age:"年龄"}
* 通过数据构建 Workbook 对象
*
* @param list 数据
* @param headMap 每一列的字段名和对应的表头名称 {name:"姓名" age:"年龄"}
* @param <T> 数据泛型支持javaBean 或Map
* @return
* @throws Exception
* @author LiangXianYou
* @date 2015-3-13 上午11:00:30
*/
public static Workbook exportExcel(List<T> list, LinkedHashMap<String, String> headMap) throws Exception {
public static <T> Workbook exportExcel(List<T> list, LinkedHashMap<String, String> headMap) {
return dataToWorkBook(list, headMap, null, null);
}
private static <T> Workbook dataToWorkBook(List<T> list, LinkedHashMap<String, String> 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 <T> 数据泛型支持javaBean 或Map
* @return
* @throws Exception
* @author LiangXianYou
* @date 2015-3-13 上午11:00:30
*/
public static <T> Workbook exportExcel(List<T> 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 <T> Object getFieldValue(T t, String fieldName) throws Exception {
Object v;
private static <T> 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<Map> 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<Map> list = readExcel(file, fields, 1, sheetName);
return list.size() > 0 ? list.get(0) : new HashMap();
}
//read excel sheet[0]
public static List<Map> readExcel(File file, String[] fields) throws IOException {
public static List<Map> readExcel(File file, String[] fields) {
return readExcel(file, fields, -1, null);
}
//read excel sheet[0] no fields
public static List<Map> readExcel(File file) throws IOException {
public static List<Map> readExcel(File file) {
return readExcel(file, null, -1, null);
}
//read excel by sheetName
public static List<Map> readExcel(File file, String[] fields, String sheetName) throws IOException {
public static List<Map> readExcel(File file, String[] fields, String sheetName) {
return readExcel(file, fields, -1, sheetName);
}
//read excel by sheetName no fields
public static List<Map> readExcel(File file, String sheetName) throws IOException {
public static List<Map> readExcel(File file, String sheetName) {
return readExcel(file, null, -1, sheetName);
}
//read excel all sheet
public static Map<String, List<Map>> readExcelAll(File file, String[] fields) throws IOException {
public static Map<String, List<Map>> readExcelAll(File file, String[] fields) {
return readExcelAll(file, fields, -1);
}
//read all excel no fields
public static Map<String, List<Map>> readExcelAll(File file) throws IOException {
public static Map<String, List<Map>> readExcelAll(File file) {
return readExcelAll(file, null, -1);
}
//read excel sheet[0]
private static List<Map> readExcel(File file, String[] fields, int lastRowNum, String sheetName) throws IOException {
private static List<Map> 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<String, List<Map>> readExcelAll(File file, String[] fields, int lastRowNum) throws IOException {
private static Map<String, List<Map>> readExcelAll(File file, String[] fields, int lastRowNum) {
Workbook wk = getWorkbook(file);
Map<String, List<Map>> data = new LinkedHashMap<>();
@ -270,7 +271,7 @@ public class ExcelKit {
*
* @author Lxyer 2016/8/1 10:32.
*/
private static List<Map> readExcel(Sheet sheet, String[] fields, int lastRowNum) throws OfficeXmlFileException {
private static List<Map> readExcel(Sheet sheet, String[] fields, int lastRowNum) {
if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()) {
lastRowNum = sheet.getLastRowNum();
}
@ -281,8 +282,7 @@ public class ExcelKit {
List<Map> 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<String> getSheetNames(File file) throws IOException {
public static List<String> getSheetNames(File file) {
Workbook wk = getWorkbook(file);
List<String> 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 {

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 {
/**
* 5redkale web项目导出示例
* @param head
* @param rows
* @param response
* @param xlsName
* @param fun
* @return
* @throws IOException
*/