根据poi接收controller层的excel文件导入
可使用后缀名xls或xlsx格式的excel。
1.pom引入
org.apache.poi
poi
3.17
org.apache.poi
poi-ooxml
3.17
2.ExcelimportUtil 工具类创建
import com.guard.biz.common.util.excel.ExcelIn; import org.apache.commons.beanutils.BeanUtilsBean; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.reflect.FieldUtils; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ExcelimportUtil{ private static final Logger log = LoggerFactory.getLogger(ExcelimportUtil.class); private static BeanUtilsBean beanUtilsBean = new BeanUtilsBean(); static { beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.DateConverter(null), java.util.Date.class); } private final Map title_to_index = new HashMap<>(); private final List fields = new ArrayList<>(); private int firstCellNum = 0; private int lastCellNum = 0; private int firstRowNum = 0; private int lastRowNum = 0; private String sheetName; private Sheet sheet; public List read(InputStream in, Class clazz) throws Exception { gatherAnnotationFields(clazz); configSheet(in); configHeader(); List rList = null; try { rList = readContent(clazz); } catch (IllegalAccessException e) { throw new Exception(e); } catch (InstantiationException e) { throw new Exception(e); } catch (InvocationTargetException e) { throw new Exception(e); } return rList; } private List readContent(Class clazz) throws IllegalAccessException, InstantiationException, InvocationTargetException { Object o = null; Row row = null; List
3.ExcelIn注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.TYPE, ElementType.FIELD})
public @interface ExcelIn {
String sheetName() default "";
String title() default "";
}
4.创建excel中的对象
import lombok.Data;
import lombok.ToString;
import java.util.Date;
@ToString
@Data
@ExcelIn(sheetName = "用户")
public class User {
private String id;
@ExcelIn(title = "姓名")
private String name;
@ExcelIn(title = "年龄")
private Integer age;
@ExcelIn(title = "出生日期")
private Date birthDate;
}
5.controller层接收
@PostMapping("/batch/excel")
@ApiOperation(value = "根据excel文件批量导入")
public ResponseVO batchAddDeviceByExcelimport(MultipartFile multipartFile) {
return new ResponseVO(deviceService.addDeviceByExcelimport(multipartFile));
}
6.service处理(此处仅打印)
public boolean addDeviceByExcelimport(MultipartFile multipartFile) {
File file = null;
try {
file = File.createTempFile("temp", null);
} catch (IOException e) {
e.printStackTrace();
}
try {
multipartFile.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
}
file.deleteonExit();
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ExcelimportUtil reader = new ExcelimportUtil<>();
List userList = null;
try {
userList = reader.read(inputStream, User.class);
} catch (Exception e) {
log.error(e.getMessage());
throw new CodeException("51302", e.getMessage());
}
userList.stream().forEach(e -> log.info(e.toString()));
return true;
}
7.测试
(1)两种文件类型的excel
(2)excel中格式如下,注意红色箭头所指的地方 对应user对象中的字段以及sheet名
(3)swagger测试
(4)成功打印
总结
以上所述是小编给大家介绍的java springboot poi 从controller 接收不同类型excel 文件处理,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!



