poi导入案例
@Transactional(rollbackFor = Exception.class)
public List<MerchantExcelEntity> uploadMerchant(MultipartFile file) throws Exception {
XSSFWorkbook book = null;
List<MerchantExcelEntity> dataList = new ArrayList<>();
try {
book = new XSSFWorkbook(file.getInputStream());
XSSFSheet sheet = book.getSheetAt(0);
if (!"身份证".equals(sheet.getRow(0).getCell(3).getStringCellValue())) {
throw new RuntimeException("请上传正确的门店导入文件");
}
if (!"开户行".equals(sheet.getRow(0).getCell(5).getStringCellValue())) {
throw new RuntimeException("请上传正确的门店导入文件");
}
XSSFRow row1 = sheet.getRow(0);
int i = 0;
while ((++i) <= sheet.getLastRowNum()) {
XSSFRow row = sheet.getRow(i);
MerchantExcelEntity entity = new MerchantExcelEntity();
if (null != row.getCell(0)) {
row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
entity.setName(row.getCell(0).getStringCellValue());
} else {
throw new RuntimeException("第" + (i + 1) + "姓名为空");
}
if (null != row.getCell(1)) {
row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
entity.setTel(row.getCell(1).getStringCellValue());
if (entity.getTel().length() > 11) {
throw new RuntimeException("请输入正确的手机号");
}
} else {
throw new RuntimeException("第" + (i + 1) + "手机号为空");
}
if (null != row.getCell(2)) {
row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
entity.setMerName(row.getCell(2).getStringCellValue());
} else {
throw new RuntimeException("第" + (i + 1) + "店铺名称为空");
}
if (null != row.getCell(3)) {
row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
entity.setIdNumber(row.getCell(3).getStringCellValue());
} else {
throw new RuntimeException("第" + (i + 1) + "身份证为空");
}
if (null != row.getCell(4)) {
row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
entity.setBankCard(row.getCell(4).getStringCellValue());
} else {
throw new RuntimeException("第" + (i + 1) + "银行卡号为空");
}
if (null != row.getCell(5)) {
row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
entity.setBankName(row.getCell(5).getStringCellValue());
} else {
throw new RuntimeException("第" + (i + 1) + "开户行为空");
}
if (null != row.getCell(6)) {
row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
entity.setRefundAddr(row.getCell(6).getStringCellValue());
} else {
throw new RuntimeException("第" + (i + 1) + "退货地址为空");
}
if (null != row.getCell(7)) {
row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
entity.setIsCheck(Integer.valueOf(row.getCell(7).getStringCellValue()));
} else {
throw new RuntimeException("第" + (i + 1) + "上架审核为空");
}
if (null != row.getCell(8)) {
row.getCell(8).setCellType(Cell.CELL_TYPE_STRING);
entity.setStockNum(Integer.valueOf(row.getCell(8).getStringCellValue()));
} else {
throw new RuntimeException("第" + (i + 1) + "库存数量上限为空");
}
if (null != row.getCell(9)) {
row.getCell(9).setCellType(Cell.CELL_TYPE_STRING);
entity.setAddNum(Integer.valueOf(row.getCell(9).getStringCellValue()));
} else {
throw new RuntimeException("第" + (i + 1) + "单次补货商品件数为空");
}
if (null != row.getCell(10)) {
row.getCell(10).setCellType(Cell.CELL_TYPE_STRING);
entity.setMerCode(row.getCell(10).getStringCellValue());
}
dataList.add(entity);
}
} catch (IOException e) {
log.error("保存数据异常", e);
}
return dataList;
}