[总结][Excel导入Oval验证][Java][Oval]

来源:互联网 发布:电脑淘宝怎么看淘友 编辑:程序博客网 时间:2024/06/13 04:16

目录

  • 总结Excel导入Oval验证JavaOval
    • Maven引用pomxml
    • 工具类Java代码
    • 如何调用


[总结][Excel导入Oval验证][Java][Oval]


Maven引用[pom.xml]

<dependency>    <groupId>net.sf.oval</groupId>    <artifactId>oval</artifactId>    <version>1.85</version>    <scope>compile</scope></dependency>

工具类Java代码

/** 文件读取 **/public <T> List<T> templateToImpVoList(    MultipartFile file,    Class<T> clz,    List<String> fieldList) throws Exception {    return ExcelExecutorEx.readExcelContentEx(file, clz, fieldList);}/** 检查ImpVo  行号 对象 控制标志**/ protected List<ImpExcelErrorVo> checkImpVo(Integer row, Object obj, String[] profiles) {    Validator validator = new Validator();    List<ConstraintViolation> errors = validator.validate(obj, profiles);    List<ImpExcelErrorVo> result = new ArrayList<ImpExcelErrorVo>();    for (ConstraintViolation each : errors) {        result.add(            new ImpExcelErrorVo(                row,                Integer.parseInt(each.getErrorCode()),                each.getMessage()           )        );    }    return result;}/** 错误导入文档标记 文件 错误列表 类型标记(分功能存储) **/public String makeImpErrorFile(    MultipartFile file,    List<ImpExcelErrorVo> errorList,    String type) throws IOException {    Workbook wb = ExcelExecutorEx.markErrorIntoWorkBook(        ExcelExecutorEx.getWorkBookFromFile(file),        errorList    ); // 修改后Excel    Date now = new Date();    String fileName; // 保存名    if (wb instanceof XSSFWorkbook) {        fileName = now.getTime() + ".xlsx";    } else {        fileName = now.getTime() + ".xls";    }    String filePath = type + "/" + CommonUtil.dateFormat(now, "yyyyMMdd") + "/"; // 相对存储路径    String dirStr = Consts.UPLOAD_PATH + "/" + filePath; // 绝对存储路径    File cacheDir = new File(dirStr);// 设置目录参数    cacheDir.mkdirs();// 新建目录    File cacheFile = new File(cacheDir, fileName);// 设置参数    cacheFile.createNewFile();// 新建文件    FileOutputStream fos = new FileOutputStream(dirStr + fileName); // 文件流    wb.write(fos);// 写入文件    fos.close();// 关闭文件    return filePath + fileName; // 返回下载路径}// 检查字典是否存在public static boolean checkKeyExist(String group, String key) {    String code = group + GROUP_KEY_SPPLIT_CODE + key + MAP_SPLIT_CODE + "cn";    String localeText = properties.get(code);    return localeText != null;}

如何调用

/** * MethodName: batchImport </br> * Description: 批量导入 * @param file 文件 * @param cellNameList 列名 * @param varNameList 值对象名 * @param realPath 真实路径 * @return 返回结果 */public Map<String, Object> batchImport(    MultipartFile file,     List<String> cellNameList,    List<String> varNameList,    String realPath) {// 前略    // 导入对象    List<EnterpriseShowVoImp> voImpList =         super.templateToImpVoList(file, EnterpriseShowVoImp.class, varNameList);    // 文档空验证    if (voImpList.size() == 0) {        result.put("error", "message");        result.put("message", "导入内容为空");        return result;    }    List<ImpExcelErrorVo> errorList = new ArrayList<ImpExcelErrorVo>();    // 检查对象    Integer index = 1; // 行号    for (EnterpriseShowVoImp each : voImpList) {        // 自行扩展检查其他如数据库是否存在相同数据等        List<ImpExcelErrorVo> eachList = checkImpVo(index, each, null);        if (eachList.size() > 0) {            errorList.addAll(eachList);        }        index++;    }    // 导入对象有错    if (errorList.size() > 0) {        // 导入数据有错        result.put("error", "filepath");        result.put("filepath", super.makeImpErrorFile(file, errorList, "enterprise"));        return result;    }    // 整体插入// 后略}