public void excelUploadtest(@RequestParam("file")
MultipartFile file, @RequestParam("fileName") String fileName,
@RequestMapping(value = "add.do")
public ResponseEntity<?> add(HttpServletRequest
request) {
HttpHeaders headers = new HttpHeaders();
MediaType mediaType = new MediaType("text",
"html", Charset.forName("utf-8"));
headers.setContentType(mediaType);
return new ResponseEntity<Map<String,
Object>>("msg", headers, HttpStatus.OK);
}
@RequestMapping(value = "/excelUpload", method = RequestMethod.POST)
public ResponseEntity<JsonMessage>
excelUpload(@RequestParam("file") MultipartFile file, @RequestParam("fileName")
String fileName,
HttpServletResponse response)
throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_HTML);
ResponseEntity<JsonMessage>
responseEntity = new ResponseEntity<JsonMessage>(new
JsonMessage("无法读取上传的Excel文件,请重试。"),headers,HttpStatus.OK);
Workbook wb = null;
if (fileName.endsWith(".xls")) {
try {
wb = new
HSSFWorkbook(file.getInputStream());
} catch (Exception e) {
return
responseEntity;
}
} else if (fileName.endsWith(".xlsx")) {
try {
wb = new
XSSFWorkbook(file.getInputStream());
} catch (Exception e) {
return
responseEntity;
}
} else {
return responseEntity;
}
// 该工具类仅仅负责解析、数据绑定(含类型转换)
// TODO 业务值判断(存在性判断,唯一性判断等)
// 返回错误画面
Integer errorCount =
errorResultList.size();
if (errorCount > 0) {
JsonMessage jsonMessage = new
JsonMessage();
jsonMessage.setResult(errorResultList);
jsonMessage.setErrorMsg("您有<em
class=‘red‘>" + errorCount + "</em>条错误信息,上传失败!请修改模板后继续上传");
jsonMessage.setStatus(JsonMessage.STATUS_FAIL);
return new
ResponseEntity<JsonMessage>(jsonMessage, headers, HttpStatus.OK);
}
List<Medicine> medicines =
BeanMapper.mapList(medicineVOs, Medicine.class);
medicineService.batchInsertMedicines(medicines);
JsonMessage jsonMessage = new
JsonMessage();
jsonMessage.setMsg("恭喜您,上传成功!");
jsonMessage.setStatus(JsonMessage.STATUS_SUCCESS);
return new
ResponseEntity<JsonMessage>(jsonMessage, headers, HttpStatus.OK);
}