浏览代码

excel 导出共用方法改动

caozixuan 2 年之前
父节点
当前提交
8dc8cbf8f2

+ 15 - 2
distributed-print-business/src/main/java/com/qmth/distributed/print/business/bean/dto/excel/GradePaperStructDto.java

@@ -2,69 +2,82 @@ package com.qmth.distributed.print.business.bean.dto.excel;
 
 import com.qmth.teachcloud.common.annotation.ExcelNote;
 import com.qmth.teachcloud.common.annotation.ExcelProperty;
-import com.qmth.teachcloud.common.enums.QuestionType;
+import com.qmth.teachcloud.common.annotation.excelStyle.ExcelDataStyle;
+import com.qmth.teachcloud.common.annotation.excelStyle.ExcelHeaderStyle;
+import org.apache.poi.ss.usermodel.IndexedColors;
 
 import javax.validation.constraints.NotNull;
-import java.math.BigDecimal;
 
 /**
  * @Description: 分析试卷结构excel操作类
  * @Author: CaoZixuan
  * @Date: 2022-05-30
  */
+@ExcelHeaderStyle(fillColorRed = 0,fillColorGreen = 0,fillColorBlue = 0,fontColor = IndexedColors.WHITE1)
 public class GradePaperStructDto {
     @ExcelNote(value = "课程代码")
     @ExcelProperty(name = "课程代码", width = 30, index = 1)
+    @ExcelDataStyle(lock = true)
     @NotNull
     private String courseCode;
 
     @ExcelNote(value = "课程名称")
     @ExcelProperty(name = "课程名称", width = 30, index = 2)
+    @ExcelDataStyle(lock = true)
     @NotNull
     private String courseName;
 
     @ExcelNote(value = "试卷编号")
     @ExcelProperty(name = "试卷编号", width = 30, index = 3)
+    @ExcelDataStyle(lock = true)
     @NotNull
     private String paperNumber;
 
     @ExcelNote(value = "试卷类型")
     @ExcelProperty(name = "试卷类型", width = 30, index = 4)
+    @ExcelDataStyle(lock = true)
     @NotNull
     private String paperType;
 
     @ExcelNote(value = "题目类型")
     @ExcelProperty(name = "题目类型", width = 30, index = 5)
+    @ExcelDataStyle(lock = true)
     @NotNull
     private String numberType;
 
     @ExcelNote(value = "大题名称")
     @ExcelProperty(name = "大题名称", width = 30, index = 6)
+    @ExcelDataStyle(lock = true)
     @NotNull
     private String bigTopicName;
 
     @ExcelNote(value = "大题号")
     @ExcelProperty(name = "大题号", width = 30, index = 7)
+    @ExcelDataStyle(lock = true)
     @NotNull
     private String bigQuestionNumber;
 
     @ExcelNote(value = "小题号")
     @ExcelProperty(name = "小题号", width = 30, index = 8)
+    @ExcelDataStyle(lock = true)
     @NotNull
     private String smallQuestionNumber;
 
     @ExcelNote(value = "题目满分")
     @ExcelProperty(name = "题目满分", width = 30, index = 9)
+    @ExcelDataStyle(lock = true)
     @NotNull
     private String fullScore;
 
     @ExcelNote(value = "知识维度")
     @ExcelProperty(name = "知识维度", width = 30, index = 10)
+    @ExcelDataStyle
     @NotNull
     private String knowledgeDimension;
 
     @ExcelNote(value = "能力维度")
     @ExcelProperty(name = "能力维度", width = 30, index = 11)
+    @ExcelDataStyle
     private String abilityDimension;
 
     public String getCourseCode() {

+ 9 - 6
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/GradeBatchServiceImpl.java

@@ -9,15 +9,19 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.google.common.collect.Lists;
 import com.qmth.boot.api.exception.ApiException;
 import com.qmth.distributed.print.business.bean.dto.ExamStudentCourseClassDto;
-import com.qmth.distributed.print.business.bean.dto.ExamStudentCourseDto;
 import com.qmth.distributed.print.business.bean.dto.GradeBatchStudentClazzDto;
-import com.qmth.distributed.print.business.bean.dto.GradeBatchStudentDto;
 import com.qmth.distributed.print.business.bean.params.analyze.GradeBatchParam;
 import com.qmth.distributed.print.business.bean.result.analyze.GradeBatchResult;
-import com.qmth.distributed.print.business.entity.*;
+import com.qmth.distributed.print.business.entity.ExamTask;
+import com.qmth.distributed.print.business.entity.GradeBatch;
+import com.qmth.distributed.print.business.entity.GradeBatchPaper;
+import com.qmth.distributed.print.business.entity.GradeBatchStudentClazz;
 import com.qmth.distributed.print.business.mapper.ExamStudentMapper;
 import com.qmth.distributed.print.business.mapper.GradeBatchMapper;
-import com.qmth.distributed.print.business.service.*;
+import com.qmth.distributed.print.business.service.ExamTaskService;
+import com.qmth.distributed.print.business.service.GradeBatchPaperService;
+import com.qmth.distributed.print.business.service.GradeBatchService;
+import com.qmth.distributed.print.business.service.GradeBatchStudentClazzService;
 import com.qmth.teachcloud.common.config.DictionaryConfig;
 import com.qmth.teachcloud.common.contant.SpringContextHolder;
 import com.qmth.teachcloud.common.contant.SystemConstant;
@@ -33,7 +37,6 @@ import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.time.DateFormatUtils;
-import org.apache.poi.hssf.usermodel.HSSFCellStyle;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.xssf.usermodel.*;
 import org.springframework.stereotype.Service;
@@ -308,7 +311,7 @@ public class GradeBatchServiceImpl extends ServiceImpl<GradeBatchMapper, GradeBa
         }
 
         // sheet添加保护,这个一定要否则光锁定还是可以编辑的
-        sheet.protectSheet("Qmth87863577");
+        sheet.protectSheet(SystemConstant.EXCEL_PROTECT_KEY);
 
         OutputStream output = response.getOutputStream();
         response.reset();

+ 9 - 9
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/PdfUtil.java

@@ -261,13 +261,13 @@ public class PdfUtil {
         }
     }
 
