/** * 下载模板 * @param request * @param response * @param model */ @SuppressWarnings("deprecation") @RequestMapping(value = "import/template") public void importTemplate(HttpServletRequest request, HttpServletResponse response, Model model) { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet=workbook.createSheet(); sheet.setColumnWidth(0, 6500); sheet.setColumnWidth(1, 4500); sheet.setColumnWidth(2, 6500); sheet.setColumnWidth(3, 4500); sheet.setColumnWidth(4, 12000); // 设置字体 HSSFFont headfont = workbook.createFont(); headfont.setFontName("黑体"); headfont.setFontHeightInPoints((short) 14);// 字体大小 headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗 // 另一个样式 HSSFCellStyle headstyle = workbook.createCellStyle(); headstyle.setFont(headfont); headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中 headstyle.setLocked(true); headstyle.setWrapText(true);// 自动换行 // 另一个字体样式 HSSFFont columnHeadFont = workbook.createFont(); columnHeadFont.setFontName("宋体"); columnHeadFont.setFontHeightInPoints((short) 10); columnHeadFont.setColor(HSSFColor.WHITE.index); columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 列头的样式 HSSFCellStyle columnHeadStyle = workbook.createCellStyle(); // 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式) columnHeadStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); columnHeadStyle.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index); columnHeadStyle.setFont(columnHeadFont); columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中 columnHeadStyle.setLocked(true); columnHeadStyle.setWrapText(true); columnHeadStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); columnHeadStyle.setTopBorderColor(HSSFColor.BLACK.index); columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色 columnHeadStyle.setBorderLeft((short) 1);// 边框的大小 columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色 columnHeadStyle.setBorderRight((short) 1);// 边框的大小 columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体 columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色 HSSFFont font = workbook.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 10); // 普通单元格样式 HSSFCellStyle style = workbook.createCellStyle(); style.setFont(font); style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中 style.setWrapText(true); style.setLeftBorderColor(HSSFColor.BLACK.index); style.setBorderLeft((short) 1); style.setRightBorderColor(HSSFColor.BLACK.index); style.setBorderRight((short) 1); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体 style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色. style.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色. HSSFRow row1 = sheet.createRow(0); // 设置行高 row1.setHeight((short) 450); // 创建第一列 HSSFCell cell0 = row1.createCell(0); cell0.setCellValue(new HSSFRichTextString("教师科目关联表")); cell0.setCellStyle(headstyle); CellRangeAddress range = new CellRangeAddress(0, 0, 0, 4); sheet.addMergedRegion(range); // 第1行 HSSFRow row2 = sheet.createRow(1); row2.setHeight((short) 300); HSSFCell cell = row2.createCell(0); cell.setCellValue(new HSSFRichTextString("学校")); cell.setCellStyle(columnHeadStyle); cell = row2.createCell(1); cell.setCellValue(new HSSFRichTextString("教师姓名")); cell.setCellStyle(columnHeadStyle); cell = row2.createCell(2); cell.setCellValue(new HSSFRichTextString("教师工号")); cell.setCellStyle(columnHeadStyle); cell = row2.createCell(3); cell.setCellValue(new HSSFRichTextString("科目")); cell.setCellStyle(columnHeadStyle); cell = row2.createCell(4); cell.setCellValue(new HSSFRichTextString("备注")); cell.setCellStyle(columnHeadStyle); // 第2行 HSSFRow row3 = sheet.createRow(2); row3.setHeight((short) 300); cell = row3.createCell(0); cell.setCellValue("A学校"); cell.setCellStyle(style); cell = row3.createCell(1); cell.setCellValue("张三"); cell.setCellStyle(style); cell = row3.createCell(2); cell.setCellValue("45566566695551125"); cell.setCellStyle(style); cell = row3.createCell(3); cell.setCellValue("语文"); cell.setCellStyle(style); cell = row3.createCell(4); cell.setCellValue("张三语文老师"); cell.setCellStyle(style); try { String fileName = "教师科目关联导入模板.xls"; OutputStream output=response.getOutputStream(); response.reset(); response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName).getBytes(), "iso-8859-1")); response.setContentType("application/msexcel"); workbook.write(output); output.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 导出excle数据 * @param rrtTeacherSubject * @param request * @param response * @param model * @param redirectAttributes * @return */ @SuppressWarnings("deprecation") @RequestMapping(value = "export") public String export(RrtTeacherSubject rrtTeacherSubject, HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes redirectAttributes) { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet=workbook.createSheet(); sheet.setColumnWidth(0, 6500); sheet.setColumnWidth(1, 4500); sheet.setColumnWidth(2, 6500); sheet.setColumnWidth(3, 4500); sheet.setColumnWidth(4, 12000); // 设置字体 HSSFFont headfont = workbook.createFont(); headfont.setFontName("黑体"); headfont.setFontHeightInPoints((short) 14);// 字体大小 headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗 // 另一个样式 HSSFCellStyle headstyle = workbook.createCellStyle(); headstyle.setFont(headfont); headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中 headstyle.setLocked(true); headstyle.setWrapText(true);// 自动换行 // 另一个字体样式 HSSFFont columnHeadFont = workbook.createFont(); columnHeadFont.setFontName("宋体"); columnHeadFont.setFontHeightInPoints((short) 10); columnHeadFont.setColor(HSSFColor.WHITE.index); columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 列头的样式 HSSFCellStyle columnHeadStyle = workbook.createCellStyle(); // 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式) columnHeadStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); columnHeadStyle.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index); columnHeadStyle.setFont(columnHeadFont); columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中 columnHeadStyle.setLocked(true); columnHeadStyle.setWrapText(true); columnHeadStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); columnHeadStyle.setTopBorderColor(HSSFColor.BLACK.index); columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色 columnHeadStyle.setBorderLeft((short) 1);// 边框的大小 columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色 columnHeadStyle.setBorderRight((short) 1);// 边框的大小 columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体 columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色 HSSFFont font = workbook.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 10); // 普通单元格样式 HSSFCellStyle style = workbook.createCellStyle(); style.setFont(font); style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中 style.setWrapText(true); style.setLeftBorderColor(HSSFColor.BLACK.index); style.setBorderLeft((short) 1); style.setRightBorderColor(HSSFColor.BLACK.index); style.setBorderRight((short) 1); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体 style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色. style.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色. HSSFRow row1 = sheet.createRow(0); // 设置行高 row1.setHeight((short) 450); // 创建第一列 HSSFCell cell0 = row1.createCell(0); cell0.setCellValue(new HSSFRichTextString("教师科目关联管理表")); cell0.setCellStyle(headstyle); CellRangeAddress range = new CellRangeAddress(0, 0, 0, 4); sheet.addMergedRegion(range); // 第1行 HSSFRow row2 = sheet.createRow(1); row2.setHeight((short) 300); HSSFCell cell = row2.createCell(0); cell.setCellValue(new HSSFRichTextString("学校")); cell.setCellStyle(columnHeadStyle); cell = row2.createCell(1); cell.setCellValue(new HSSFRichTextString("教师姓名")); cell.setCellStyle(columnHeadStyle); cell = row2.createCell(2); cell.setCellValue(new HSSFRichTextString("教师工号")); cell.setCellStyle(columnHeadStyle); cell = row2.createCell(3); cell.setCellValue(new HSSFRichTextString("科目")); cell.setCellStyle(columnHeadStyle); cell = row2.createCell(4); cell.setCellValue(new HSSFRichTextString("备注")); cell.setCellStyle(columnHeadStyle); List<RrtTeacherSubject> rrtTeacherSubjectList = rrtTeacherSubjectService.findTeacherSubjectList(rrtTeacherSubject); int m = 2; for (int i = 0; i < rrtTeacherSubjectList.size(); i++) { RrtTeacherSubject teaSubject = rrtTeacherSubjectList.get(i); HSSFRow row = sheet.createRow(m); cell = row.createCell(0); cell.setCellValue(teaSubject.getOffice().getName()); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue(teaSubject.getTeacherName()); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue(teaSubject.getTeacherNo()); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue(teaSubject.getSubjectName()); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue(teaSubject.getRemarks()); cell.setCellStyle(style); m++; } StringBuilder failureMsg = new StringBuilder(); if(m == 2){ failureMsg.append("导出失败,失败原因:<br/>没有可导出数据"); addMessage(redirectAttributes, ""+failureMsg); }else{ try { String fileName = "教师科目关联管理.xls"; OutputStream output=response.getOutputStream(); response.reset(); response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName).getBytes(), "iso-8859-1")); response.setContentType("application/msexcel"); workbook.write(output); output.close(); return null; } catch (IOException e) { e.printStackTrace(); } } return "redirect:" + adminPath + "/teachersubject/rrtTeacherSubject/?repage"; } /** * 导入excle数据 * @param file * @param model * @param redirectAttributes * @return */ @SuppressWarnings("null") @RequestMapping(value = "import") public String importStuParentList(MultipartFile file,Model model,RedirectAttributes redirectAttributes) { String returnPath = "redirect:" + adminPath + "/teachersubject/rrtTeacherSubject/list?repage"; try { StringBuilder failureMsg = new StringBuilder(); Workbook workbook = WorkbookFactory.create(file.getInputStream()); Sheet sheet = workbook.getSheetAt(0); int maxRow = sheet.getLastRowNum(); if (maxRow < 2) { failureMsg.insert(0,"没有数据。"); addMessage(redirectAttributes, ""+failureMsg); return returnPath; } // 表头 Row titleRow = sheet.getRow(1); int maxCol = titleRow.getLastCellNum(); if (maxCol != 5) { failureMsg.insert(0,"列数必须=5。"); addMessage(redirectAttributes, ""+failureMsg); return returnPath; } String[] colsStart = {"学校", "教师姓名", "教师工号","科目","备注"}; for (int j = 0; j < colsStart.length; j++) { if (titleRow.getCell(j) == null || !colsStart[j].equals(titleRow.getCell(j).getStringCellValue())) { failureMsg.insert(0,"表头列名不正确,前7列应该分别为学校,教师姓名,教师工号,科目,备注。"); addMessage(redirectAttributes, ""+failureMsg); return returnPath; } } //读取数据放入集合 List<RrtTeacherSubject> list = new ArrayList<RrtTeacherSubject>(); for( int i =2; i<=maxRow; i++ ){ Row dataRow = sheet.getRow(i); dataRow.getCell(2).setCellType(Cell.CELL_TYPE_STRING); User teacher = rStuParentsService.findUserByNo(dataRow.getCell(2).getStringCellValue()); // User teacher = rStuParentsService.findUserByNo(new DecimalFormat("0").format(dataRow.getCell(2).getNumericCellValue())); String subjectId = bussSubjectService.findSubIdByName(dataRow.getCell(3).getStringCellValue()); RrtTeacherSubject rrtTeacherSubject =new RrtTeacherSubject(); if(teacher != null && subjectId !=null && subjectId != ""){ String officeName = dataRow.getCell(0).getStringCellValue(); String TeacherName = dataRow.getCell(1).getStringCellValue(); if(!officeName.equals(teacher.getOffice().getName()) || !TeacherName.equals(teacher.getName())){ failureMsg.append("<br/>学校或教师姓名与教师工号对应用户数据不符!"); }else{ rrtTeacherSubject.setOffice(teacher.getOffice()); rrtTeacherSubject.setTeacherId(teacher.getId()); rrtTeacherSubject.setSubjectId(subjectId); if(dataRow.getLastCellNum()>4) { rrtTeacherSubject.setRemarks(dataRow.getCell(4).getStringCellValue()); } list.add(rrtTeacherSubject); } }else{ failureMsg.append("<br/>系统未找到第"+(i+1)+"行教师工号或科目!"); } } if(failureMsg.length()<1){ //插入数据库 Integer successNum = 0; Integer failNum = 0; for(RrtTeacherSubject teaSub : list){ RrtTeacherSubject flag = rrtTeacherSubjectService.getTeaSubjectByTeaIdAndSubId(teaSub); if(flag==null){ rrtTeacherSubjectService.save(teaSub); successNum++; }else{ failNum++; } } failureMsg.append(successNum+"条数据导入成功!"); if(failNum !=0){ failureMsg.append(failNum+"条重复数据未导入!"); } }else{ failureMsg.insert(0,"导入失败,失败信息如下:"); } addMessage(redirectAttributes, ""+failureMsg); } catch (Exception e) { addMessage(redirectAttributes, "导入教师科目关联管理失败!失败信息:导入文档为空"); } return returnPath; }
项目中用到的excle导入,导出功能,Java端源码分享
最新推荐文章于 2021-02-12 16:51:44 发布