diff --git a/src/main/java/com/example/meetingroom/config/GlobalCorsConfig.java b/src/main/java/com/example/meetingroom/config/GlobalCorsConfig.java index ac0f8cbe90ed6a1e4fda9b04f8192a3541047a27..a7422e1fc6e144f86548d23809c610b05c1f3e52 100644 --- a/src/main/java/com/example/meetingroom/config/GlobalCorsConfig.java +++ b/src/main/java/com/example/meetingroom/config/GlobalCorsConfig.java @@ -22,7 +22,7 @@ public class GlobalCorsConfig { public CorsFilter corsFilter() { CorsConfiguration corsConfig = new CorsConfiguration(); //设置Origin(原始域) - corsConfig.addAllowedOrigin("*"); + corsConfig.addAllowedOriginPattern("*"); //设置是否发送cookie corsConfig.setAllowCredentials(true); //放行哪些原始域(头部信息) diff --git a/src/main/java/com/example/meetingroom/controller/ExcelController.java b/src/main/java/com/example/meetingroom/controller/ExcelController.java index df30963562c3276e9e3dcb2d3be87bc5446e13cc..78590154b6eca50e319e0e2b2a45589fb5f1e8f2 100644 --- a/src/main/java/com/example/meetingroom/controller/ExcelController.java +++ b/src/main/java/com/example/meetingroom/controller/ExcelController.java @@ -5,6 +5,7 @@ import com.example.meetingroom.service.ExcelService; import com.example.meetingroom.service.RoleService; import com.example.meetingroom.service.RoomOrderService; import com.example.meetingroom.vo.ResultVo; +import org.apache.http.HttpResponse; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; @@ -13,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletResponse; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; @@ -119,41 +121,59 @@ public class ExcelController { * @author: Chen si ran */ @GetMapping("/employeeFile") - public ResultVo exportEmployeeExcel(){ + public ResultVo exportEmployeeExcel(HttpServletResponse response){ + + Integer role = 0; + /*try { + // 根据身份得到授权(0为普管,1为超管) + role = roleService.superOrCommon(); + }catch (NullPointerException e){ + e.printStackTrace(); + return ResultVo.error(500,"导出失败,授权失败,您不是管理员!",null); + }*/ - //0为普管,1为超管 - int role = 0; String[] headers = {}; List list = null; - //根据权限选择标题行 + // 根据权限选择标题行 if (role == 0){ - headers = new String[]{"id", "员工编号", "姓名", "部门", "权限", "联系方式", "性别(0为使用,1为不使用)"}; + headers = new String[]{"id", "员工编号", "姓名", "部门", "权限", "联系方式", "性别"}; }else { - headers = new String[]{"id","员工编号","姓名","部门","权限","联系方式","密码","性别"}; + // 超管比普管多展示一个密码 + headers = new String[]{"id", "员工编号", "姓名", "部门", "权限", "联系方式", "密码", "性别"}; } - //获取员工信息表数据 - list = employeeInfoService.selectAdmin(); + // 获取员工信息表数据 + list = employeeInfoService.selectAdmin(role); + + String fileName = "测试员工信息表.xlsx"; OutputStream out = null; try { - out = new FileOutputStream("E://测试员工信息表.xls"); - excelService.export("测试员工信息表",headers,list,out,role); - System.out.println("成功导出!!"); + // 设置Http响应头告诉浏览器下载这个附件 + response.setHeader("Content-Disposition", "attachment;Filename=" + fileName); + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"); + out = response.getOutputStream(); + excelService.export("测试员工信息表", headers, list, out, "EmployeeInfo", role); + System.out.println("导出成功!"); + } catch (FileNotFoundException e){ + e.printStackTrace(); + return ResultVo.error(500,"导出失败,请查看并处理异常:" + e.getMessage(),null); } catch (IOException e) { e.printStackTrace(); - return ResultVo.error(500,e.getMessage(),null); + return ResultVo.error(500,"导出失败,请重新尝试!",null); }finally { + // 关闭流 if (out != null){ try { + out.flush(); out.close(); } catch (IOException e) { + System.out.println("==关闭导出文件流=="); e.printStackTrace(); } } } - - return ResultVo.ok(200,"成功导出!",null); + return ResultVo.ok(200,"导出成功!",null); } /** @@ -172,7 +192,7 @@ public class ExcelController { OutputStream out = null; try { out = new FileOutputStream("E://会议室预订记录表.xls"); - excelService.export("会议室预订记录表",headers,list,out,2); + excelService.export("会议室预订记录表",headers,list,out,"RoomOrder",2); System.out.println("成功导出!"); } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/com/example/meetingroom/controller/RoomOrderController.java b/src/main/java/com/example/meetingroom/controller/RoomOrderController.java index 36962a56b23033c4dce5458798b84ce4411b2607..555119f5b8f5efc0e505ceee6c7a4748ab408417 100644 --- a/src/main/java/com/example/meetingroom/controller/RoomOrderController.java +++ b/src/main/java/com/example/meetingroom/controller/RoomOrderController.java @@ -228,7 +228,7 @@ public class RoomOrderController { * @author feng * @date: 2021/9/23 19:57 */ - @PostMapping("/orders/detail/{orderId}") + @GetMapping("/orders/detail/{orderId}") public ResultVo getDetail(@PathVariable String orderId){ RoomOrderDetailsVo result = roomOrderService.selectOrderDetail(orderId); if(result == null){ diff --git a/src/main/java/com/example/meetingroom/dao/EmployeeInfoDao.java b/src/main/java/com/example/meetingroom/dao/EmployeeInfoDao.java index 3703af78230dfeb6d499ab03b8d8f130c51c454d..b81ee66417900856c6ed4818d97f6d57fe9efc41 100644 --- a/src/main/java/com/example/meetingroom/dao/EmployeeInfoDao.java +++ b/src/main/java/com/example/meetingroom/dao/EmployeeInfoDao.java @@ -62,23 +62,23 @@ public interface EmployeeInfoDao { String selectEmployeeInfoName(String id); /** - * @param - * @return employeeInfoVo对象集合 * @function: 联表查询返回的数据 + * @param role 角色授权,0为普通管理员,1为超级管理员 + * @return employeeInfoVo对象集合 */ - List selectAdmin(); + List selectAdmin(@Param("role") Integer role); /** + * @function: 批量插入多条数据 * @param lists employee对象集合 * @return 成功插入的数量 - * @function: 批量插入多条数据 */ int insertBatch(List lists); /** + * @function: 用于管理员新增员工时判断电话号码是否重复 * @param phone * @return - * 用于管理员新增员工时判断电话号码是否重复 */ String judgePhone(String phone); diff --git a/src/main/java/com/example/meetingroom/service/EmployeeInfoService.java b/src/main/java/com/example/meetingroom/service/EmployeeInfoService.java index cdaa3a577b5b6ed00a7ec18a994602a18fad466b..fae991b06c8fc85fb4cd48061d9e081ccfc758bc 100644 --- a/src/main/java/com/example/meetingroom/service/EmployeeInfoService.java +++ b/src/main/java/com/example/meetingroom/service/EmployeeInfoService.java @@ -31,9 +31,10 @@ public interface EmployeeInfoService { /** * @function: 联表查询返回的数据 + * @param role 角色授权,0为普通管理员,1为超级管理员 * @return employeeInfoVo对象集合 */ - List selectAdmin(); + List selectAdmin(Integer role); /** * @param employeeInfo diff --git a/src/main/java/com/example/meetingroom/service/ExcelService.java b/src/main/java/com/example/meetingroom/service/ExcelService.java index 9e966534572170f4ed9bc47b977275e7cc5f1af0..5e45369a542d418f93c994e5b7feb9b38c5cca80 100644 --- a/src/main/java/com/example/meetingroom/service/ExcelService.java +++ b/src/main/java/com/example/meetingroom/service/ExcelService.java @@ -40,9 +40,10 @@ public interface ExcelService { * @param headers 标题列表 * @param dataset 要导出的数据集合 * @param out 与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中 - * @param role 该操作是普管(0)执行的还是超管(1)执行的 + * @param role 根据身份得到授权(0为普管,1为超管) + * @param starter 发起者,判断导出的数据是员工信息还是会议室预订记录 * @return */ - void export(String title, String[] headers, List dataset, OutputStream out, int role) throws IOException; + void export(String title, String[] headers, List dataset, OutputStream out, String starter, int role) throws IOException; } diff --git a/src/main/java/com/example/meetingroom/service/impl/EmployeeInfoServiceImpl.java b/src/main/java/com/example/meetingroom/service/impl/EmployeeInfoServiceImpl.java index c44185726c5f095aff4e04d35d523c5aa4218da1..64cfbd62cad40a48d58cdb896c4e36e0f7a1d032 100644 --- a/src/main/java/com/example/meetingroom/service/impl/EmployeeInfoServiceImpl.java +++ b/src/main/java/com/example/meetingroom/service/impl/EmployeeInfoServiceImpl.java @@ -66,12 +66,12 @@ public class EmployeeInfoServiceImpl implements EmployeeInfoService { /** * @function: 联表查询返回给普通管理员的数据 - * @param + * @param role 角色授权,0为普通管理员,1为超级管理员 * @return employeeInfoVo对象集合 */ @Override - public List selectAdmin() { - return employeeInfoDao.selectAdmin(); + public List selectAdmin(Integer role) { + return employeeInfoDao.selectAdmin(role); } /** diff --git a/src/main/java/com/example/meetingroom/service/impl/ExcelServiceImpl.java b/src/main/java/com/example/meetingroom/service/impl/ExcelServiceImpl.java index 2dab6c28b7d003b93b47875d814a4b4451325ab5..076f66ca3104db0cad59ed0abba6fe649a6df45f 100644 --- a/src/main/java/com/example/meetingroom/service/impl/ExcelServiceImpl.java +++ b/src/main/java/com/example/meetingroom/service/impl/ExcelServiceImpl.java @@ -1,17 +1,12 @@ package com.example.meetingroom.service.impl; -import com.example.meetingroom.service.EmployeeInfoService; import com.example.meetingroom.service.ExcelService; import com.example.meetingroom.utils.DateUtil; import com.example.meetingroom.vo.EmployeeInfoSuperVo; -import com.example.meetingroom.vo.EmployeeInfoVo; import com.example.meetingroom.vo.RoomOrderListVo; -import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.ss.usermodel.*; -import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.IOException; @@ -144,142 +139,53 @@ public class ExcelServiceImpl implements ExcelService { return cellValue; } - /** - * @function: 使用Excel表格导出数据 - * @param title Excel表名 - * @param headers 标题列表 - * @param dataset 要导出的数据集合 - * @param out 与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中 - * @return - */ - /*@Override - public void export(String title, String[] headers, List dataset, OutputStream out){ - - //创建一个工作簿 - Workbook workbook = new XSSFWorkbook(); - - //创建一个表 - Sheet sheet = workbook.createSheet(title); - - //创建标题行并赋值 - Row row = sheet.createRow(0); - - //根据标题列表长度创建标题行并赋值 - for (int column = 0; column < headers.length; column++) { - Cell cell = row.createCell(column); - cell.setCellValue(headers[column]); - } - - *//*for (int i = 0; i < dataset.size(); i++) { - row = sheet.createRow(i); - - - }*//* - - // 遍历集合数据,产生数据行 - *//*Iterator iterator = dataset.iterator(); - int index = 0; - while(iterator.hasNext()){ - index++; - row = sheet.createRow(index); - Object next = iterator.next(); - - //根据反射获取javabean属性,动态调用getXXX()方法得到属性值 - Field[] fields = next.getClass().getDeclaredFields(); - - //创建单元格并赋值 - for (int i = 0; i < fields.length; i++) { - - Cell cell = row.createCell(i); - - //拼接一个getXX方法 - String fieldName = fields[i].getName(); - String getMethodName = "get" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1); - - try { - //通过反射得到getter方法 - Class tCls = next.getClass(); - Method getMethod = tCls.getMethod(getMethodName, new Class[]{}); - - //获取这个值 - Object value = getMethod.invoke(next, new Object[]{}); - // 判断值的类型后进行强制类型转换 - String textValue = null; - - if (value instanceof Integer) { - int intValue = (Integer) value; - cell.setCellValue(intValue); - }else { - // 其它数据类型都当作字符串简单处理 - textValue = value.toString(); - } - - //为单元格赋值 - cell.setCellValue(textValue); - - } catch (SecurityException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } finally { - // 清理资源 - } - } - - }*//* - - //导出文档 - try { - workbook.write(out); - } catch (IOException e) { - e.printStackTrace(); - } - }*/ - /** * @function: 导出所有数据 * @param title Excel表名 * @param headers 标题列表 * @param dataset 要导出的数据集合 * @param out 与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中 - * @param role 该操作是普管(0)执行的还是超管(1)执行的 + * @param role 根据身份得到授权(0为普管,1为超管) + * @param starter 发起者,判断导出的数据是员工信息还是会议室预订记录 * @return */ @Override - public void export(String title, String[] headers, List dataset, OutputStream out, int role) throws IOException{ - //创建一个工作簿 + public void export(String title, String[] headers, List dataset, OutputStream out, String starter, int role) throws IOException{ + + // XSSF(.xlsx) Workbook workbook = new XSSFWorkbook(); - //创建一个表 Sheet sheet = workbook.createSheet(title); - //创建标题行并赋值 Row row = sheet.createRow(0); Cell cell = null; + + // 单元格格式 CellStyle cellStyle = workbook.createCellStyle(); + // 水平居中 cellStyle.setAlignment(HorizontalAlignment.CENTER); + // 垂直居中 + cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); - //根据标题列表长度创建标题行并赋值 + // 根据标题列表长度创建标题行并赋值 for (int column = 0; column < headers.length; column++) { + + // 设置列宽 sheet.setColumnWidth(column,15*256); + cell = row.createCell(column); cell.setCellValue(headers[column]); + + // 设置单元格格式 cell.setCellStyle(cellStyle); } - //使用数据集赋值 + // 使用数据集赋值除标题行以外的行 for (int rowCount = 0; rowCount < dataset.size(); rowCount++) { - //创建行 + // 创建行 row = sheet.createRow(rowCount+1); - //选择导出普管、超管的员工信息表还是会议室预订记录表 - if (role == 0){ + //选择导出 员工信息 or 会议室预订记录 + if ( "EmployeeInfo".equals(starter) ){ EmployeeInfoSuperVo employeeInfoSuperVo = (EmployeeInfoSuperVo)dataset.get(rowCount); - //为各单元格赋值(EmployeeInfoVo) cell = row.createCell(0); cell.setCellValue(employeeInfoSuperVo.getId()); cell.setCellStyle(cellStyle); @@ -301,37 +207,13 @@ public class ExcelServiceImpl implements ExcelService { cell = row.createCell(6); cell.setCellValue(employeeInfoSuperVo.getSex()); cell.setCellStyle(cellStyle); - }else if(role == 1){ - EmployeeInfoSuperVo employeeInfoSuperVo = (EmployeeInfoSuperVo)dataset.get(rowCount); - //为各单元格赋值(EmployeeInfoVo) - cell = row.createCell(0); - cell.setCellValue(employeeInfoSuperVo.getId()); - cell.setCellStyle(cellStyle); - cell = row.createCell(1); - cell.setCellValue(employeeInfoSuperVo.getNumber()); - cell.setCellStyle(cellStyle); - cell = row.createCell(2); - cell.setCellValue(employeeInfoSuperVo.getName()); - cell.setCellStyle(cellStyle); - cell = row.createCell(3); - cell.setCellValue(employeeInfoSuperVo.getDepartName()); - cell.setCellStyle(cellStyle); - cell = row.createCell(4); - cell.setCellValue(employeeInfoSuperVo.getRoleName()); - cell.setCellStyle(cellStyle); - cell = row.createCell(5); - cell.setCellValue(employeeInfoSuperVo.getPhone()); - cell.setCellStyle(cellStyle); - cell = row.createCell(6); - cell.setCellValue(employeeInfoSuperVo.getSex()); - cell.setCellStyle(cellStyle); - cell = row.createCell(7); - cell.setCellValue(employeeInfoSuperVo.getPassword()); - cell.setCellStyle(cellStyle); - }else if(role == 2){ - + if (role == 1){ + // 如果是超管,可查询并导出密码 + cell.setCellValue(employeeInfoSuperVo.getPassword()); + cell.setCellStyle(cellStyle); + } + }else if( "RoomOrder".equals(starter) ){ RoomOrderListVo roomOrderVo = (RoomOrderListVo)dataset.get(rowCount); - //为各单元格赋值 cell = row.createCell(0); cell.setCellValue(roomOrderVo.getId()); cell.setCellStyle(cellStyle); @@ -379,9 +261,7 @@ public class ExcelServiceImpl implements ExcelService { cell.setCellStyle(cellStyle); } } - //导出文档 workbook.write(out); } - -} +} \ No newline at end of file diff --git a/src/main/resources/mapper/EmployeeInfoMapper.xml b/src/main/resources/mapper/EmployeeInfoMapper.xml index d3cb1829bb1c82a4bc2dd1702e6aa6c1cd7d798a..f2597466248b03164b2ec29cb56713bdc9fb1ad5 100644 --- a/src/main/resources/mapper/EmployeeInfoMapper.xml +++ b/src/main/resources/mapper/EmployeeInfoMapper.xml @@ -84,7 +84,10 @@