admin管理员组

文章数量:1660065

1,先写一个公共类

public static String courseFile;
    public static String uploadImagePath = "/Upload/vbsqyw/image/";
    public static String uploadFilePath ="/Upload/vbsqyw/file/";
    public static String templatePath="/Template/vbsqyw/";

    /**
     * 导入excle文件,导入后先进行文件上传,再解析文件内容,并返回
     *
     * @param file
     * @return
     */
    public static HashMap<String, List> importFile(MultipartFile file) {
   
        HashMap<String, List> result = new HashMap<>();
        List headers = new ArrayList(); //表头
        List bodys = new ArrayList(); //表格行
        HashMap fileInfo = uploadFile(file); //先进行文件上传,再解析文件内容
        String fileName = fileInfo.get("fileName").toString();
        if (!fileName.equals("")) {
   
            Workbook workbook = null;
            try {
   
                workbook = getWorkbook(fileName);
            } catch (IOException e) {
   
                e.printStackTrace();
            }
            if (workbook != null || !workbook.equals("")) {
   
                for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
   
                    Sheet sheet = workbook.getSheetAt(i); //获取excle中的每一个sheet,再解析每个sheet中的内容
                    if (sheet.getLastRowNum() != 0) {
   
                        for (int r = 0; r <= sheet.getLastRowNum(); r++) {
   
                            Row row = sheet.getRow(r); //获取sheet的每一行
                            List body = new ArrayList();
                            for (int n = 0; n < row.getLastCellNum(); n++) {
   
                                HashMap bodyVal = new HashMap();
                                HashMap headerVal = new HashMap();
                                if (r == 0) {
    //如果行号为0,则是表头
                                    headerVal.put("value", row.getCell(n).getStringCellValue());
                                    headers.add(headerVal);
                                } else {
   
                                    Cell cell = row.getCell(n);
                                    String dateString = checkDate(cell);
                                    if (dateString.equals("")) {
   
                                        cell.setCellType(CellType.STRING);
                                    } else {
   
                                        cell.setCellValue(dateString);
                                    }
                                    bodyVal.put("value", cell.getStringCellValue());
                                    body.add(bodyVal);
                                }
                            }
                            if (r > 0) {
    //行号为0时不添加内容到bodys中
                                bodys.add(body);
                            }
                        }
                    }
                }
                result.put("body", bodys); //返回的内容
                result.put("header", headers); //返回的表头
            }
        }
        return result;
    }

    /**
     * 上传文件,包括图片,表格等文件,不解析文件内容
     *
     * @param file 选择的文件
     * @return
     */
    public static HashMap<String, String> uploadFile(MultipartFile file) {
   
        HashMap<String, String> result = new HashMap<>();
        String saveFilePath = "";
        if (!file.isEmpty

本文标签: 文件上传后台后端地址文件