wangliang преди 4 години
родител
ревизия
77af2189b3

+ 32 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/ExcelUtil.java

@@ -10,8 +10,10 @@ import org.apache.poi.hssf.usermodel.HSSFDateUtil;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.checkerframework.checker.units.qual.C;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.util.LinkedMultiValueMap;
@@ -40,6 +42,36 @@ public class ExcelUtil {
     private static final String DEFALUT_CONTENT_TYPE = "application/vnd.ms-excel";
     private static final String DEFALUT_EXT = ".xlsx";
 
+    /**
+     * excel多sheet
+     *
+     * @param fileName
+     * @param sheetNames
+     * @param dataClass
+     * @param dataset
+     * @param response
+     */
+    public static void excelExportBatchSheet(String fileName, List<String> sheetNames, List<Class<?>> dataClass, List<Collection<?>> dataset, HttpServletResponse response) throws Exception {
+        log.debug("导出Excel开始...");
+        response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(fileName, SystemConstant.CHARSET_NAME) + DEFALUT_EXT);
+        response.setContentType(DEFALUT_CONTENT_TYPE);
+        ServletOutputStream outputStream = response.getOutputStream();
+
+        ExcelWriter excelExporter = new ExcelWriter();
+//        excelExporter.write(dataset.get(0), outputStream);
+        for (int i = 0; i < dataClass.size(); i++) {
+            Class clazz = dataClass.get(i);
+            String sheetName = sheetNames.get(i);
+            Collection collection = dataset.get(i);
+            Sheet sheet = excelExporter.createSheet(clazz, sheetName);
+            excelExporter.write(collection, sheet);
+        }
+        excelExporter.getWorkbook().write(outputStream);
+        outputStream.flush();
+        outputStream.close();
+        log.debug("导出Excel结束");
+    }
+
     /**
      * 导出excel
      *

+ 4 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/excel/ExcelExport.java

@@ -13,6 +13,10 @@ public abstract class ExcelExport {
     private Class<?> dataClass;
     private List<ColumnSetting> columnSettings;
 
+    public ExcelExport(){
+
+    }
+
     public ExcelExport(Class<?> dataClass) {
         this.dataClass = dataClass;
         this.columnSettings = getColumnSettings(dataClass);

+ 66 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/excel/ExcelWriter.java

@@ -30,6 +30,10 @@ public class ExcelWriter extends ExcelExport {
 
     private XSSFCellStyle style2;
 
+    public ExcelWriter(){
+        workbook = new XSSFWorkbook();
+    }
+
     public ExcelWriter(Class<?> dataClass, String sheetName) {
         super(dataClass);
         // 声明一个工作薄
@@ -41,6 +45,15 @@ public class ExcelWriter extends ExcelExport {
         sheet.setDefaultColumnWidth((short) 15);
     }
 
+    public Sheet createSheet(Class<?> dataClass, String sheetName) {
+        setDataClass(dataClass);
+        setColumnSettings(getColumnSettings(dataClass));
+        Sheet sheet = workbook.createSheet(sheetName);
+        // 设置表格默认列宽度为15个字节
+        sheet.setDefaultColumnWidth((short) 15);
+        return sheet;
+    }
+
     private List<ColumnSetting> createColumnSettings() {
         List<ColumnSetting> columnSettings = this.getColumnSettings();
         // 产生表格标题行
@@ -89,4 +102,57 @@ public class ExcelWriter extends ExcelExport {
         }
         workbook.write(out);
     }
+
+    /**
+     * 写入excel
+     *
+     * @param dataset 数据集合
+     * @param sheet
+     * @throws SecurityException
+     * @throws NoSuchMethodException
+     * @throws InvocationTargetException
+     * @throws IllegalArgumentException
+     * @throws IllegalAccessException
+     */
+    public void write(Collection<?> dataset, Sheet sheet) throws Exception {
+        List<ColumnSetting> columnSettings = this.createColumnSettings(sheet);
+        int index = 0;
+        for (Object obj : dataset) {
+            index++;
+            row = sheet.createRow(index);// 创建行
+            // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
+            for (short i = 0; i < columnSettings.size(); i++) {
+                cell = row.createCell(i);// 创建列
+                cell.setCellStyle(style2);
+                String methodName = columnSettings.get(i).getGetMethodName();
+                Method method = this.getDataClass().getMethod(methodName, new Class[]{});
+                Object value = method.invoke(obj, new Object[]{});
+                cell.setCellValue(value == null ? "" : value.toString());
+            }
+        }
+//        workbook.write(out);
+    }
+
+    private List<ColumnSetting> createColumnSettings(Sheet sheet) {
+        List<ColumnSetting> columnSettings = this.getColumnSettings();
+        // 产生表格标题行
+        row = sheet.createRow(0);
+        for (short i = 0; i < columnSettings.size(); i++) {
+            cell = row.createCell(i);
+            style = workbook.createCellStyle();
+            style.setFillForegroundColor(new XSSFColor(new Color(227, 239, 217)));
+            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+            cell.setCellStyle(style);
+            XSSFRichTextString text = new XSSFRichTextString(columnSettings.get(i).getHeader());
+            cell.setCellValue(text);
+            if (columnSettings.get(i).getWidth() > 0) {
+                sheet.setColumnWidth(i, columnSettings.get(i).getWidth() * 256);
+            }
+        }
+        return columnSettings;
+    }
+
+    public XSSFWorkbook getWorkbook() {
+        return workbook;
+    }
 }

