<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
public AjaxJson importData(HttpServletRequest request,MultipartFile mFile) {
List<Map<String, Object>> dataSource = new ArrayList<>();
AjaxJson ajaxJson = new AjaxJson();
int rowNum = 0;
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if (multipartResolver.isMultipart(request)){
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//开始导入文件
mFile = multipartRequest.getFile("mFile");
String eName = "";
if(mFile==null){
ajaxJson.setSuccess(false);
ajaxJson.setMsg("请上传数据源文件!");
return ajaxJson;
}
// 获得上传文件的文件名
String fileName = mFile.getOriginalFilename();
// 获取文件扩展名
eName = fileName.substring(fileName.lastIndexOf(".") + 1);
if(!"xls".equals(eName)&&!"xlsx".equals(eName)){
ajaxJson.setSuccess(false);
ajaxJson.setMsg("请上传正确格式的数据源文件!");
return ajaxJson;
}
//数据是否正确;
Workbook workbook = null;
try {
InputStream inputStream = mFile.getInputStream();
if ("xls".equals(eName)) {
// 2003
workbook = new HSSFWorkbook(inputStream);
} else {
// 2007
workbook = new XSSFWorkbook(inputStream);
}
// 获取工作薄第一张表
Sheet sheet = workbook.getSheetAt(0);
// 获取名称
String sheetName = sheet.getSheetName().trim();
// 获取第一行
Row row = sheet.getRow(0);
if(null == row){
ajaxJson.setSuccess(false);
ajaxJson.setMsg("模板错误,请重新上传!");
return ajaxJson;
}
// 获得有效行数
rowNum = sheet.getLastRowNum();
if(0==rowNum){
ajaxJson.setSuccess(false);
ajaxJson.setMsg("数据源表中数据不得小于1条!");
return ajaxJson;
}
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
if(null==row.getCell(0)){
row.createCell(0);
}
row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
if(null==row.getCell(1)){
row.createCell(1);
}
row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
if(null==row.getCell(2)){
row.createCell(2);
}
row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
if(null==row.getCell(3)){
row.createCell(3);
}
row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
if(null==row.getCell(4)){
row.createCell(4);
}
row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
if(null==row.getCell(5)){
row.createCell(5);
}
row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
Pmcused pmcused = new Pmcused();
if(row.getCell(0)!=null && !"".equals(row.getCell(0).getStringCellValue())) {
pmcused.setId(row.getCell(0).getStringCellValue());//ID
}
if(row.getCell(1)!=null && !"".equals(row.getCell(1).getStringCellValue())){
pmcused.setCreateTime(row.getCell(1).getStringCellValue());
}
if(row.getCell(2)!=null && !"".equals(row.getCell(2).getStringCellValue())){
pmcused.setStartTime(row.getCell(2).getStringCellValue());
}
if(row.getCell(3)!=null && !"".equals(row.getCell(3).getStringCellValue())){
pmcused.setEndTime(row.getCell(3).getStringCellValue());
}
if(row.getCell(4)!=null && !"".equals(row.getCell(4).getStringCellValue())){
pmcused.setOrgCode(row.getCell(4).getStringCellValue());
}
if(row.getCell(5)!=null && !"".equals(row.getCell(5).getStringCellValue())){
pmcused.setOrgName(row.getCell(5).getStringCellValue());
}
if(row.getCell(6)!=null && !"".equals(row.getCell(6).getStringCellValue())){
pmcused.setHospLv(row.getCell(6).getStringCellValue());
}
if(row.getCell(7)!=null && !"".equals(row.getCell(7).getStringCellValue())){
pmcused.setPmcNum(row.getCell(7).getStringCellValue());
}
if(row.getCell(8)!=null && !"".equals(row.getCell(8).getStringCellValue())){
pmcused.setTotal(row.getCell(8).getStringCellValue());
}
if((!"".equals(row.getCell(4).getStringCellValue())) && (!"".equals(row.getCell(5).getStringCellValue()))){
HashMap<String, Object> map = new HashMap<>();
map.put("id",pmcused.getId());
map.put("createTime",pmcused.getCreateTime());
map.put("startTime",pmcused.getStartTime());
map.put("endTime",pmcused.getEndTime());
map.put("orgCode",pmcused.getOrgCode());
map.put("orgName",pmcused.getOrgName());
map.put("hospLv",pmcused.getHospLv());
map.put("pmcNum",pmcused.getPmcNum());
map.put("total",pmcused.getTotal());
try {
//这里是操作数据库,根据实际需求进行取舍
pmcused.setFlag("0");
pmcDataImportMapper.importDataSource(pmcused);
//将读取的数据存入dataSource集合中,用于页面展示
dataSource.add(map);
}catch (Exception e){
throw new ServiceException(ServiceCode.ERR_INSERT,"插入数据失败!");
}
}
}
} catch (Exception e) {
throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"网络异常,请联系管理员!");
}
}
ajaxJson.setSuccess(true);
ajaxJson.setMsg("导入成功,共记:"+rowNum+"条");
ajaxJson.setObj(dataSource);
return ajaxJson;
}
注:本案例以以下表格进行设计,实际以自己需求设计