|
|
@@ -0,0 +1,172 @@
|
|
|
+package com.xjrsoft.module.schedule.util;
|
|
|
+
|
|
|
+import com.xjrsoft.module.courseTable.entity.ClassTime;
|
|
|
+import com.xjrsoft.module.schedule.vo.CourseDetailVo;
|
|
|
+import com.xjrsoft.module.schedule.vo.CourseTableVo;
|
|
|
+import org.apache.poi.ss.usermodel.BorderStyle;
|
|
|
+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.Row;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.VerticalAlignment;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author dzx
|
|
|
+ * @date 2025/4/9
|
|
|
+ */
|
|
|
+public class CourseTableExportQueryUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 合并9列
|
|
|
+ * @param rowNumber 写入的行号
|
|
|
+ */
|
|
|
+ public void createThirdTitle(Workbook workbook, Sheet sheet, int rowNumber, String leftTitle, String rightTitle){
|
|
|
+ Font font = workbook.createFont();
|
|
|
+ font.setBold(true);// 设置为粗体
|
|
|
+ font.setFontName("宋体");
|
|
|
+ //font.setColor(IndexedColors.RED.getIndex()); // 设置字体颜色为红色
|
|
|
+ font.setFontHeightInPoints((short) 18);
|
|
|
+
|
|
|
+ //设置样式
|
|
|
+ CellStyle cellStyle = workbook.createCellStyle();
|
|
|
+ cellStyle.setFont(font); // 将字体应用到样式
|
|
|
+ cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ cellStyle.setBorderTop(BorderStyle.THIN);
|
|
|
+ cellStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
+ cellStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
+ cellStyle.setBorderRight(BorderStyle.THIN);
|
|
|
+
|
|
|
+ //创建一行
|
|
|
+ Row row = sheet.createRow(rowNumber);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void createContent(Workbook workbook, Sheet sheet, int rowNumber, CourseTableVo data, String courseType){
|
|
|
+ Map<Integer, ClassTime> timeNumberMap = data.getClassTimeList().stream().collect(Collectors.toMap(ClassTime::getNumber, x -> x));
|
|
|
+ Map<String, List<CourseDetailVo>> timeNumberDataMap = data.getCourseList().stream().collect(Collectors.groupingBy(CourseDetailVo::getTimeNumber));
|
|
|
+
|
|
|
+ List<Integer> weekList = new ArrayList<>();
|
|
|
+ weekList.add(1);
|
|
|
+ weekList.add(2);
|
|
|
+ weekList.add(3);
|
|
|
+ weekList.add(4);
|
|
|
+ weekList.add(5);
|
|
|
+ weekList.add(6);
|
|
|
+ weekList.add(7);
|
|
|
+ Collections.sort(weekList);
|
|
|
+
|
|
|
+ List<Integer> timeNumberList = data.getClassTimeList().stream().map(ClassTime::getNumber).collect(Collectors.toList());
|
|
|
+ Collections.sort(timeNumberList);
|
|
|
+
|
|
|
+ Map<Integer, Integer> timePeriodMap = data.getClassTimeList().stream().collect(Collectors.toMap(ClassTime::getNumber, ClassTime::getTimePeriod));
|
|
|
+
|
|
|
+ ArrayList<ArrayList<String>> dataList = new ArrayList<>();
|
|
|
+
|
|
|
+ for (Integer timeNumber : timeNumberList) {
|
|
|
+ ArrayList<String> rowData = new ArrayList<>();
|
|
|
+ Integer timePeriod = timePeriodMap.get(timeNumber);
|
|
|
+ String timePeriodStr = "";
|
|
|
+ if (timePeriod == 1) {
|
|
|
+ timePeriodStr = "上午";
|
|
|
+ } else if (timePeriod == 2) {
|
|
|
+ timePeriodStr = "下午";
|
|
|
+ } else if (timePeriod == 3) {
|
|
|
+ timePeriodStr = "晚上";
|
|
|
+ }
|
|
|
+ rowData.add(timePeriodStr);
|
|
|
+ ClassTime classTime = timeNumberMap.get(timeNumber);
|
|
|
+ String timeNumberStr = classTime.getShortName() + "\r\n" + classTime.getSummerStartTime() + "-" + classTime.getSummerEndTime();
|
|
|
+ rowData.add(timeNumberStr);
|
|
|
+ List<CourseDetailVo> list1 = timeNumberDataMap.get(timeNumber.toString());
|
|
|
+ if (list1 == null) {
|
|
|
+ for (Integer week : weekList) {
|
|
|
+ rowData.add("");
|
|
|
+ }
|
|
|
+ dataList.add(rowData);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Map<Integer, List<CourseDetailVo>> weeksMap = list1.stream().collect(Collectors.groupingBy(CourseDetailVo::getWeeks));
|
|
|
+ for (Integer week : weekList) {
|
|
|
+ String content = "";
|
|
|
+ List<CourseDetailVo> list = weeksMap.get(week);
|
|
|
+ if (list != null && !list.isEmpty()) {
|
|
|
+ if (list.size() == 1) {
|
|
|
+ CourseDetailVo courseDetailVo = list.get(0);
|
|
|
+ content += courseDetailVo.getCourseName() + "\r\n";
|
|
|
+ content += courseDetailVo.getClassroomName() + "\r\n";
|
|
|
+ if ("teacher".equals(courseType)) {
|
|
|
+ content += courseDetailVo.getClassName();
|
|
|
+ } else if ("class".equals(courseType)) {
|
|
|
+ content += courseDetailVo.getTeacherName();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ CourseDetailVo courseDetailVo = list.get(0);
|
|
|
+ content += courseDetailVo.getCourseName() + "\r\n";
|
|
|
+ content += courseDetailVo.getClassroomName() + "\r\n";
|
|
|
+ if ("teacher".equals(courseType)) {
|
|
|
+ content += courseDetailVo.getClassName();
|
|
|
+ } else if ("class".equals(courseType)) {
|
|
|
+ Set<String> classNames = list.stream().map(CourseDetailVo::getTeacherName).collect(Collectors.toSet());
|
|
|
+ content += classNames.toString().replace("]", "").replace("[", "").replace(" ", "");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ rowData.add(content);
|
|
|
+ }
|
|
|
+ dataList.add(rowData);
|
|
|
+ }
|
|
|
+
|
|
|
+ Font font = workbook.createFont();
|
|
|
+ font.setFontName("宋体");
|
|
|
+ font.setFontHeightInPoints((short) 9);
|
|
|
+
|
|
|
+ // 创建一个单元格样式对象
|
|
|
+ CellStyle cellStyle = workbook.createCellStyle();
|
|
|
+ cellStyle.setFont(font); // 将字体应用到样式
|
|
|
+ cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ cellStyle.setWrapText(true);
|
|
|
+ cellStyle.setBorderTop(BorderStyle.THIN);
|
|
|
+ cellStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
+ cellStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
+ cellStyle.setBorderRight(BorderStyle.THIN);
|
|
|
+
|
|
|
+ for (ArrayList<String> strings : dataList) {
|
|
|
+ Row row = sheet.createRow(rowNumber);
|
|
|
+ int cellNumber = 0;
|
|
|
+ for (String string : strings) {
|
|
|
+ Cell row1cell1 = row.createCell(cellNumber);
|
|
|
+ row1cell1.setCellValue(string);
|
|
|
+ row1cell1.setCellStyle(cellStyle);
|
|
|
+ cellNumber++;
|
|
|
+ }
|
|
|
+ rowNumber++;
|
|
|
+ row.setHeightInPoints(48);
|
|
|
+ }
|
|
|
+ //合并
|
|
|
+ Map<Integer, List<Integer>> collect = data.getClassTimeList().stream()
|
|
|
+ .collect(Collectors.groupingBy(ClassTime::getTimePeriod, Collectors.mapping(ClassTime::getNumber, Collectors.toList())));
|
|
|
+ int firstRow = 2;
|
|
|
+ for (Integer i : collect.keySet()) {
|
|
|
+ int lastRow = firstRow + collect.get(i).size() - 1;
|
|
|
+ if (lastRow == firstRow) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, 0, 0));
|
|
|
+
|
|
|
+ firstRow = firstRow + collect.get(i).size();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|