layui导入模板数据_java+layui的Excel导入导出

该博客介绍了一种使用Java后端配合layui前端框架实现Excel数据的导入和导出的方法。在导入过程中,首先通过`ExcelUtil.importExcel`方法解析上传的Excel文件,然后检查数据的完整性和唯一性,将有效数据保存到数据库。在导出部分,利用`ExcelUtil.exportExcel`生成Excel文件并提供下载。此外,还提供了下载模板功能供用户参照。
摘要由CSDN通过智能技术生成

//导入

@PostMapping(value = "importData")

ResultJson importData(@RequestParam MultipartFile file) {

ResultJson resultJson= newResultJson();

List importData = null;try{

importData= ExcelUtil.importExcel(file.getInputStream(), ProjectJson.class);

}catch(IOException e) {

e.printStackTrace();

}if (null ==importData) {

resultJson.setStatus(1);

resultJson.setMessage("导入失败!");returnresultJson;

}int num = 0;for(ProjectJson projectJson : importData) {

ProjectManageEntity projectManageEntity= newProjectManageEntity();

num++;if(CommonUtil.isEmpty(projectJson.getNumber())) {

resultJson.setMessage("导入失败!第" + num + "行的编号不能为空");

resultJson.setStatus(1);returnresultJson;

}if (projectService.findByprojectNumber(projectJson.getNumber()) != null) {

resultJson.setStatus(1);

resultJson.setMessage("导入失败!第" + num + "行的编号重复");

resultJson.setData(null);returnresultJson;

}

projectManageEntity.setCreateDate(newDate());

projectManageEntity.setUpdateDate(newDate());

projectManageEntity.setName(projectJson.getName());

projectManageEntity.setNumber(projectJson.getNumber());

projectManageEntity.setType(projectJson.getType());

projectManageEntity.setDeleteMark(false);

projectManageEntity.setCode("PROJECT_MANAGE" +UUID.randomUUID());

projectService.save(projectManageEntity);

}

resultJson.setStatus(0);

resultJson.setMessage("导入成功!");

resultJson.setData(null);returnresultJson;

}//导出

@PostMapping(value = "exportData")

ResultJson exportData() {

String excelTitle= "项目管理";

String path= "/export/company/excel/" + DateUtil.getEightDateFormat().format(newDate());

String realPath= CommonUtil.createFolderPath(propertyUtil.getUploadPath() +path);return ExcelUtil.exportExcel(excelTitle, realPath, path, ProjectJson.class, projectService.getAll());

}//下载模板

@PostMapping(value = "downloadTemplate")publicResultJson downloadTemplate() {

String excelTitle= "项目管理模板";

String path= "/export/company/excel/" + DateUtil.getEightDateFormat().format(newDate());

String realPath= CommonUtil.createFolderPath(propertyUtil.getUploadPath() +path);return ExcelUtil.exportExcel(excelTitle, realPath, path, ProjectJson.class, projectService.getDownloadTemplate());

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值