implementation group: 'org.apache.poi', name: 'poi', version: '5.2.2'
implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '5.2.2'
// excel导入
@CrossOrigin
@PostMapping(value = "/batch_sms_send/parseExcel", produces = {"application/json;charset=UTF-8"})
public void parseExcel(@RequestParam("file") MultipartFile file,HttpServletRequest request, HttpServletResponse response) {
try {
// @RequestParam("file") MultipartFile file 是用来接收前端传递过来的文件
// 1.创建workbook对象,读取整个文档
InputStream inputStream = file.getInputStream();
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(inputStream);
HSSFWorkbook wb = new HSSFWorkbook(poifsFileSystem);
// 2.读取页脚sheet
HSSFSheet sheetAt = wb.getSheetAt(0);
// 3.循环读取某一行
for (Row row : sheetAt) {
//先将第二列手机号转换为string格式
row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
// 4.读取每一行的单元格
String stringCellValue="";
String stringCellValue2="";
if(row.getCell(0)!=null && row.getCell(1)!=null){
stringCellValue = row.getCell(0).getStringCellValue(); // 第一列数据
stringCellValue2 = row.getCell(1).getStringCellValue();// 第二列
// 写多少个具体看大家上传的文件有多少列.....
// 测试是否读取到数据,及数据的正确性
System.out.println(stringCellValue);
System.out.println(stringCellValue2);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}