poi的excel文件上传

本文只是用poi的介绍excel文件上传
controller层

//ResponseResult 是自定义实体
@RestController
@RequestMapping("/tech/truck-service-information")
public class TruckServiceInformationController {

    @Autowired
    TruckServiceInformationServices truckServiceInformationServices;
    //解析excel
    @PostMapping(value = "uploadTruckExcel")

    public ResponseResult uploadExcel(@RequestPart("file") MultipartFile file){
        ResponseResult result = truckServiceInformationServices.uploadExcel(file);
        return result;
    }
  }

service层

@Service
public class TruckServiceInformationServiceImpl extends ServiceImpl<TruckServiceInformationMapper, TruckServiceInformation> implements TruckServiceInformationServices {

    @Autowired
    TruckServiceInformationMapper truckServiceInformationMapper;

    @Override
    public ResponseResult uploadExcel(MultipartFile file) {
        String filename = file.getOriginalFilename();
        String fileType = filename.substring(filename.lastIndexOf("."));
        System.out.println("filename"+fileType);
        try {
            List<TruckServiceInformation> lists = new ArrayList();
            InputStream inputStream = file.getInputStream();
            Workbook wb = null;
            //截取文件后缀判断是否excel文件 以及什么类型的文件
            if (fileType.equals(".xls")) {
                wb = new HSSFWorkbook(inputStream);
            } else if (fileType.equals(".xlsx")) {
                wb = new XSSFWorkbook(inputStream);
            }else if(!fileType.equals(".xls")||!fileType.equals(".xlsx")){
                throw new CustomizeException(20001,"文件格式错误");
            }
            Sheet sheetAt = wb.getSheetAt(0);
            int i = 0;
            for (Row row : sheetAt) {
                TruckServiceInformation truckServiceInformation = new TruckServiceInformation();
                if(row.getRowNum() < 1 || i == 0){
                    i++;
                    continue;
                }
                String Plate_Num = row.getCell(9).getStringCellValue();
                System.out.println(Plate_Num);
                //查询数据库是否有值
                Integer plate_num = truckServiceInformationMapper.selectCount(new QueryWrapper<TruckServiceInformation>().eq("Plate_Num", Plate_Num));
                if (plate_num==0){
                    int no =(int)row.getCell(0).getNumericCellValue();
                    String contract = row.getCell(1).getStringCellValue();
                    String equipmentType = row.getCell(2).getStringCellValue();
                    String Brand = row.getCell(3).getStringCellValue();
                    String Sn = row.getCell(4).getStringCellValue();
                    String Engine = row.getCell(5).getStringCellValue();
                    String Model = row.getCell(6).getStringCellValue();
                    String BelongCompany = row.getCell(7).getStringCellValue();
                    String PJB_Number =null;
                    PJB_Number=row.getCell(8).getStringCellValue();
                    String Arrivals_Date =row.getCell(10).getStringCellValue();
                    truckServiceInformation.setContract(contract);
                    truckServiceInformation.setEngineType(equipmentType);
                    truckServiceInformation.setBrand(Brand);
                    truckServiceInformation.setSn(Sn);
                    truckServiceInformation.setEngine(Engine);
                    truckServiceInformation.setModel(Model);
                    truckServiceInformation.setBelongCompany(BelongCompany);
                    truckServiceInformation.setPjbNumber(PJB_Number);
                    truckServiceInformation.setPlateNum(Plate_Num);
                    truckServiceInformation.setArrieveDate(new Date());
                    //插入获取的值
                    int insert = truckServiceInformationMapper.insert(truckServiceInformation);
                }
                lists.add(truckServiceInformation);
            }
            return ResponseResult.ok("上传成功"+lists);
        } catch (IOException e) {
            return ResponseResult.error();
        }
    }
}

所用的的包

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.6</version>
        </dependency>
<!--新加excel包 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.6</version>
        </dependency>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值