上源码之前,说下要注意的问题:
一定要该清楚列宽和合并单元格之间的区别,如图:
直接上源码:
public int importData(MultipartFile file) throws IOException, InvalidFormatException { Workbook wb = WorkbookFactory.create(file.getInputStream()); Sheet sheet = wb.getSheetAt(0); Iterator<Row> rows = sheet.rowIterator(); List<User> list = new ArrayList<User>(); while (rows.hasNext()) { Row row = rows.next(); Cell cell = row.getCell(0); Cell cell2 = row.getCell(1); if (cell != null && cell2 != null) { cell.setCellType(Cell.CELL_TYPE_STRING); cell2.setCellType(Cell.CELL_TYPE_STRING); String name = cell.getStringCellValue(); String address = cell2.getStringCellValue(); if (StringUtils.isNotEmpty(name)) { User user = new User(); user.setName(name); user.setAddress(address); list.add(video); } } } return mapper.insertList(list); }