merge
This commit is contained in:
@@ -38,6 +38,172 @@ public class ExcelUtil {
|
||||
*/
|
||||
public final static String SEPARATOR = ";";
|
||||
|
||||
/**
|
||||
* 获取工作簿
|
||||
*
|
||||
* @param title
|
||||
* @param columns
|
||||
* @param list
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HSSFWorkbook Export(String title, String[] columns, List<String[]> list) throws Exception {
|
||||
// 声明一个工作薄
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
// 声明一个单子并命名
|
||||
HSSFSheet sheet = wb.createSheet(title);
|
||||
// 给单子名称一个长度
|
||||
sheet.setDefaultColumnWidth((short) 15);
|
||||
// 生成一个样式
|
||||
HSSFCellStyle style = wb.createCellStyle();
|
||||
// 创建第一行(也可以称为表头)
|
||||
HSSFRow row = sheet.createRow(0);
|
||||
// 样式字体居中
|
||||
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
|
||||
// 给表头第一行一次创建单元格
|
||||
if (null != columns && columns.length > 0) {
|
||||
for (int i = 0; i < columns.length; i++) {
|
||||
HSSFCell cell = row.createCell((int) i);
|
||||
cell.setCellValue(columns[i]);
|
||||
cell.setCellStyle(style);
|
||||
}
|
||||
}
|
||||
if (CollectionUtils.isNotEmpty(list)) {
|
||||
// 向单元格里填充数据
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
row = sheet.createRow(i + 1);
|
||||
assert columns != null;
|
||||
for (int j = 0; j < columns.length; j++) {
|
||||
row.createCell(j).setCellValue(list.get(i)[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return wb;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将数据list转换成excel导出的形式
|
||||
*
|
||||
* @param list
|
||||
* @return
|
||||
*/
|
||||
public static <T> List<String[]> convert2List(List<T> list) {
|
||||
List<String[]> result = new ArrayList<>(list.size());
|
||||
T obj4Class = list.get(0);
|
||||
Class<?> classOfT = obj4Class.getClass();
|
||||
Field[] declaredFields = classOfT.getDeclaredFields();
|
||||
String[] temp;
|
||||
for (T obj : list) {
|
||||
temp = new String[declaredFields.length];
|
||||
for (int i = 0; i < declaredFields.length; i++) {
|
||||
String fieldName = declaredFields[i].getName();
|
||||
StringBuffer getMethodStr;
|
||||
// 拼接get方法
|
||||
getMethodStr = new StringBuffer();
|
||||
getMethodStr.append("get");
|
||||
getMethodStr.append(fieldName.substring(0, 1).toUpperCase());
|
||||
getMethodStr.append(fieldName.substring(1));
|
||||
String value = "";
|
||||
Method getMethod;
|
||||
try {
|
||||
getMethod = classOfT.getMethod(getMethodStr.toString());
|
||||
} catch (NoSuchMethodException e1) {
|
||||
System.out.println("NoSuchMethodException");
|
||||
continue;
|
||||
} catch (SecurityException e1) {
|
||||
System.out.println("SecurityException");
|
||||
continue;
|
||||
}
|
||||
// 执行get方法
|
||||
try {
|
||||
value = String.valueOf(getMethod.invoke(obj, new Object[0]));
|
||||
value = Objects.equals("null", value) ? null : value;
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
temp[i] = value;
|
||||
}
|
||||
result.add(temp);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将导入的excel文件生成类
|
||||
*
|
||||
* @param file excel文件
|
||||
* @param sheetNum excel页签
|
||||
* @param classVO 要导出的类型
|
||||
* @return
|
||||
*/
|
||||
public static <T> List<T> getObjectFromExcel(File file, int sheetNum, Class<T> classVO) {
|
||||
List<T> resultList = new ArrayList<>();
|
||||
T obj;
|
||||
try {
|
||||
// 获取解析结果:每行是一个String
|
||||
// 格式:[Name;Sex;Email;NeedRePswd;Status;UserName;NickName;UnitId;Rank;Office,
|
||||
// INPUT111;1.0;INPUT111@sss.com;0.0;1.0;INPUT111;INPUT111;asdfasdfasdfasdf;1.0,
|
||||
// INPUT222;1.0;INPUT111@sss.com;0.0;1.0;INPUT111;INPUT111;asdfasdfasdfasdf;1.0]
|
||||
List<String> importObjects = exportListFromExcel(file, sheetNum);
|
||||
if (CollectionUtils.isNotEmpty(importObjects)) {
|
||||
// Class<? extends Object> classVO = obj.getClass();
|
||||
Constructor<?> cons[] = classVO.getConstructors();
|
||||
// 取出列名称用来设置set方法
|
||||
String colNames = importObjects.get(0);
|
||||
// 解析列名
|
||||
String[] colName = colNames.split(";");
|
||||
String setMethodStr;
|
||||
for (int i = 1; i < importObjects.size(); i++) {
|
||||
// 解析数据
|
||||
String[] values = importObjects.get(i).split(SEPARATOR);
|
||||
// 初始化对象
|
||||
try {
|
||||
obj = (T) cons[0].newInstance();
|
||||
} catch (Exception e3) {
|
||||
e3.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
for (int j = 0; j < values.length; j++) {
|
||||
// set方法名
|
||||
setMethodStr = "set" + colName[j];
|
||||
Method setMethod;
|
||||
// 尝试调用set方法,因无法确定参数,尝试参数类型,目前只有String和Integer两种
|
||||
try {
|
||||
setMethod = classVO.getMethod(setMethodStr, String.class);
|
||||
setMethod.invoke(obj, String.valueOf(values[j]));
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
setMethod = classVO.getMethod(setMethodStr, Integer.class);
|
||||
setMethod.invoke(obj, Double.valueOf(values[j]).intValue());
|
||||
} catch (Exception e1) {
|
||||
// 目前没有bool值参数暂不添加
|
||||
// try
|
||||
// {
|
||||
// setMethod =
|
||||
// classVO.getMethod(setMethodStr,Boolean.class);
|
||||
// setMethod.invoke(obj,
|
||||
// Boolean.valueOf(values[j]));
|
||||
// }
|
||||
// catch (Exception e3)
|
||||
// {
|
||||
// }
|
||||
System.out.println("Exception" + e);
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
resultList.add(obj);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 由Excel文件的Sheet导出至List
|
||||
*
|
||||
@@ -125,169 +291,4 @@ public class ExcelUtil {
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将导入的excel文件生成类
|
||||
*
|
||||
* @param file excel文件
|
||||
* @param sheetNum excel页签
|
||||
* @param classVO 要导出的类型
|
||||
* @return
|
||||
*/
|
||||
public static <T> List<T> getObjectFromExcel(File file, int sheetNum, Class<T> classVO) {
|
||||
List<T> resultList = new ArrayList<>();
|
||||
T obj;
|
||||
try {
|
||||
// 获取解析结果:每行是一个String
|
||||
// 格式:[Name;Sex;Email;NeedRePswd;Status;UserName;NickName;UnitId;Rank;Office,
|
||||
// INPUT111;1.0;INPUT111@sss.com;0.0;1.0;INPUT111;INPUT111;asdfasdfasdfasdf;1.0,
|
||||
// INPUT222;1.0;INPUT111@sss.com;0.0;1.0;INPUT111;INPUT111;asdfasdfasdfasdf;1.0]
|
||||
List<String> importObjects = exportListFromExcel(file, sheetNum);
|
||||
if (CollectionUtils.isNotEmpty(importObjects)) {
|
||||
// Class<? extends Object> classVO = obj.getClass();
|
||||
Constructor<?> cons[] = classVO.getConstructors();
|
||||
// 取出列名称用来设置set方法
|
||||
String colNames = importObjects.get(0);
|
||||
// 解析列名
|
||||
String[] colName = colNames.split(";");
|
||||
String setMethodStr;
|
||||
for (int i = 1; i < importObjects.size(); i++) {
|
||||
// 解析数据
|
||||
String[] values = importObjects.get(i).split(SEPARATOR);
|
||||
// 初始化对象
|
||||
try {
|
||||
obj = (T) cons[0].newInstance();
|
||||
} catch (Exception e3) {
|
||||
e3.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
for (int j = 0; j < values.length; j++) {
|
||||
// set方法名
|
||||
setMethodStr = "set" + colName[j];
|
||||
Method setMethod;
|
||||
// 尝试调用set方法,因无法确定参数,尝试参数类型,目前只有String和Integer两种
|
||||
try {
|
||||
setMethod = classVO.getMethod(setMethodStr, String.class);
|
||||
setMethod.invoke(obj, String.valueOf(values[j]));
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
setMethod = classVO.getMethod(setMethodStr, Integer.class);
|
||||
setMethod.invoke(obj, Double.valueOf(values[j]).intValue());
|
||||
} catch (Exception e1) {
|
||||
// 目前没有bool值参数暂不添加
|
||||
// try
|
||||
// {
|
||||
// setMethod =
|
||||
// classVO.getMethod(setMethodStr,Boolean.class);
|
||||
// setMethod.invoke(obj,
|
||||
// Boolean.valueOf(values[j]));
|
||||
// }
|
||||
// catch (Exception e3)
|
||||
// {
|
||||
// }
|
||||
System.out.println("Exception" + e);
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
resultList.add(obj);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取工作簿
|
||||
* @param title
|
||||
* @param columns
|
||||
* @param list
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HSSFWorkbook Export(String title, String[] columns, List<String[]> list) throws Exception {
|
||||
// 声明一个工作薄
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
// 声明一个单子并命名
|
||||
HSSFSheet sheet = wb.createSheet(title);
|
||||
// 给单子名称一个长度
|
||||
sheet.setDefaultColumnWidth((short) 15);
|
||||
// 生成一个样式
|
||||
HSSFCellStyle style = wb.createCellStyle();
|
||||
// 创建第一行(也可以称为表头)
|
||||
HSSFRow row = sheet.createRow(0);
|
||||
// 样式字体居中
|
||||
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
|
||||
// 给表头第一行一次创建单元格
|
||||
if (null != columns && columns.length > 0) {
|
||||
for (int i = 0; i < columns.length; i++) {
|
||||
HSSFCell cell = row.createCell((int) i);
|
||||
cell.setCellValue(columns[i]);
|
||||
cell.setCellStyle(style);
|
||||
}
|
||||
}
|
||||
if (CollectionUtils.isNotEmpty(list)) {
|
||||
// 向单元格里填充数据
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
row = sheet.createRow(i + 1);
|
||||
assert columns != null;
|
||||
for (int j = 0; j < columns.length; j++) {
|
||||
row.createCell(j).setCellValue(list.get(i)[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return wb;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将数据list转换成excel导出的形式
|
||||
*
|
||||
* @param list
|
||||
* @return
|
||||
*/
|
||||
public static <T> List<String[]> convert2List(List<T> list) {
|
||||
List<String[]> result = new ArrayList<>(list.size());
|
||||
T obj4Class = list.get(0);
|
||||
Class<?> classOfT = obj4Class.getClass();
|
||||
Field[] declaredFields = classOfT.getDeclaredFields();
|
||||
String[] temp;
|
||||
for (T obj : list) {
|
||||
temp = new String[declaredFields.length];
|
||||
for (int i = 0; i < declaredFields.length; i++) {
|
||||
String fieldName = declaredFields[i].getName();
|
||||
StringBuffer getMethodStr;
|
||||
// 拼接get方法
|
||||
getMethodStr = new StringBuffer();
|
||||
getMethodStr.append("get");
|
||||
getMethodStr.append(fieldName.substring(0, 1).toUpperCase());
|
||||
getMethodStr.append(fieldName.substring(1));
|
||||
String value = "";
|
||||
Method getMethod;
|
||||
try {
|
||||
getMethod = classOfT.getMethod(getMethodStr.toString());
|
||||
} catch (NoSuchMethodException e1) {
|
||||
System.out.println("NoSuchMethodException");
|
||||
continue;
|
||||
} catch (SecurityException e1) {
|
||||
System.out.println("SecurityException");
|
||||
continue;
|
||||
}
|
||||
// 执行get方法
|
||||
try {
|
||||
value = String.valueOf(getMethod.invoke(obj, new Object[0]));
|
||||
value = Objects.equals("null", value) ? null : value;
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
temp[i] = value;
|
||||
}
|
||||
result.add(temp);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user