在操作过程中,需要对数据进行统计分析和汇总,导出成excel格式
核心思想:将需要导出的数据以集合的形式查询出来,并填充到具体的excel表中。进行for循环填充,注意:不要在循环中查数据库
- public void exportReportTeachers(String name,String sex,String username){
- Teacher teacher=new Teacher();
- teacher.setName(name);
- teacher.setUsername(username);
- teacher.setSex(sex);
- List<Teacher> teachers = teacherDAO.getConditionList(teacher);
- //生成一个xls文件
- HSSFWorkbook work =new HSSFWorkbook();
- HSSFSheet sheet=work.createSheet("教师统计表");
- CellRangeAddress cellRangeAddress=new CellRangeAddress(0, 0, 0,5);
- sheet.addMergedRegion(cellRangeAddress);
- // 设置单元格样式
- HSSFCellStyle cellStyle1 = work.createCellStyle();// 创建表格样式
- // 设置字体居中
- cellStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- // 设置字体
- HSSFFont font1 = work.createFont();
- font1.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
- font1.setColor(HSSFColor.BLACK.index);
- font1.setFontHeight((short) 250);
- font1.setFontName("宋体");
- font1.setBoldweight((short) 13);
- font1.setColor(HSSFColor.BLACK.index);
- cellStyle1.setFont(font1);
- // 垂直居中
- cellStyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- cellStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- // 加边框
- cellStyle1.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
- cellStyle1.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
- cellStyle1.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
- cellStyle1.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
- HSSFRow headRow = sheet.createRow(0);
- headRow.setHeight((short) (42 * 15.625));
- for (int i = 0; i <= 5; i++) {
- HSSFCell cell = headRow.createCell(i);
- cell.setCellValue("教师统计表");
- cell.setCellStyle(cellStyle1);
- }
- // 设置单元格样式
- HSSFCellStyle cellStyle = work.createCellStyle();// 创建表格样式
- // 设置字体
- HSSFFont font = work.createFont();
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
- font.setColor(HSSFColor.BLACK.index);
- // font.setFontHeight((short)250);
- font.setFontName("宋体");
- font.setBoldweight((short) 10);
- font.setColor(HSSFColor.BLACK.index);
- cellStyle.setFont(font);
- // 设置背景色
- cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
- cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
- cellStyle.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
- cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- // 加边框
- cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
- cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
- cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
- cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
- // 设置垂直居中
- cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- HSSFRow row = sheet.createRow(1);
- // 创建单元格
- HSSFCell cell = row.createCell(0);
- cell.setCellValue("序号");
- cell.setCellStyle(cellStyle);
- HSSFCell cell1 = row.createCell(1);
- cell1.setCellValue("教师Id");
- cell1.setCellStyle(cellStyle);
- HSSFCell cell2 = row.createCell(2);
- cell2.setCellValue("教师姓名");
- cell2.setCellStyle(cellStyle);
- HSSFCell cell3 = row.createCell(3);
- cell3.setCellValue("登录用户名");
- cell3.setCellStyle(cellStyle);
- HSSFCell cell4 = row.createCell(4);
- cell4.setCellValue("性别");
- cell4.setCellStyle(cellStyle);
- HSSFCell cell5 = row.createCell(5);
- cell5.setCellValue("所任课程");
- cell5.setCellStyle(cellStyle);
- // 设置单元格样式
- HSSFCellStyle valueCellStyle = work.createCellStyle();// 创建表格样式
- HSSFFont valueFont = work.createFont();
- valueFont.setColor(HSSFColor.BLACK.index);
- valueFont.setFontName("宋体");
- valueCellStyle.setFont(valueFont);
- // 设置自动换行
- // valueCellStyle.setWrapText(true);
- // 垂直居中
- valueCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- valueCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- // 加边框
- valueCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
- valueCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
- valueCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
- valueCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
- int rowNum = 2;
- for (int j = 0; j < teachers.size(); j++) {
- // 一个教师对应多门课程
- HSSFRow valueRow = sheet.createRow(rowNum);// 前面已经写两行了
- HSSFCell valueCell = valueRow.createCell(0);
- valueCell.setCellValue(j + 1);// 设置序号
- valueCell.setCellStyle(valueCellStyle);
- HSSFCell valueCell1 = valueRow.createCell(1);
- valueCell1.setCellValue(teachers.get(j).getId());// 教师id
- valueCell1.setCellStyle(valueCellStyle);
- HSSFCell valueCell2 = valueRow.createCell(2);
- valueCell2.setCellValue(teachers.get(j).getName());// 教师姓名
- valueCell2.setCellStyle(valueCellStyle);
- HSSFCell valueCell3 = valueRow.createCell(3);
- valueCell3.setCellValue(teachers.get(j).getUsername());// 教师用户名
- valueCell3.setCellStyle(valueCellStyle);
- HSSFCell valueCell4 = valueRow.createCell(4);
- valueCell4.setCellValue(teachers.get(j).getSexStr());//性别
- valueCell4.setCellStyle(valueCellStyle);
- HSSFCell valueCell5 = valueRow.createCell(5);
- valueCell5.setCellValue(teachers.get(j).getCourseNames());//课程名称
- valueCell5.setCellStyle(valueCellStyle);
- //行号,起始列号 ,行号 ,终止列号
- rowNum++;
- }
- // =================指定列宽======================
- sheet.setColumnWidth(0, 5 * 256);// 设置列宽
- sheet.setColumnWidth(1, 40 * 256);// 设置列宽
- sheet.setColumnWidth(2, 13 * 256);// 设置列宽
- sheet.setColumnWidth(3, 25 * 256);// 设置列宽
- sheet.setColumnWidth(4, 8 * 256);// 设置列宽
- sheet.setColumnWidth(5, 30 * 256);// 设置列宽
- // 设置页边距
- sheet.setMargin(HSSFSheet.BottomMargin, 0);
- sheet.setMargin(HSSFSheet.LeftMargin, (double) 0.4);
- sheet.setMargin(HSSFSheet.RightMargin, (double) 0.8);
- sheet.setMargin(HSSFSheet.TopMargin, 0);
- /*
- * //设置打印为横板 HSSFPrintSetup ps = sheet.getPrintSetup(); //true为横向
- * false为纵向 ps.setLandscape(true);
- */
- File dir = new File(AttachmentUtil.getTempDir());
- if (!dir.exists()) {
- dir.mkdirs();
- }
- try {
- String fileName = "教师统计表";
- File file = new File(dir.getAbsolutePath() + "/" + UUID.getUUID());
- OutputStream out = new FileOutputStream(file);
- work.write(out);
- FileUtil.download(file, fileName + ".xls");
- } catch (Exception e) {
- new BusinessException("文件传输出错");
- e.printStackTrace();
- }
- }