表格导入

@RequestMapping(value = "/****", method = RequestMethod.POST)
    public void import(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws Exception {
        DeviceInfoExample example = new DeviceInfoExample();
        List<DeviceInfo> dinfo = deviceService.selectDevice(example);
        // 如果文件不为空,写入上传路径
        if (!file.isEmpty()) {
            // 上传文件路径
            String fileName = "D:/new/" + file.getOriginalFilename();
            file.transferTo(new File(fileName));
            String[] split2 = fileName.split("\\\\");
            String Str = "";
            for (int i = 0; i < split2.length; i++) {
                if (i < split2.length - 1) {
                    Str += split2[i] + "/";
                } else {
                    Str += split2[i];
                }
            }
            String filename = new String(Str.getBytes(), "utf-8");
            InputStream stream = new FileInputStream(filename);
            Workbook wb = null;
            if (filename.endsWith("xls")) {
                wb = new HSSFWorkbook(stream);
            } else if (filename.endsWith("xlsx")) {
                wb = new XSSFWorkbook(stream);
            } else {
                System.out.println("您输入的excel格式不正确");
            }
            Sheet sheet1 = wb.getSheetAt(0);
            String meme = "";
            for (Row row : sheet1) {
                DeviceInfo device = new DeviceInfo();
                for (Cell cell : row) {
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    meme += cell.getStringCellValue() + "--";
                }
                String[] split = meme.split("--");
                meme = "";
                if (split[0].equals("序号")) {
                    continue;
                }
                Integer routeid = 0;
                Float lon = null ;
                Float lat = null ;
                for (DeviceInfo ds : dinfo) {
                    if (ds.getDevicename().equals(split[2])) {
                        routeid = ds.getRouteid();
                        lon = ds.getLongitude();
                        lat = ds.getLatitude();
                    }
                }
                // int id = car.size()+Integer.parseInt(split[0]); // 每行数据的id
                // carInfo.setId(id);
                SimpleDateFormat time1 = new SimpleDateFormat("yyyy-MM-dd");
                Date date1 = time1.parse(split[7]);
                device.setRouteid(routeid);
                ...
                if (split[10].equals("正在工作")) {
                    device.setUsestatus(0);
                } else {
                    device.setUsestatus(1);
                }
                deviceMapper.insert(device);
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值