Procházet zdrojové kódy

考场导出接口

wangliang před 5 roky
rodič
revize
7ee214f5b0

+ 32 - 3
themis-business/src/main/java/com/qmth/themis/business/templete/TaskExportCommon.java

@@ -12,10 +12,11 @@ import com.qmth.themis.business.enums.UploadFileEnum;
 import com.qmth.themis.business.service.TBAttachmentService;
 import com.qmth.themis.business.service.TBTaskHistoryService;
 import com.qmth.themis.business.util.OssUtil;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.Row;
 
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
+import java.io.*;
 import java.math.BigDecimal;
 import java.time.LocalDateTime;
 import java.util.*;
@@ -91,6 +92,34 @@ public class TaskExportCommon {
         return file;
     }
 
+    /**
+     * oss上传
+     *
+     * @param file
+     * @throws FileNotFoundException
+     */
+    public void ossUpload(File file) throws FileNotFoundException {
+        boolean oss = (boolean) this.getOssEnv().get(SystemConstant.OSS);
+        if (oss) {//上传至oss
+            this.ossUtil.ossUpload(this.ossEnv, file.getPath(), new FileInputStream(file));
+            file.delete();
+        }
+    }
+
+    /**
+     * 创建excel列
+     *
+     * @param sxssfRow
+     * @param cellIndex
+     * @param excelValue
+     * @param style
+     */
+    public void createExcelCell(Row sxssfRow, int cellIndex, Object excelValue, CellStyle style) {
+        Cell cell = sxssfRow.createCell(cellIndex);
+        cell.setCellValue((String) excelValue);
+        cell.setCellStyle(style);
+    }
+
     /**
      * 写入txt文件
      *

+ 8 - 23
themis-business/src/main/java/com/qmth/themis/business/templete/impl/TaskRoomCodeExportTemplete.java

@@ -19,7 +19,6 @@ import org.slf4j.LoggerFactory;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.lang.reflect.Field;
@@ -52,7 +51,6 @@ public class TaskRoomCodeExportTemplete implements TaskExportTemplete {
         List<String> txtList = new ArrayList();
         FileOutputStream fos = null;
         SXSSFWorkbook wb = null;
-        boolean oss = (boolean) taskExportCommon.getOssEnv().get(SystemConstant.OSS);
         File file = null;
         int y = 0, min = 0;
         try {
@@ -85,25 +83,15 @@ public class TaskRoomCodeExportTemplete implements TaskExportTemplete {
                 List subList = roomCodeExportDtoList.subList(min, max);
                 //绘制数据
                 for (; y < subList.size(); y++) {
-                    RoomCodeExportDto roomCodeExportDto = roomCodeExportDtoList.get(y);
+                    RoomCodeExportDto roomCodeExportDto = (RoomCodeExportDto) subList.get(y);
                     int firstRow = y + 1;
                     cellIndex = 0;
-                    Row hssfRow = sheet.createRow(firstRow);
-
-                    Cell cell = hssfRow.createCell(cellIndex);
-                    cell.setCellValue(roomCodeExportDto.getRoomCode());
-                    cell.setCellStyle(style);
-                    cellIndex++;
-
-                    cell = hssfRow.createCell(cellIndex);
-                    cell.setCellValue(roomCodeExportDto.getRoomName());
-                    cell.setCellStyle(style);
-                    cellIndex++;
-
-                    cell = hssfRow.createCell(cellIndex);
-                    cell.setCellValue(roomCodeExportDto.getName());
-                    cell.setCellStyle(style);
-                    cellIndex++;
+                    Row sxssfRow = sheet.createRow(firstRow);
+                    for (Field field : fields) {
+                        field.setAccessible(true);
+                        taskExportCommon.createExcelCell(sxssfRow, cellIndex, field.get(roomCodeExportDto), style);
+                        cellIndex++;
+                    }
                 }
                 txtList = taskExportCommon.progress(max, min, size, txtList);
                 if (max == size) {
@@ -119,10 +107,7 @@ public class TaskRoomCodeExportTemplete implements TaskExportTemplete {
             //写入excel并上传附件
             fos = new FileOutputStream(file);
             wb.write(fos);
-            if (oss) {//上传至oss
-                taskExportCommon.getOssUtil().ossUpload(taskExportCommon.getOssEnv(), file.getPath(), new FileInputStream(file));
-                file.delete();
-            }
+            taskExportCommon.ossUpload(file);
             long end = System.currentTimeMillis();
             log.info("导出考场数据结束,============耗时============:{}秒", (end - start) / 1000);
         } catch (Exception e) {