本文章都是自创,如有类同纯属巧合,转发的朋友请写我的文章链接
excel的导入
1.接收方法
@RequestMapping("imp")
@ResponseBody
public Result imp(MultipartFile file) throws IOException, InvalidFormatException, ParseException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Workbook workbook = WorkbookFactory.create(file.getInputStream());
Sheet sheet = workbook.getSheetAt(0);
List<User> users = importData(sheet);
UserService.imp(users);
return result();
}
2. 写一个excel对应实体属性的list
private ArrayList<String> list = new ArrayList<String>(Arrays.asList(
"Name","Sex","ShenGao","TiZhong","Dept"
));
3.写将excel信息转换成实体类的方法
private List<User> importData(Sheet sheet) throws ParseException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
List<User> user= new ArrayList<>();
int lastRowNum = sheet.getLastRowNum();
for (int i = 0; i < lastRowNum; i++) {
User user= new User();
Field[] fields = user.getClass().getDeclaredFields();
Map<String, Integer> map = new HashMap<>();
Row row = sheet.getRow(i + 1);
for (int i1 = 0; i1 < fields.length; i1++) {
Field declaredField = fields[i1];
String name = declaredField.getName();
name = name.substring(0, 1).toUpperCase() + name.substring(1);
map.put(name, i1);
}
for (int j = 0; j < list.size(); j++) {
String s = list.get(j);
Cell cell = row.getCell(j);
Integer integer = map.get(s);
if (integer != null) {
Field field = fields[integer];
setPattern(field,cell,user,s);
}
}
users.add(user);
}
return users;
}
setPattern对应的方法及作用
private void setPattern(Field field,Cell cell,User user,String s) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ParseException {
if (cell == null) {
return;
}
String type = field.getGenericType().toString();
if (type.equals("class java.lang.String")) {
Method m = user.getClass().getMethod("set" + s,String.class);
m.invoke(user, ExcelUtil.cellStringValue(cell));
}
if (type.equals("class java.lang.Integer")) {
Method m = user.getClass().getMethod("set" + s,Integer.class);
m.invoke(user, ExcelUtil.cellIntegerValue(cell));
}
if (type.equals("class java.lang.Short")) {
Method m = user.getClass().getMethod("set" + s,Short.class);
m.invoke(user, ExcelUtil.cellShortValue(cell));
}
if (type.equals("class java.lang.Double")) {
Method m = user.getClass().getMethod("set" + s,Double.class);
m.invoke(user, ExcelUtil.cellDoubleValue(cell));
}
if (type.equals("class java.lang.Boolean")) {
Method m = user.getClass().getMethod("set" + s,Boolean.class);
m.invoke(user, ExcelUtil.cellBooleanValue(cell));
}
if (type.equals("class java.util.Date")) {
Method m = user.getClass().getMethod("set" + s,Date.class);
m.invoke(user, ExcelUtil.cellDateVlaue(cell));
}
}
excel的对应操作util模板
package com.aisino.zhgl.zichan.util;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ExcelUtil {
public static Double cellDoubleValue(Cell cell) {
if (cell == null) {
return null;
}
return cell.getNumericCellValue();
}
public static Integer cellIntegerValue(Cell cell) {
if (cell == null) {
return null;
}
Double d = cell.getNumericCellValue();
return d.intValue();
}
public static Short cellShortValue(Cell cell) {
if (cell == null) {
return null;
}
Double d = cell.getNumericCellValue();
Short aShort = (short) d.intValue();
return aShort;
}
public static boolean cellBooleanValue(Cell cell) {
boolean b = cell.getBooleanCellValue();
return b;
}
public static Date cellDateVlaue(Cell cell) throws ParseException {
if (cell == null) {
return null;
}
if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) {
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date d = cell.getDateCellValue();
return d;
}
}
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String s = cell.getStringCellValue().trim();
Date date = format.parse(s);
return date;
}
public static String cellStringValue(Cell cell) {
if (cell == null) {
return null;
}
DateFormat format = new SimpleDateFormat("yyyy/MM/dd");
if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) {
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date dateCellValue = cell.getDateCellValue();
String format1 = format.format(dateCellValue);
return format1;
}
Double numbe = cell.getNumericCellValue();
String s = Integer.toString(numbe.intValue());
return s;
}
return cell.getStringCellValue().trim();
}
}
serviceImpl中的方法
@Override
public void imp(List<User> users) {
for (User user: users) {
dao.insert(user);
}
}