| | |
| | | import io.swagger.annotations.ApiOperation; |
| | | import io.swagger.annotations.ApiParam; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| | | import org.apache.poi.ss.usermodel.Sheet; |
| | | import org.apache.poi.ss.usermodel.Row; |
| | | import org.apache.poi.ss.usermodel.Cell; |
| | | import org.apache.poi.ss.usermodel.CellStyle; |
| | | import org.apache.poi.ss.usermodel.Font; |
| | | import org.apache.poi.ss.usermodel.HorizontalAlignment; |
| | | import org.apache.poi.ss.usermodel.VerticalAlignment; |
| | | import org.apache.poi.ss.usermodel.BorderStyle; |
| | | import org.apache.poi.ss.util.CellRangeAddress; |
| | | import org.apache.poi.ss.util.RegionUtil; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.transaction.annotation.Propagation; |
| | |
| | | @ApiOperation(value = "员工导入模板下载") |
| | | @GetMapping("/importDownload") |
| | | public void importDownload() { |
| | | List<EmployeeImportExcel> employeeImportExcels = new ArrayList<>(); |
| | | EmployeeImportExcel employeeImportExcel = new EmployeeImportExcel(); |
| | | employeeImportExcels.add(employeeImportExcel); |
| | | ExportParams exportParams = new ExportParams(); |
| | | exportParams.setTitle("员工信息导入模板"); |
| | | exportParams.setSheetName("员工信息"); |
| | | Workbook workbook = ExcelExportUtil.exportExcel(exportParams, EmployeeImportExcel.class, employeeImportExcels); |
| | | // 自定义表头,构建两行表头并合并"学历"大标题 |
| | | Workbook workbook = new HSSFWorkbook(); |
| | | Sheet sheet = workbook.createSheet("员工信息"); |
| | | |
| | | // 列宽适配 |
| | | int col = 0; |
| | | String[] firstRowHeads = new String[]{ |
| | | "序号","姓名","性别","身份证号码","年龄","政治面貌","参工时间","进入公司时间", |
| | | "学历","所在股室","职务","任职时间","电话号码","编制" |
| | | }; |
| | | String[] secondRowHeads = new String[]{ |
| | | "","","","","","","","", |
| | | "第一","学院","专业","最高","学院","专业", |
| | | "","","","","","" |
| | | }; |
| | | |
| | | // 创建样式 |
| | | CellStyle headStyle = workbook.createCellStyle(); |
| | | headStyle.setAlignment(HorizontalAlignment.CENTER); |
| | | headStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | headStyle.setBorderBottom(BorderStyle.THIN); |
| | | headStyle.setBorderTop(BorderStyle.THIN); |
| | | headStyle.setBorderLeft(BorderStyle.THIN); |
| | | headStyle.setBorderRight(BorderStyle.THIN); |
| | | Font font = workbook.createFont(); |
| | | font.setBold(true); |
| | | headStyle.setFont(font); |
| | | |
| | | Row row1 = sheet.createRow(0); |
| | | Row row2 = sheet.createRow(1); |
| | | |
| | | // 先填充第一行 |
| | | for (int i = 0; i < firstRowHeads.length; i++) { |
| | | Cell cell = row1.createCell(i); |
| | | cell.setCellValue(firstRowHeads[i]); |
| | | cell.setCellStyle(headStyle); |
| | | sheet.setColumnWidth(i, 16 * 256); |
| | | } |
| | | // 第二行 |
| | | for (int i = 0; i < secondRowHeads.length; i++) { |
| | | Cell cell = row2.createCell(i); |
| | | cell.setCellValue(secondRowHeads[i]); |
| | | cell.setCellStyle(headStyle); |
| | | } |
| | | |
| | | // 合并除"学历"外的单列表头(两行合并): 0-7 列,以及 9-13 列 |
| | | int[][] singleMerge = new int[][]{ |
| | | {0,0},{1,1},{2,2},{3,3},{4,4},{5,5},{6,6},{7,7}, |
| | | {9,9},{10,10},{11,11},{12,12},{13,13} |
| | | }; |
| | | for (int[] m : singleMerge) { |
| | | CellRangeAddress region = new CellRangeAddress(0,1,m[0],m[1]); |
| | | sheet.addMergedRegion(region); |
| | | RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet); |
| | | RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet); |
| | | RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet); |
| | | RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet); |
| | | } |
| | | |
| | | // 合并"学历"大标题区域:第8列到第13列(共6列)第一行 |
| | | CellRangeAddress xlMerge = new CellRangeAddress(0,0,8,13); |
| | | sheet.addMergedRegion(xlMerge); |
| | | RegionUtil.setBorderBottom(BorderStyle.THIN, xlMerge, sheet); |
| | | RegionUtil.setBorderTop(BorderStyle.THIN, xlMerge, sheet); |
| | | RegionUtil.setBorderLeft(BorderStyle.THIN, xlMerge, sheet); |
| | | RegionUtil.setBorderRight(BorderStyle.THIN, xlMerge, sheet); |
| | | |
| | | HttpServletResponse response = WebUtils.response(); |
| | | ServletOutputStream outputStream = null; |
| | | try { |