excel导入文件数据

    @RequestMapping(value = "improtHd",method = RequestMethod.POST,produces = {"application/json;charset = utf-8"})
    public JSONObject improtHd(@RequestParam MultipartFile multipartFile,@RequestHeader("token") String token) {
        try {
            Map<String,Object> map = new HashMap<>();
            SetMsg.setUser(map, (Map<String, Object>) redisTemplate.opsForValue().get("USERINFO_"+token));
            InputStream inputStream = multipartFile.getInputStream();
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            XSSFSheet sheetAt = workbook.getSheetAt(0);
            sheetAt.getLastRowNum();
            for(int i = 4;i <= sheetAt.getLastRowNum();i++){
                map.put("HDCONTENT",sheetAt.getRow(i).getCell(0).toString());
                map.put("HDTYPE",sheetAt.getRow(i).getCell(1).toString());
                map.put("ZZPLAIN",sheetAt.getRow(i).getCell(2).toString());
                map.put("YYFX",sheetAt.getRow(i).getCell(3).toString());
                map.put("ZJCS",sheetAt.getRow(i).getCell(4).toString());
                map.put("ZLQX",sheetAt.getRow(i).getCell(5).toString());
                map.put("ZJLY",sheetAt.getRow(i).getCell(6).toString());
                map.put("ZRR",sheetAt.getRow(i).getCell(7).toString());
                map.put("ZGWCQK",sheetAt.getRow(i).getCell(8).toString());
                map.put("CHECKDATE",sheetAt.getRow(i).getCell(9).toString());
                map.put("QRR",sheetAt.getRow(i).getCell(10).toString());
                map.put("QRRQ",sheetAt.getRow(i).getCell(11).toString());
                map.put("FCR",sheetAt.getRow(i).getCell(12).toString());
                map.put("FCRQ",sheetAt.getRow(i).getCell(13).toString());
                hdServie.addWdb(map);
            }
            return JSONUtil.SUCCESSJson(null, CommonEnum.SUCCESS_CODE, CommonEnum.SUCCESS_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONUtil.ErrorJson(null, CommonEnum.FAILE_CODE, CommonEnum.FAILE_MSG);
        }
    }

 or

    @PostMapping(value = "/importEquipmentList", produces = {"application/json;charset=UTF-8"})
    public JSONObject importDeptExcel(@RequestParam("file") MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            XSSFSheet sheet = workbook.getSheetAt(0);
            List<Map<String,Object>> list = new ArrayList<>();
            for (Row row:sheet) {//逐行插入数据
                if(row.getRowNum() > 1) {
                    Map<String, Object> map = new HashMap<>();
                    row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                    row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                    row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                    row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
                    row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
                    row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
                    row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
                    row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
                    row.getCell(8).setCellType(Cell.CELL_TYPE_STRING);
                    row.getCell(9).setCellType(Cell.CELL_TYPE_STRING);
                    row.getCell(10).setCellType(Cell.CELL_TYPE_STRING);
                    row.getCell(11).setCellType(Cell.CELL_TYPE_STRING);
                    row.getCell(12).setCellType(Cell.CELL_TYPE_STRING);
                    map.put("equipmentCode", row.getCell(0).getStringCellValue());
                    map.put("equipmentName", row.getCell(1).getStringCellValue());
                    map.put("equipmentPositionNumber", row.getCell(2).getStringCellValue());
                    map.put("equipmentTypeName", row.getCell(3).getStringCellValue());
                    map.put("departmentId", row.getCell(4).getStringCellValue());
                    map.put("departmentName", row.getCell(5).getStringCellValue());
                    map.put("liablePersonName", row.getCell(6).getStringCellValue());
                    map.put("functionLocation", row.getCell(7).getStringCellValue());
                    map.put("latitude", row.getCell(8).getStringCellValue());
                    map.put("longitude", row.getCell(9).getStringCellValue());
                    map.put("equipmentStatus", row.getCell(10).getStringCellValue());
                    map.put("isSpecialEquipment", row.getCell(11).getStringCellValue());
                    map.put("detectionTime", row.getCell(12).getStringCellValue());
                    list.add(map);
                }
            }
            equipmentService.addEquipmentList(list);
            return JSONUtil.SUCCESSJson(null, CommonEnum.SUCCESS_CODE, CommonEnum.SUCCESS_MSG);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return JSONUtil.SUCCESSJson(null, CommonEnum.SUCCESS_CODE, "上传失败");
        }
    }

 

posted @ 2022-09-01 19:29  _Lawrence  阅读(161)  评论(0编辑  收藏  举报