控制层:
/**
* 通过Excel表格导入
* @param getResponse
* @param path
* @throws CommonException
* @throws IOException
*/
@ApiOperation(value="从Excel导入提现记录")
@RequestMapping(value = "/import", method = RequestMethod.POST)
@Transactional
public RetEntity<Cash> importByExcel(HttpServletRequest request,@RequestParam("file") MultipartFile file,HttpServletResponse getResponse) throws CommonException, IOException {
String[] title = "序号,记录Id,申请人,金额,状态,创建日期".split(",");
return service.importByExcel(request,file,getResponse,title);
}
具体实现:
/**
* 从Excel导入提现记录
*
* @throws IOException
*/
@Override
public RetEntity<Cash> importByExcel(HttpServletRequest request,
@RequestParam("file") MultipartFile file,
HttpServletResponse response, String[] title) throws IOException {
Map<String, Integer> map = new HashMap<String, Integer>();
Cash onecash;
RetEntity<Cash> retEntity = new RetEntity<Cash>();
InputStream fin = file.getInputStream();
// 根据指定的文件输入流导入Excel从而产生Workbook对象
Workbook workbook = new HSSFWorkbook(fin);
// 获取Excel文档中的第一个表单
Sheet sht0 = workbook.getSheetAt(0);
Row row = sht0.getRow(0);
// 初始化设置在Excel中存在的对应的标题数为0
int i = 0;
// 遍历第一行(标题行),获取Excel文件中的标题中含有的给与的标题的存在数
for (Cell cell : row) {
for (String ti : title) {
if (ti.equals(cell.getStringCellValue())) {
i++;
}
}
}
// 遍历第一行,读取标题
for (Cell cell : row) {
// 如果给与的标题全部存在于Excel文件中,则将标题作为键值,将所在列的值作为值存入map中
if (i == title.length) {
map.put(cell.getStringCellValue(), cell.getColumnIndex());
} else {// 如果缺少列或者标题被修改
try {
throw new CommonException(
CommonConstant.RESULT_CODE_EXPECTATION_FAILED,
MsgConstant.RET_USER_UPDATE_FAILURE);
} catch (CommonException e) {
e.printStackTrace();
}
retEntity.setCode(CommonConstant.RESULT_CODE_NOT_FOUND);
retEntity.setMessage("更新失败,Excel中的标题被更改或者缺少标题");
return retEntity;
}
}
List<Cash> cashlist = new ArrayList<Cash>();
// 对Sheet中的每一行进行迭代
for (Row r : sht0) {
// 如果当前行的行号(从0开始)未达到2(第三行)则从新循环
if (r.getRowNum() < 1) {
continue;
}
if (r.getCell(map.get("记录Id")) != null) {
r.getCell(map.get("记录Id")).setCellType(Cell.CELL_TYPE_STRING);
onecash = cashService.selectById(r.getCell(map.get("记录Id"))
.getStringCellValue());// 根据ID取得相应的记录
// 如果查到记录则进行相应操作
if (onecash != null) {
if (r.getCell(map.get("申请人")) != null) {
r.getCell(map.get("申请人")).setCellType(
Cell.CELL_TYPE_STRING);
onecash.setRealName(r.getCell(map.get("申请人"))
.getStringCellValue());// 申请人
}
if (r.getCell(map.get("金额")) != null) {
r.getCell(map.get("金额")).setCellType(
Cell.CELL_TYPE_STRING);
onecash.setMoney(r.getCell(map.get("金额"))
.getStringCellValue());// 金额
}
if (r.getCell(map.get("状态")) != null) {
r.getCell(map.get("状态")).setCellType(
Cell.CELL_TYPE_STRING);
String status = r.getCell(map.get("状态"))
.getStringCellValue();// 取得状态
switch (status) {
// 0:新申请,1提现完成,9提现撤销
case "新申请":
onecash.setStatus("0");
break;
case "提现完成":
onecash.setStatus("1");
break;
case "提现撤销":
onecash.setStatus("9");
break;
default:
try {
throw new CommonException(
CommonConstant.RESULT_CODE_EXPECTATION_FAILED,
MsgConstant.RET_USER_UPDATE_FAILURE);
} catch (CommonException e) {
e.printStackTrace();
}
retEntity
.setCode(CommonConstant.RESULT_CODE_NOT_FOUND);
retEntity.setMessage("更新失败,有状态的更改不符合规范");
return retEntity;
}
}
if (r.getCell(map.get("创建日期")) != null) {
r.getCell(map.get("创建日期")).setCellType(
Cell.CELL_TYPE_STRING);
onecash.setCreateTime(r.getCell(map.get("创建日期"))
.getStringCellValue());// 创建时间
}
onecash.setUpdateTime(new Date().toString());
cashlist.add(onecash);
cashService.updateAllColumnById(onecash);
retEntity.setCode(CommonConstant.RESULT_CODE_OK);
retEntity.setMessage("更新成功");
} else {
try {
throw new CommonException(
CommonConstant.RESULT_CODE_EXPECTATION_FAILED,
MsgConstant.RET_USER_UPDATE_FAILURE);
} catch (CommonException e) {
e.printStackTrace();
}
retEntity.setCode(CommonConstant.RESULT_CODE_NOT_FOUND);
retEntity.setMessage("更新失败,有ID被更改");
return retEntity;
}
}
}
// 关闭资源
fin.close();
return retEntity;
}