+ 9 - 0
teachcloud-report-business/src/main/java/com/qmth/teachcloud/report/business/service/TAExamCourseService.java

@@ -125,4 +125,13 @@ public interface TAExamCourseService extends IService<TAExamCourse> {
      * @return 结果
      */
     TrialCalculationResult trialCalculate(Long examId, String courseCode, BigDecimal coefficient);
+
+    /**
+     * 导出课程分析接口
+     *
+     * @param examId
+     * @param semester
+     * @param schoolId
+     */
+    void courseExport(Long examId, SemesterEnum semester, Long schoolId);
 }

+ 12 - 1
teachcloud-report-business/src/main/java/com/qmth/teachcloud/report/business/service/impl/TAExamCourseServiceImpl.java

@@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.qmth.teachcloud.common.contant.SystemConstant;
-import com.qmth.teachcloud.common.entity.SysOrg;
 import com.qmth.teachcloud.common.entity.SysUser;
 import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
 import com.qmth.teachcloud.common.service.SysOrgService;
@@ -330,6 +329,18 @@ public class TAExamCourseServiceImpl extends ServiceImpl<TAExamCourseMapper, TAE
         return trialCalculationResult;
     }
 
+    /**
+     * 导出课程分析接口
+     *
+     * @param examId
+     * @param semester
+     * @param schoolId
+     */
+    @Override
+    public void courseExport(Long examId, SemesterEnum semester, Long schoolId) {
+        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
+    }
+
 
     /**
      * 获取开课课程考试课程总览

+ 81 - 0
teachcloud-report/src/main/java/com/qmth/teachcloud/report/api/CourseController.java

@@ -24,14 +24,25 @@ import com.qmth.teachcloud.report.business.enums.SemesterEnum;
 import com.qmth.teachcloud.report.business.service.*;
 import com.qmth.teachcloud.report.business.templete.execute.AsyncDataCalculateTempleteService;
 import io.swagger.annotations.*;
+import org.apache.poi.hssf.usermodel.HSSFCellStyle;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;
+import org.apache.poi.xssf.usermodel.XSSFCellStyle;
+import org.apache.poi.xssf.usermodel.XSSFColor;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
+import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletResponse;
+import java.awt.Color;
 import java.math.BigDecimal;
+import java.net.URLEncoder;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -88,6 +99,76 @@ public class CourseController {
         ExcelUtil.excelExport("开课课程考试总览", TAExamCourseExportDto.class, taExamCourseExportDtoList, response);
     }
 
+    @ApiOperation(value = "导出课程分析接口")
+    @RequestMapping(value = "/survey_teacher/course_export", method = RequestMethod.POST)
+    @ApiResponses({@ApiResponse(code = 200, message = "考试科目信息", response = Result.class)})
+    public void courseExport(@ApiParam(value = "考试id", required = true) @RequestParam String examId,
+                             @ApiParam(value = "学期", required = true) @RequestParam SemesterEnum semester,
+                             @ApiParam(value = "学校id", required = false) @RequestParam(required = false) String schoolId,
+                             HttpServletResponse response) throws Exception {
+        IPage<TAExamCourseResult> taExamCourseResultIPage = taExamCourseService.surveyTeacherList(new Page<>(0, 10), SystemConstant.convertIdToLong(examId), semester, Objects.isNull(schoolId) ? SystemConstant.convertIdToLong(String.valueOf(ServletUtil.getRequestHeaderSchoolId())) : SystemConstant.convertIdToLong(schoolId), null, null);
+        Gson gson = new Gson();
+        List<TAExamCourseExportDto> taExamCourseExportDtoList = gson.fromJson(JacksonUtil.parseJson(taExamCourseResultIPage.getRecords()), new TypeToken<List<TAExamCourseExportDto>>() {
+        }.getType());
+        ExcelUtil.excelExportBatchSheet("测试", Arrays.asList("测试1", "测试2"), Arrays.asList(TAExamCourseExportDto.class, TAExamCourseExportDto.class), Arrays.asList(taExamCourseExportDtoList, taExamCourseExportDtoList), response);
+
+//        //创建excel
+//        XSSFWorkbook wb = new XSSFWorkbook();
+//        Sheet sheet = wb.createSheet("成绩对比明细");
+//        XSSFCellStyle style = wb.createCellStyle();
+//        style.setFillForegroundColor(new XSSFColor(new Color(227, 239, 217)));
+//        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+//        style.setAlignment(HorizontalAlignment.CENTER); // 水平居中格式
+//        style.setVerticalAlignment(VerticalAlignment.CENTER); //垂直居中
+//        Row row = sheet.createRow(0);
+//
+//        Cell cell = row.createCell(0);
+//        cell.setCellValue("测试1");
+//        cell.setCellStyle(style);
+////        CellRangeAddress region1 = new CellRangeAddress(0, 1, (short) 0, (short) 0);
+////        sheet.addMergedRegion(region1);
+//        sheet.setColumnWidth(0, 15 * 256);
+//
+//        Cell cell1 = row.createCell(1);
+//        cell1.setCellValue("测试2");
+//        cell1.setCellStyle(style);
+////        CellRangeAddress region2 = new CellRangeAddress(0, 1, (short) 1, (short) 1);
+////        sheet.addMergedRegion(region2);
+//        sheet.setColumnWidth(1, 15 * 256);
+//
+//        /***************************************/
+//
+//        Sheet sheet1 = wb.createSheet("全校卷面分数段分布");
+//        XSSFCellStyle style1 = wb.createCellStyle();
+//        style1.setFillForegroundColor(new XSSFColor(new Color(227, 239, 217)));
+//        style1.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+//        style1.setAlignment(HorizontalAlignment.CENTER); // 水平居中格式
+//        style1.setVerticalAlignment(VerticalAlignment.CENTER); //垂直居中
+//        Row row1 = sheet1.createRow(0);
+//
+//        Cell cell2 = row1.createCell(0);
+//        cell2.setCellValue("测试3");
+//        cell2.setCellStyle(style);
+////        CellRangeAddress region1 = new CellRangeAddress(0, 1, (short) 0, (short) 0);
+////        sheet.addMergedRegion(region1);
+//        sheet1.setColumnWidth(0, 15 * 256);
+//
+//        Cell cell3 = row1.createCell(1);
+//        cell3.setCellValue("测试4");
+//        cell3.setCellStyle(style);
+////        CellRangeAddress region2 = new CellRangeAddress(0, 1, (short) 1, (short) 1);
+////        sheet.addMergedRegion(region2);
+//        sheet1.setColumnWidth(1, 15 * 256);
+//
+//        response.setHeader("Content-Disposition", "inline; filename="
+//                + URLEncoder.encode("测试", "UTF-8") + ".xlsx");
+//        response.setContentType("application/vnd.ms-excel");
+//        ServletOutputStream outputStream = response.getOutputStream();
+//        wb.write(outputStream);
+//        outputStream.flush();
+//        outputStream.close();
+    }
+
     @ApiOperation(value = "开课课程考试总览-教师各课堂成绩排名接口")
     @RequestMapping(value = "/survey_teacher/teacher_view", method = RequestMethod.POST)
     @ApiResponses({@ApiResponse(code = 200, message = "考试科目信息", response = TAExamCourseCollegeTeacherResult.class)})