-//    public static void main(String[] args) {
-//        File file2 = new File("E:\\file\\《航空通信原理B》期末考试 A卷 答题卷.pdf");
-//        try {
-//            System.out.println(PdfUtil.getPdfFormat(file2));
-//        } catch (IOException e) {
-//            e.printStackTrace();
-//        }
-//
-//    }
+    public static void main(String[] args) {
+        File file2 = new File("E:\\file\\《航空通信原理B》期末考试 A卷 答题卷.pdf");
+        try {
+            System.out.println(PdfUtil.getPdfFormat(file2));
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+    }
 }

+ 100 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/annotation/excelStyle/ExcelDataStyle.java

@@ -0,0 +1,100 @@
+package com.qmth.teachcloud.common.annotation.excelStyle;
+
+import org.apache.poi.ss.usermodel.*;
+
+import java.lang.annotation.*;
+
+/**
+ * @Description: excel单元格格式 - 数据内容格式注解
+ * @Author: CaoZixuan
+ * @Date: 2022-07-21
+ */
+@Documented
+@Target({ElementType.FIELD})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface ExcelDataStyle {
+    // 文字位置
+
+    /**
+     * 水平方向
+     *
+     * @return 居左
+     */
+    HorizontalAlignment horizontal() default HorizontalAlignment.LEFT;
+
+    /**
+     * 垂直方向
+     *
+     * @return 居中
+     */
+    VerticalAlignment vertical() default VerticalAlignment.CENTER;
+
+    // 边框线条
+
+    /**
+     * 上边框线条
+     *
+     * @return 默认细实线
+     */
+    BorderStyle borderTop() default BorderStyle.NONE;
+
+    /**
+     * 下边框线条
+     *
+     * @return 默认细实线
+     */
+    BorderStyle borderBottom() default BorderStyle.NONE;
+
+    /**
+     * 左边框线条
+     *
+     * @return 默认细实线
+     */
+    BorderStyle borderLeft() default BorderStyle.NONE;
+
+    /**
+     * 右边框线条
+     *
+     * @return 默认细实线
+     */
+    BorderStyle borderRight() default BorderStyle.NONE;
+
+    // 边框默认颜色黑色Color(0, 0, 0)
+    int borderColorRed() default 0;
+
+    int borderColorGreen() default 0;
+
+    int borderColorBlue() default 0;
+
+    // 背景
+    FillPatternType fillPartner() default FillPatternType.NO_FILL;
+
+    // 背景颜色默认白色Color(255, 255, 255)
+    int fillColorRed() default 255;
+
+    int fillColorGreen() default 255;
+
+    int fillColorBlue() default 255;
+
+    // 字体
+
+    /**
+     * 字体大小
+     *
+     * @return 默认10
+     */
+    short fontSize() default 10;
+
+    /**
+     * 字体名称
+     *
+     * @return 默认宋体
+     */
+    String fontName() default "宋体";
+
+    // 字体颜色默认黑色
+    IndexedColors fontColor() default IndexedColors.BLACK1;
+
+    //单元格锁定
+    boolean lock() default false;
+}

