json接口
@PostMapping("/upload")
@ResponseBody
public String upload(MultipartFile file) throws IOException {
InputStream in =null;
try {
//将file转InputStream
in = file.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
//将InputStream转XLSX对象(即表格对象)
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(in);
//获取表格的第一页
XSSFSheet xssfSheet=xssfWorkbook.getSheetAt(0);
//获取该页有多少数据
int rowNum= xssfSheet.getLastRowNum();
for(int i=0;i<rowNum;i++){
User user = new User();
//获取当前行
XSSFRow xssfRow=xssfSheet.getRow(i);
//以此获得该行的所有单元格
String sno=xssfRow.getCell(0).toString();
String name=xssfRow.getCell(1).toString();
String clbun=xssfRow.getCell(2).toString();
String node=xssfRow.getCell(3).toString();
//将数据写入实体类
user.setUsername(sno);
user.setRealname(name);
user.setOrganizationId(clbun);
user.setPassword(node);
userService.insert(user);
//System.out.println("sno:::"+sno+"name:::"+"clbun:::"+clbun+"node:::"+node);
}
return "文件上传成功!";
}