Springboot/SpringMvc 读取上传 xls 文件内容

时间:2023-03-09 03:32:30
Springboot/SpringMvc 读取上传 xls 文件内容
/**
* 读取上传 xls 内容返回
* @param file
* @return
*/
@RequestMapping(value = "/read.xls")
@ResponseBody
public String read(@RequestParam("file") MultipartFile file) {
//判断上传的文件类型是不是图片
int maxSize = 1024 * 1024 * 2;
if (file == null || file.getSize()> maxSize){
object.put("msg","上传文件 不能为空/不能大于2M");
}else{
StringBuffer phones = new StringBuffer();
try {
Workbook wb = WorkbookFactory.create(file.getInputStream());
Sheet sheet = wb.getSheetAt(0);// 第一个脚本下的
logger.info(sheet.toString());
sheet.forEach(e->{
e.forEach(e1->{
e1.setCellType(Cell.CELL_TYPE_STRING);
phones.append(e1.getStringCellValue() + ",");
});
});
} catch (Exception e) {
e.printStackTrace();
}
if(phones.length()>0){
phones.delete(phones.length()-1,phones.length());
}
}
return phones.toString();
}