diff --git a/pom.xml b/pom.xml index 4c7f134b7bcdb14d4a0406e76bcdf5be25784bdc..e484f5f46457c7e20c1e4abfdb7204b0f32a0208 100644 --- a/pom.xml +++ b/pom.xml @@ -108,6 +108,22 @@ thymeleaf-extras-java8time + + cn.afterturn + easypoi-base + 4.1.0 + + + cn.afterturn + easypoi-web + 4.1.0 + + + cn.afterturn + easypoi-annotation + 4.1.0 + + diff --git a/src/main/java/com/sixteam/irs/controller/EnterpriseController.java b/src/main/java/com/sixteam/irs/controller/EnterpriseController.java index 57f2e0487b2e75e10f0903ab53908c07e6fcdbbe..deab79831e0b8a9d9f44fd1e621804a2820dfa73 100644 --- a/src/main/java/com/sixteam/irs/controller/EnterpriseController.java +++ b/src/main/java/com/sixteam/irs/controller/EnterpriseController.java @@ -1,15 +1,24 @@ package com.sixteam.irs.controller; +import cn.afterturn.easypoi.excel.ExcelExportUtil; +import cn.afterturn.easypoi.excel.ExcelImportUtil; +import cn.afterturn.easypoi.excel.entity.ExportParams; +import cn.afterturn.easypoi.excel.entity.ImportParams; import com.sixteam.irs.entity.Enterprise; -import com.sixteam.irs.entity.R; import com.sixteam.irs.entity.User; import com.sixteam.irs.service.EnterpriseService; +import org.apache.poi.ss.usermodel.Workbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.net.URLEncoder; import java.util.List; @RestController @@ -21,32 +30,35 @@ public class EnterpriseController { private EnterpriseService enterpriseService; //查询所有企业 - @RequestMapping("/getAllEnterprise") @ResponseBody - public List getAllEnterprise(HttpServletRequest request){ - List enterpriseList = enterpriseService.findAllEnterprise(); - return enterpriseList; + @RequestMapping("/getAllEnterprise") + public List getAllEnterprise(HttpServletRequest request) { + List getAllEnterprise = enterpriseService.findAllEnterprise(); + + return getAllEnterprise; } @RequestMapping("/toAddEnterprise") - public String toAddEnterprise(){ + public String toAddEnterprise() { return "/test/enterprise/添加企业页面.html"; } @RequestMapping("/addEnterprise") - @ResponseBody - public R addEnterprise(Enterprise enterprise, HttpServletRequest request){ - System.out.println(enterpriseService.addEnterprise(enterprise)); - return enterpriseService.addEnterprise(enterprise); - + public String addEnterprise(Enterprise enterprise, HttpServletRequest request) { + if (enterpriseService.addEnterprise(enterprise) > 0) { + return "redirect:/enterprise/getAllEnterprise"; + } else { + request.setAttribute("errorMessage", "删除错误"); + return "/test/错误页面"; + } } @RequestMapping(value = "/delEnterprise") public String delEnterprise(Enterprise enterprise, Model model) { - if (enterpriseService.delEnterprise(enterprise)>0) { + if (enterpriseService.delEnterprise(enterprise) > 0) { return "redirect:/enterprise/getAllEnterprise"; } else { @@ -68,7 +80,7 @@ public class EnterpriseController { @RequestMapping(value = "/updateEnterprise") public String updateUser(Enterprise enterprise, Model model) { - if (enterpriseService.updateEnterprise(enterprise)>0) { + if (enterpriseService.updateEnterprise(enterprise) > 0) { return "redirect:/enterprise/getAllEnterprise"; } else { @@ -78,4 +90,44 @@ public class EnterpriseController { } } + /** + * 导出excel + * + * @param response + * @throws IOException + */ + @RequestMapping("/export") + public void ExportExel(HttpServletResponse response) throws IOException{ + List enterprises = enterpriseService.findAllEnterprise(); + Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("企业列表信息","企业信息"),Enterprise.class,enterprises); + response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("企业列表.xls", "UTF-8")); + ServletOutputStream outputStream = response.getOutputStream(); + workbook.write(outputStream); + outputStream.close(); + workbook.close(); + } + + /** + * excel导入 + * + * @param excelFile + * @return + * @throws Exception + */ + @RequestMapping("/import") + public String importExcel(MultipartFile excelFile) throws Exception { +// log.info("文件名:"+excelFile.getOriginalFilename()); + //excel导入 + ImportParams params = new ImportParams(); + params.setTitleRows(1); + params.setHeadRows(1); + + List enterprises = ExcelImportUtil.importExcel(excelFile.getInputStream(), Enterprise.class, params); +// for (User user : users) { +// System.out.println(user); +// } + enterpriseService.savaAll(enterprises); + return "redirect:/enterprise/getAllEnterprise"; + } + } diff --git a/src/main/java/com/sixteam/irs/entity/Enterprise.java b/src/main/java/com/sixteam/irs/entity/Enterprise.java index 9fb25827b1bb957d9a65d3efceecb86c5b498cb8..74fa4b7ff5b7b0d959d6a54fa8e8acdda97be395 100644 --- a/src/main/java/com/sixteam/irs/entity/Enterprise.java +++ b/src/main/java/com/sixteam/irs/entity/Enterprise.java @@ -1,15 +1,27 @@ package com.sixteam.irs.entity; +import cn.afterturn.easypoi.excel.annotation.Excel; +import cn.afterturn.easypoi.excel.annotation.ExcelTarget; + import java.util.List; +@ExcelTarget("enterprises") public class Enterprise { + @Excel(name = "编号") private String eId;//企业id + @Excel(name = "企业名称") private String eName;//企业名称 + @Excel(name = "企业介绍") private String eIntroduce;//企业介绍 + @Excel(name = "企业成立时间") private String eStart;//企业成立时间 + @Excel(name = "企业类型") private String eType;//企业类型,定位 + @Excel(name = "企业创始人") private String eFounder;//企业创始人 + @Excel(name = "企业地点") private String eAddress;//企业地点 + @Excel(name = "企业状态") private String eState;//企业申请认证 已通过/未通过 diff --git a/src/main/java/com/sixteam/irs/service/EnterpriseService.java b/src/main/java/com/sixteam/irs/service/EnterpriseService.java index a629a2456c377982fef90cd13cd3b773bfa2df54..b910e14a64a1cb7f11b341a3c12635c4dbad715a 100644 --- a/src/main/java/com/sixteam/irs/service/EnterpriseService.java +++ b/src/main/java/com/sixteam/irs/service/EnterpriseService.java @@ -7,10 +7,11 @@ import java.util.List; public interface EnterpriseService { - List findAllEnterprise();//查询所有企业 int addEnterprise(Enterprise enterprise);//添加企业 + List findAllEnterprise(); + List findEnterpriseByName(Enterprise enterprise); Enterprise findById(Enterprise enterprise); @@ -18,4 +19,6 @@ public interface EnterpriseService { int delEnterprise(Enterprise enterprise); int updateEnterprise(Enterprise enterprise); + + void savaAll(List enterprises); } diff --git a/src/main/java/com/sixteam/irs/service/Impl/EnterpriceServiceImpl.java b/src/main/java/com/sixteam/irs/service/Impl/EnterpriceServiceImpl.java index 76d00aac8cfbc68a7d45d95301b85748d2b356d4..022dc25384f77aa859aaa3853d6352193f2fd8b7 100644 --- a/src/main/java/com/sixteam/irs/service/Impl/EnterpriceServiceImpl.java +++ b/src/main/java/com/sixteam/irs/service/Impl/EnterpriceServiceImpl.java @@ -34,15 +34,9 @@ public class EnterpriceServiceImpl implements EnterpriseService { } @Override - public R addEnterprise(Enterprise enterprise) { - - Enterprise enterprise1 = new Enterprise(); - enterprise1.seteId(UuidUtils.uuid()); - BeanUtils.copyProperties(enterprise1, enterprise); - if (enterpriseMapper.addEnterprise(enterprise1) > 0) { - return R.success("添加记录成功"); - } - return R.error("添加时,发生异常"); + public int addEnterprise(Enterprise enterprise) { + enterprise.seteId(UuidUtils.uuid()); + return enterpriseMapper.addEnterprise(enterprise); } @Override @@ -54,4 +48,12 @@ public class EnterpriceServiceImpl implements EnterpriseService { public int updateEnterprise(Enterprise enterprise) { return enterpriseMapper.updateEnterprise(enterprise); } + + @Override + public void savaAll(List enterprises) { + for (Enterprise enterprise : enterprises){ + enterprise.seteId(UuidUtils.uuid()); + enterpriseMapper.addEnterprise(enterprise); + } + } } diff --git a/src/main/resources/com/sixteam/irs/mapper/EnterpriseMapper.xml b/src/main/resources/com/sixteam/irs/mapper/EnterpriseMapper.xml index 1805287e64057777ac26662842988abf1ad9c362..fcd13b92583fc8239b12095fc6ff0aba755e825d 100644 --- a/src/main/resources/com/sixteam/irs/mapper/EnterpriseMapper.xml +++ b/src/main/resources/com/sixteam/irs/mapper/EnterpriseMapper.xml @@ -47,4 +47,6 @@ where e_id = #{eId} + + \ No newline at end of file