zoukankan      html  css  js  c++  java
  • 文件导入

    public String importKeyPointDevice(MultipartFile file) {
            //生成任务id
            String taskid = assessTaskUtilsService.getTaskid();
            try {
                Workbook workbook;
                List<TaskDevice> taskDevices = new ArrayList<>();
                Map<String, List<String>> map = new HashMap<>(10);
                //获取文件名
                String fileName = file.getOriginalFilename();
                //文件名后缀
                String postfix = fileName.substring(fileName.lastIndexOf("."));
                //获取输入流
                InputStream inputStream = file.getInputStream();
                //验证文件格式
                if (".xls".equalsIgnoreCase(postfix)) {
                    workbook = new HSSFWorkbook(inputStream);
                } else if (".xlsx".equalsIgnoreCase(postfix)) {
                    workbook = new XSSFWorkbook(inputStream);
                } else {
                    return null;
                }
                //获取第一页表格数据
                Sheet sheet = workbook.getSheetAt(0);
                int first = sheet.getFirstRowNum();
                int last = sheet.getLastRowNum();
                Date date = new Date();
                //遍历每一行数据
                for (int i = first + 1; i <= last; i++) {
                    List<String> contentList = new ArrayList<>();
                    //获取行数据
                    Row row = sheet.getRow(i);
                    //去除空行
                    if (null == row) {
                        continue;
                    }
                    int firstCellNum = 0;
                    int lastCellNum = 5;
                    for (int j = firstCellNum; j < lastCellNum; j++) {
                        //验证每行的列数据
                        if (j < 3) {
                            if (null == sheet.getRow(i).getCell(j) || "".equals(sheet.getRow(i).getCell(j).toString())) {
                                //存在未空或者null,清空contentList数据
                                contentList.clear();
                                break;
                            }
                        }
                        //添加数据
                        if (null != sheet.getRow(i).getCell(j)) {
                            contentList.add(sheet.getRow(i).getCell(j).toString());
                        } else {
                            contentList.add("");
                        }
                    }
                    if (CollectionUtils.isNotEmpty(contentList)) {
                        map.put("deviceInfo" + i, contentList);
                    }
                }
                for (Map.Entry<String, List<String>> entry : map.entrySet()) {
                    List<String> list = entry.getValue();
                    TaskDevice taskDevice = new TaskDevice();
                    taskDevice.setDeviceid(list.get(0));
                    taskDevice.setDevicename(list.get(1));
                    taskDevice.setCivilcode(list.get(2));
                    taskDevice.setParentid(list.get(3));
                    taskDevice.setPkeep(list.get(4));
                    taskDevice.setLabel(taskid);
                    taskDevice.setCtime(date);
                    taskDevice.setTaskno("");
                    taskDevice.setIssend(0);
                    taskDevices.add(taskDevice);
                }
                detectionDao.insertTaskDevices(taskDevices);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return taskid;
        }
    

      

  • 相关阅读:
    九宫幻方
    K倍区间
    Excel地址
    2的n次幂
    最小乘积(基本型)
    基础练习 十六进制转八进
    java分析工具系列3:jstat (用于收集虚拟机个方面的运行数据)
    java分析工具系列2:jps(显示指定系统内所有的虚拟机进程)
    java分析工具系列1:入门
    oracle系列5:权限管理
  • 原文地址:https://www.cnblogs.com/zhaoatian/p/13906451.html
Copyright © 2011-2022 走看看