+ 97 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/annotation/excelStyle/ExcelHeaderStyle.java

@@ -0,0 +1,97 @@
+package com.qmth.teachcloud.common.annotation.excelStyle;
+
+import org.apache.poi.ss.usermodel.*;
+
+import java.lang.annotation.*;
+
+/**
+ * @Description: excel单元格格式 - 表头格式注解
+ * @Author: CaoZixuan
+ * @Date: 2022-07-08
+ */
+@Documented
+@Target({ElementType.FIELD,ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface ExcelHeaderStyle {
+    // 文字位置
+
+    /**
+     * 水平方向
+     *
+     * @return 居左
+     */
+    HorizontalAlignment horizontal() default HorizontalAlignment.LEFT;
+
+    /**
+     * 垂直方向
+     *
+     * @return 居中
+     */
+    VerticalAlignment vertical() default VerticalAlignment.CENTER;
+
+    // 边框线条
+
+    /**
+     * 上边框线条
+     *
+     * @return 默认细实线
+     */
+    BorderStyle borderTop() default BorderStyle.NONE;
+
+    /**
+     * 下边框线条
+     *
+     * @return 默认细实线
+     */
+    BorderStyle borderBottom() default BorderStyle.NONE;
+
+    /**
+     * 左边框线条
+     *
+     * @return 默认细实线
+     */
+    BorderStyle borderLeft() default BorderStyle.NONE;
+
+    /**
+     * 右边框线条
+     *
+     * @return 默认细实线
+     */
+    BorderStyle borderRight() default BorderStyle.NONE;
+
+    // 边框默认颜色黑色Color(0, 0, 0)
+    int borderColorRed() default 0;
+
+    int borderColorGreen() default 0;
+
+    int borderColorBlue() default 0;
+
+    // 背景
+    FillPatternType fillPartner() default FillPatternType.SOLID_FOREGROUND;
+
+    // 背景颜色默认绿色Color(227, 239, 217)
+    int fillColorRed() default 227;
+
+    int fillColorGreen() default 239;
+
+    int fillColorBlue() default 217;
+
+    // 字体
+
+    /**
+     * 字体大小
+     *
+     * @return 默认12
+     */
+    short fontSize() default 12;
+
+    /**
+     * 字体名称
+     *
+     * @return 默认宋体
+     */
+    String fontName() default "宋体";
+
+    // 字体颜色默认黑色
+    IndexedColors fontColor() default IndexedColors.BLACK1;
+}

+ 1 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/contant/SystemConstant.java

@@ -115,6 +115,7 @@ public class SystemConstant {
     public static final String COMMA_OF_CHINESE = ",";
     public static final String PAUSE_SIGN = "、";
     public static final String PUSH_OPERATE_NOTICE = "操作成功,请去基础配置 -> 系统设置 -> 同步管理中查看结果";
+    public static final String EXCEL_PROTECT_KEY = "Qmth87863577";
 
     /**
      * oss url过期时间

+ 12 - 1
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/excel/ColumnSetting.java

@@ -1,15 +1,19 @@
 package com.qmth.teachcloud.common.util.excel;
 
+import java.lang.reflect.Field;
+
 public class ColumnSetting implements Comparable<ColumnSetting> {
 
     private String header;
     private String getMethodName;
+    private Field field;
     private int width;
     private int index;
 
-    public ColumnSetting(String header, String getMethodName, int width, int index) {
+    public ColumnSetting(String header, String getMethodName,Field field, int width, int index) {
         this.header = header;
         this.getMethodName = getMethodName;
+        this.field = field;
         this.width = width;
         this.index = index;
     }
@@ -46,6 +50,13 @@ public class ColumnSetting implements Comparable<ColumnSetting> {
         this.index = index;
     }
 
+    public Field getField() {
+        return field;
+    }
+
+    public void setField(Field field) {
+        this.field = field;
+    }
 
     @Override
     public int compareTo(ColumnSetting columnSetting) {

+ 19 - 20
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/excel/ExcelExport.java

@@ -13,7 +13,7 @@ public abstract class ExcelExport {
     private Class<?> dataClass;
     private List<ColumnSetting> columnSettings;
 
-    public ExcelExport(){
+    public ExcelExport() {
 
     }
 
@@ -46,29 +46,28 @@ public abstract class ExcelExport {
      */
     protected List<ColumnSetting> getColumnSettings(Class<?> dataClass) {
         List<ColumnSetting> columnSettings = new ArrayList<>();
-        // 先在方法上找ExcelProperty注解
-        Method[] methods = dataClass.getDeclaredMethods();
-        for (Method method : methods) {
-            ExcelProperty exportProperty = method.getAnnotation(ExcelProperty.class);
+//        // 先在方法上找ExcelProperty注解
+//        Method[] methods = dataClass.getDeclaredMethods();
+//        for (Method method : methods) {
+//            ExcelProperty exportProperty = method.getAnnotation(ExcelProperty.class);
+//            if (exportProperty != null && exportProperty.name().trim().length() > 0) {
+//                ColumnSetting columnSetting = new ColumnSetting(exportProperty.name(), method.getName(),
+//                        exportProperty.width(), exportProperty.index());
+//                columnSettings.add(columnSetting);
+//            }
+//        }
+        // 如果方法上找不到注解,再到属性上找
+        Field[] fields = dataClass.getDeclaredFields();
+        for (Field field : fields) {
+            ExcelProperty exportProperty = field.getAnnotation(ExcelProperty.class);
             if (exportProperty != null && exportProperty.name().trim().length() > 0) {
-                ColumnSetting columnSetting = new ColumnSetting(exportProperty.name(), method.getName(),
-                        exportProperty.width(), exportProperty.index());
+                ColumnSetting columnSetting = new ColumnSetting(exportProperty.name(),
+                        "get" + toUpperCaseFirstOne(field.getName()), field, exportProperty.width(),
+                        exportProperty.index());
                 columnSettings.add(columnSetting);
             }
         }
-        // 如果方法上找不到注解,再到属性上找
-        if (columnSettings.size() == 0) {
-            Field[] fields = dataClass.getDeclaredFields();
-            for (Field field : fields) {
-                ExcelProperty exportProperty = field.getAnnotation(ExcelProperty.class);
-                if (exportProperty != null && exportProperty.name().trim().length() > 0) {
-                    ColumnSetting columnSetting = new ColumnSetting(exportProperty.name(),
-                            "get" + toUpperCaseFirstOne(field.getName()), exportProperty.width(),
-                            exportProperty.index());
-                    columnSettings.add(columnSetting);
-                }
-            }
-        }
+
         Collections.sort(columnSettings);
         return columnSettings;
     }

+ 116 - 10
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/excel/ExcelWriter.java

@@ -1,16 +1,17 @@
 package com.qmth.teachcloud.common.util.excel;
 
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.FillPatternType;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
+import com.qmth.teachcloud.common.annotation.excelStyle.ExcelDataStyle;
+import com.qmth.teachcloud.common.annotation.excelStyle.ExcelHeaderStyle;
+import com.qmth.teachcloud.common.contant.SystemConstant;
+import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.xssf.usermodel.XSSFCellStyle;
 import org.apache.poi.xssf.usermodel.XSSFColor;
 import org.apache.poi.xssf.usermodel.XSSFRichTextString;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
-import java.awt.*;
+import java.awt.Color;
 import java.io.OutputStream;
+import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.Collection;
@@ -19,7 +20,7 @@ import java.util.Objects;
 
 public class ExcelWriter extends ExcelExport {
 
-    private XSSFWorkbook workbook;// 工作簿
+    private final XSSFWorkbook workbook;// 工作簿
 
     private Sheet sheet; // 工作表
 
@@ -59,11 +60,56 @@ public class ExcelWriter extends ExcelExport {
         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 = workbook.createCellStyle();
+        ExcelHeaderStyle excelHeaderStyle = this.getDataClass().getAnnotation(ExcelHeaderStyle.class);
+        if (Objects.nonNull(excelHeaderStyle)) {
+            // 文字位置
+            style.setAlignment(excelHeaderStyle.horizontal());
+            style.setVerticalAlignment(excelHeaderStyle.vertical());
+
+            //边框
+            int borderRed = excelHeaderStyle.borderColorRed();
+            int borderGreen = excelHeaderStyle.borderColorGreen();
+            int borderBlue = excelHeaderStyle.borderColorBlue();
+            XSSFColor borderColor = new XSSFColor(new Color(borderRed, borderGreen, borderBlue));
+            // 上边框
+            if (excelHeaderStyle.borderTop().getCode() > 0) {
+                style.setBorderTop(excelHeaderStyle.borderTop());
+                style.setTopBorderColor(borderColor);
+            }
+            // 下边框
+            if (excelHeaderStyle.borderBottom().getCode() > 0) {
+                style.setBorderBottom(excelHeaderStyle.borderBottom());
+                style.setBottomBorderColor(borderColor);
+            }
+            // 左边框
+            if (excelHeaderStyle.borderLeft().getCode() > 0) {
+                style.setBorderLeft(excelHeaderStyle.borderLeft());
+                style.setLeftBorderColor(borderColor);
+            }
+            // 右边框
+            if (excelHeaderStyle.borderRight().getCode() > 0) {
+                style.setBorderRight(excelHeaderStyle.borderRight());
+                style.setRightBorderColor(borderColor);
+            }
+
+            // 背景
+            style.setFillPattern(excelHeaderStyle.fillPartner());
+            XSSFColor fillColor = new XSSFColor(new Color(excelHeaderStyle.fillColorRed(), excelHeaderStyle.fillColorGreen(), excelHeaderStyle.fillColorBlue()));
+            style.setFillForegroundColor(fillColor);
+
+            // 字体
+            Font font = workbook.createFont();
+            font.setFontHeightInPoints(excelHeaderStyle.fontSize());
+            font.setFontName(excelHeaderStyle.fontName());
+            font.setColor(excelHeaderStyle.fontColor().getIndex());
+            style.setFont(font);
+        } else {
             style.setFillForegroundColor(new XSSFColor(new Color(227, 239, 217)));
             style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+        }
+        for (short i = 0; i < columnSettings.size(); i++) {
+            cell = row.createCell(i);
             cell.setCellStyle(style);
             XSSFRichTextString text = new XSSFRichTextString(columnSettings.get(i).getHeader());
             cell.setCellValue(text);
@@ -95,7 +141,9 @@ public class ExcelWriter extends ExcelExport {
                 // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
                 for (short i = 0; i < columnSettings.size(); i++) {
                     cell = row.createCell(i);// 创建列
-                    cell.setCellStyle(style2);
+                    Field field = columnSettings.get(i).getField();
+
+                    cell.setCellStyle(this.getCellStyle(workbook.createCellStyle(), field));
                     String methodName = columnSettings.get(i).getGetMethodName();
                     Method method = this.getDataClass().getMethod(methodName, new Class[]{});
                     Object value = method.invoke(obj, new Object[]{});
@@ -160,4 +208,62 @@ public class ExcelWriter extends ExcelExport {
     public XSSFWorkbook getWorkbook() {
         return workbook;
     }
+
+    private XSSFCellStyle getCellStyle(XSSFCellStyle style, Field field) {
+        ExcelDataStyle excelDataStyle = field.getAnnotation(ExcelDataStyle.class);
+        if (Objects.isNull(excelDataStyle)) {
+            return style;
+        } else {
+            // 文字位置
+            style.setAlignment(excelDataStyle.horizontal());
+            style.setVerticalAlignment(excelDataStyle.vertical());
+
+            //边框
+            int borderRed = excelDataStyle.borderColorRed();
+            int borderGreen = excelDataStyle.borderColorGreen();
+            int borderBlue = excelDataStyle.borderColorBlue();
+            XSSFColor borderColor = new XSSFColor(new Color(borderRed, borderGreen, borderBlue));
+            // 上边框
+            if (excelDataStyle.borderTop().getCode() > 0) {
+                style.setBorderTop(excelDataStyle.borderTop());
+                style.setTopBorderColor(borderColor);
+            }
+            // 下边框
+            if (excelDataStyle.borderBottom().getCode() > 0) {
+                style.setBorderBottom(excelDataStyle.borderBottom());
+                style.setBottomBorderColor(borderColor);
+            }
+            // 左边框
+            if (excelDataStyle.borderLeft().getCode() > 0) {
+                style.setBorderLeft(excelDataStyle.borderLeft());
+                style.setLeftBorderColor(borderColor);
+            }
+            // 右边框
+            if (excelDataStyle.borderRight().getCode() > 0) {
+                style.setBorderRight(excelDataStyle.borderRight());
+                style.setRightBorderColor(borderColor);
+            }
+
+            // 背景
+            style.setFillPattern(excelDataStyle.fillPartner());
+            XSSFColor fillColor = new XSSFColor(new Color(excelDataStyle.fillColorRed(), excelDataStyle.fillColorGreen(), excelDataStyle.fillColorBlue()));
+            style.setFillForegroundColor(fillColor);
+
+            // 字体
+            Font font = workbook.createFont();
+            font.setFontHeightInPoints(excelDataStyle.fontSize());
+            font.setFontName(excelDataStyle.fontName());
+            font.setColor(excelDataStyle.fontColor().getIndex());
+            style.setFont(font);
+
+            // 锁定
+            if (excelDataStyle.lock()) {
+                style.setLocked(true);
+                sheet.protectSheet(SystemConstant.EXCEL_PROTECT_KEY);
+            } else {
+                style.setLocked(false);
+            }
+            return style;
+        }
+    }
 }