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

@@ -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 {