This commit is contained in:
xiangwb
2017-11-21 09:56:40 +08:00
parent 40c70a85ba
commit 349cec722a
4 changed files with 185 additions and 172 deletions
@@ -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;
}
}
@@ -4,10 +4,12 @@ import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.xwbing.handler.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.ArrayList;
@@ -56,6 +58,19 @@ public class DispatcherServletConfig extends WebMvcConfigurerAdapter {
super.addResourceHandlers(registry);
}
/**
* 视图配置
*
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login.html");
//添加更多
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
super.addViewControllers(registry);
}
/**
* 扩展消息转换器,增加fastjson
*
@@ -18,13 +18,9 @@ public class ServletConfig {
private final Logger logger = LoggerFactory.getLogger(ServletConfig.class);
@Bean
public CaptchaServlet captchaServlet() {
return new CaptchaServlet();
}
@Bean
public ServletRegistrationBean captchaServletRegistrationBean(CaptchaServlet captchaServlet) {
public ServletRegistrationBean captchaServletRegistrationBean() {
logger.info("注册验证码servlet ======================= ");
CaptchaServlet captchaServlet = new CaptchaServlet();
ServletRegistrationBean registration = new ServletRegistrationBean(captchaServlet);
registration.setEnabled(true);
registration.addUrlMappings("/servlet/captcha");
@@ -26,6 +26,8 @@ public class LoginInterceptor extends HandlerInterceptorAdapter {
private static final Set<String> set = new HashSet<>();//拦截器白名单
static {
//映射到登录页面,不拦截
set.add("/");
//验证码
set.add("/servlet/captcha");
//swagger
@@ -33,7 +35,6 @@ public class LoginInterceptor extends HandlerInterceptorAdapter {
set.add("/swagger-resources");
set.add("/configuration/ui");
set.add("/configuration/security");
//
}
@Override