Selaa lähdekoodia

close io streams.

deason 5 vuotta sitten
vanhempi
commit
6beb8d2a25

+ 76 - 77
examcloud-core-questions-base/src/main/java/cn/com/qmth/examcloud/core/questions/base/excel/ExcelWriter.java

@@ -33,15 +33,18 @@ public class ExcelWriter extends ExcelUtils {
      */
     @SuppressWarnings("resource")
     public void write(String sheetName, Collection<?> dataset, OutputStream out) {
+
         // 声明一个工作薄
-        XSSFWorkbook workbook = new XSSFWorkbook();
-        // 生成一个表格
-        XSSFSheet sheet = workbook.createSheet(sheetName);
+        try (XSSFWorkbook workbook = new XSSFWorkbook();) {
+
+            // 生成一个表格
+            XSSFSheet sheet = workbook.createSheet(sheetName);
 
-        // 设置表格默认列宽度为15个字节
-        sheet.setDefaultColumnWidth((short) 15);
-        // 生成一个样式
-        XSSFCellStyle style = workbook.createCellStyle();
+            // 设置表格默认列宽度为15个字节
+            sheet.setDefaultColumnWidth((short) 15);
+
+            // 生成一个样式
+            XSSFCellStyle style = workbook.createCellStyle();
 
         /*
         // 设置这些样式
@@ -93,87 +96,83 @@ public class ExcelWriter extends ExcelUtils {
         comment.setAuthor("leno");
         */
 
-        List<ColumnSetting> columnSettings = this.getColumnSettings();
-
-        // 产生表格标题行
-        XSSFRow row = sheet.createRow(0);
-        for (short i = 0; i < columnSettings.size(); i++) {
-            XSSFCell cell = row.createCell(i);
-            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);
-            }
-        }
-
-        // 遍历集合数据,产生数据行
-        int index = 0;
-        for (Object obj : dataset) {
-            index++;
-            row = sheet.createRow(index);
+            List<ColumnSetting> columnSettings = this.getColumnSettings();
 
-            // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
+            // 产生表格标题行
+            XSSFRow row = sheet.createRow(0);
             for (short i = 0; i < columnSettings.size(); i++) {
                 XSSFCell cell = row.createCell(i);
-                //cell.setCellStyle(style2);
-                String fieldName = columnSettings.get(i).getFieldName();
-
-                try {
-                    Field field = this.getDataClass().getDeclaredField(fieldName);
-                    field.setAccessible(true);
-                    Object value = field.get(obj);
-                    // 判断值的类型后进行强制类型转换
-                    String textValue;
-                    if (value == null) {
-                        textValue = "";
-                    } else if (value instanceof Boolean) {
-                        boolean bValue = (Boolean) value;
-                        textValue = "是";
-                        if (!bValue) {
-                            textValue = "否";
-                        }
-                    } else if (value instanceof Date) {
-                        Date date = (Date) value;
-                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
-                        textValue = sdf.format(date);
-                    } else {
-                        // 其它数据类型都当作字符串简单处理
-                        textValue = String.valueOf(value);
-                    }
+                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);
+                }
+            }
 
-                    // 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成
-                    if (textValue != null) {
-                        Pattern p = Pattern.compile("^//d+(//.//d+)?$");
-                        Matcher matcher = p.matcher(textValue);
-                        if (matcher.matches()) {
-                            // 是数字当作double处理
-                            cell.setCellValue(Double.parseDouble(textValue));
+            // 遍历集合数据,产生数据行
+            int index = 0;
+            for (Object obj : dataset) {
+                index++;
+                row = sheet.createRow(index);
+
+                // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
+                for (short i = 0; i < columnSettings.size(); i++) {
+                    XSSFCell cell = row.createCell(i);
+                    //cell.setCellStyle(style2);
+                    String fieldName = columnSettings.get(i).getFieldName();
+
+                    try {
+                        Field field = this.getDataClass().getDeclaredField(fieldName);
+                        field.setAccessible(true);
+                        Object value = field.get(obj);
+                        // 判断值的类型后进行强制类型转换
+                        String textValue;
+                        if (value == null) {
+                            textValue = "";
+                        } else if (value instanceof Boolean) {
+                            boolean bValue = (Boolean) value;
+                            textValue = "是";
+                            if (!bValue) {
+                                textValue = "否";
+                            }
+                        } else if (value instanceof Date) {
+                            Date date = (Date) value;
+                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+                            textValue = sdf.format(date);
                         } else {
-                            XSSFRichTextString richString = new XSSFRichTextString(
-                                    textValue);
+                            // 其它数据类型都当作字符串简单处理
+                            textValue = String.valueOf(value);
+                        }
 
-                            cell.setCellValue(richString);
+                        // 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成
+                        if (textValue != null) {
+                            Pattern p = Pattern.compile("^//d+(//.//d+)?$");
+                            Matcher matcher = p.matcher(textValue);
+                            if (matcher.matches()) {
+                                // 是数字当作double处理
+                                cell.setCellValue(Double.parseDouble(textValue));
+                            } else {
+                                XSSFRichTextString richString = new XSSFRichTextString(
+                                        textValue);
+
+                                cell.setCellValue(richString);
+                            }
+                        } else {
+                            textValue = "";
                         }
-                    } else {
-                        textValue = "";
+                    } catch (NoSuchFieldException e) {
+                        log.error(e.getMessage(), e);
+                    } catch (IllegalAccessException e) {
+                        log.error(e.getMessage(), e);
+                    } catch (SecurityException e) {
+                        log.error(e.getMessage(), e);
+                    } catch (IllegalArgumentException e) {
+                        log.error(e.getMessage(), e);
                     }
-                } catch (NoSuchFieldException e) {
-                    log.error(e.getMessage(), e);
-                } catch (IllegalAccessException e) {
-                    log.error(e.getMessage(), e);
-                } catch (SecurityException e) {
-                    log.error(e.getMessage(), e);
-                } catch (IllegalArgumentException e) {
-                    log.error(e.getMessage(), e);
-                } finally {
-                    // 清理资源
                 }
             }
 
-        }
-
-        try {
             workbook.write(out);
         } catch (IOException e) {
             log.error(e.getMessage(), e);

+ 14 - 10
examcloud-core-questions-base/src/main/java/cn/com/qmth/examcloud/core/questions/base/excel/ImportUtils.java

@@ -5,9 +5,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.OutputStream;
+import java.io.*;
 
 /**
  * Created by songyue on 17/7/14.
@@ -45,13 +43,19 @@ public final class ImportUtils {
      * @param file
      * @return
      */
-    public static File getUploadFile(MultipartFile file) throws Exception {
-        String fileName = file.getOriginalFilename();
-        File tempFile = new File(TEMP_FILE_IMP + fileName);
-        OutputStream os = new FileOutputStream(tempFile);
-        IOUtils.copyLarge(file.getInputStream(), os);
-        IOUtils.closeQuietly(os);
+    public static File getUploadFile(MultipartFile file) {
+        File tempFile = new File(TEMP_FILE_IMP + file.getOriginalFilename());
+
+        try (OutputStream os = new FileOutputStream(tempFile);
+             InputStream is = file.getInputStream();) {
+            IOUtils.copyLarge(is, os);
+        } catch (FileNotFoundException e) {
+            logger.error(e.getMessage(), e);
+        } catch (IOException e) {
+            logger.error(e.getMessage(), e);
+        }
+
         return tempFile;
     }
 
-}
+}

+ 5 - 8
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/export/ExportPaperAbstractService.java

@@ -910,13 +910,10 @@ public abstract class ExportPaperAbstractService {
         Gson gson = new Gson();
         String strJSON = gson.toJson(computerTestPaper);
         strJSON = CommonUtils.replaceUnicodeStr(strJSON);
+
         //生成文件流写入JSON文件
-        FileOutputStream outputStream;
-        try {
-            outputStream = new FileOutputStream(file);
-            byte b[] = strJSON.getBytes();
-            outputStream.write(b);
-            outputStream.close();
+        try (FileOutputStream outputStream = new FileOutputStream(file);) {
+            outputStream.write(strJSON.getBytes());
         } catch (FileNotFoundException e) {
             log.error(e.getMessage(), e);
         } catch (IOException e) {
@@ -1038,7 +1035,7 @@ public abstract class ExportPaperAbstractService {
         List<PaperDetailExp> objectiveDetails = getAllObjectiveDetails(paperExp);
         //根据试卷结构导出设置中的数量补齐客观题
         List<PaperDetailExp> paperDetailExps = fillObjectiveQuestions(objectiveDetails, questionTypeNums);
-        List<ObjectiveQuestionStructure> objectiveQuestionStructureList = new ArrayList<ObjectiveQuestionStructure>();
+        List<ObjectiveQuestionStructure> objectiveQuestionStructureList = new ArrayList<>();
 
         //String paperType = findPaperType(extractConfig,paperExp.getId());
         String paperType = examPaper.getGroupCode();
@@ -1077,7 +1074,7 @@ public abstract class ExportPaperAbstractService {
     protected void exportSubjectiveQuestionStructures(PaperExp paperExp, ExtractConfig extractConfig, User accessUser, String currNum, ExamPaper examPaper) {
         String subjectiveFileName = currNum + ExamFileType.PAPER_STRUCTURE_SUBJECTIVE.name() + EXCEL_SUFFIX;
         List<PaperDetailExp> subjectiveDetails = getAllSubjectiveDetails(paperExp);
-        List<SubjectiveQuestionStructure> subjectiveQuestionStructureList = new ArrayList<SubjectiveQuestionStructure>();
+        List<SubjectiveQuestionStructure> subjectiveQuestionStructureList = new ArrayList<>();
         //String paperType = findPaperType(extractConfig,paperExp.getId());
         String paperType = examPaper.getGroupCode();
         for (PaperDetailExp paperDetailExp : subjectiveDetails) {

+ 285 - 245
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/temp/DdExcelService.java

@@ -11,6 +11,7 @@ import cn.com.qmth.examcloud.core.questions.service.temp.vo.DdExcelDto;
 import cn.com.qmth.examcloud.core.questions.service.temp.vo.DdPaperDto;
 import cn.com.qmth.examcloud.core.questions.service.temp.vo.DdPaperStructDto;
 import cn.com.qmth.examcloud.core.questions.service.temp.vo.SubQuestionDto;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
@@ -25,6 +26,7 @@ import org.springframework.stereotype.Service;
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.OutputStream;
 import java.util.*;
 
@@ -60,7 +62,7 @@ public class DdExcelService {
         //查询所有课程
         long startCourseTime = System.currentTimeMillis();
         //List<Course> courses = courseRepo.findByOrgId(orgId);
-        List<Course> courses = new ArrayList<Course>();
+        List<Course> courses = new ArrayList<>();
         long endCourseTime = System.currentTimeMillis();
         log.debug("查询课程耗时:" + (endCourseTime - startCourseTime) + "ms");
         if (courses != null && courses.size() > 0) {
@@ -140,7 +142,7 @@ public class DdExcelService {
      * 导出按大题名称统计分布
      */
     @SuppressWarnings("resource")
-    public void exporExcel(String orgId) throws Exception {
+    public void exporExcel(String orgId) {
         //导出Excel集合
         List<DdExcelDto> ddExcelDtos = new ArrayList<>();
         //查询所有课程
@@ -186,199 +188,209 @@ public class DdExcelService {
                 i++;
             }
         }
-        Map<String, Long> otherMap = new HashMap<>();
+
         //生成Excel对象
         File ddExcelFile = new File(exportExcelPath);
-        Workbook workBook = new XSSFWorkbook(ddExcelFile);
-        //获取第一个工作页
-        Sheet sheet = workBook.getSheetAt(0);
-        log.debug(sheet.getSheetName());
-        //向Excle中写入数据,从第2行开始
-        for (int i = 0; i < ddExcelDtos.size(); i++) {
-            //创建一行:从第1行开始,跳过表头
-            Row row = sheet.createRow(i + 1);
-            DdExcelDto ddExcelDto = ddExcelDtos.get(i);
-            //每列赋值
-            row.createCell(0).setCellValue(ddExcelDto.getCourseName());
-            row.createCell(1).setCellValue(ddExcelDto.getCourseCode());
-            if (ddExcelDto.getMap() != null && ddExcelDto.getMap().size() > 0) {
-                if (ddExcelDto.getMap().get("单选题") == null) {
-                    row.createCell(2).setCellValue(0);
-                } else {
-                    row.createCell(2).setCellValue(ddExcelDto.getMap().get("单选题"));
-                }
+        File file = new File(xianExcelPath);
 
-                if (ddExcelDto.getMap().get("多选题") == null) {
-                    row.createCell(3).setCellValue(0);
-                } else {
-                    row.createCell(3).setCellValue(ddExcelDto.getMap().get("多选题"));
-                }
+        try (Workbook workBook = new XSSFWorkbook(ddExcelFile);
+             OutputStream out = new FileOutputStream(file);) {
+
+            //获取第一个工作页
+            Sheet sheet = workBook.getSheetAt(0);
+            log.debug(sheet.getSheetName());
+
+            //向Excel中写入数据,从第2行开始
+            for (int i = 0; i < ddExcelDtos.size(); i++) {
+                //创建一行:从第1行开始,跳过表头
+                Row row = sheet.createRow(i + 1);
+                DdExcelDto ddExcelDto = ddExcelDtos.get(i);
+                //每列赋值
+                row.createCell(0).setCellValue(ddExcelDto.getCourseName());
+                row.createCell(1).setCellValue(ddExcelDto.getCourseCode());
+
+                if (ddExcelDto.getMap() != null && ddExcelDto.getMap().size() > 0) {
+                    if (ddExcelDto.getMap().get("单选题") == null) {
+                        row.createCell(2).setCellValue(0);
+                    } else {
+                        row.createCell(2).setCellValue(ddExcelDto.getMap().get("单选题"));
+                    }
 
-                if (ddExcelDto.getMap().get("填空题") == null) {
-                    row.createCell(4).setCellValue(0);
-                } else {
-                    row.createCell(4).setCellValue(ddExcelDto.getMap().get("填空题"));
-                }
+                    if (ddExcelDto.getMap().get("多选题") == null) {
+                        row.createCell(3).setCellValue(0);
+                    } else {
+                        row.createCell(3).setCellValue(ddExcelDto.getMap().get("多选题"));
+                    }
 
-                if (ddExcelDto.getMap().get("选词填空") == null) {
-                    row.createCell(5).setCellValue(0);
-                } else {
-                    row.createCell(5).setCellValue(ddExcelDto.getMap().get("选词填空"));
-                }
+                    if (ddExcelDto.getMap().get("填空") == null) {
+                        row.createCell(4).setCellValue(0);
+                    } else {
+                        row.createCell(4).setCellValue(ddExcelDto.getMap().get("填空题"));
+                    }
 
-                if (ddExcelDto.getMap().get("完型填空") == null) {
-                    row.createCell(6).setCellValue(0);
-                } else {
-                    row.createCell(6).setCellValue(ddExcelDto.getMap().get("完型填空"));
-                }
+                    if (ddExcelDto.getMap().get("选词填空") == null) {
+                        row.createCell(5).setCellValue(0);
+                    } else {
+                        row.createCell(5).setCellValue(ddExcelDto.getMap().get("选词填空"));
+                    }
 
-                if (ddExcelDto.getMap().get("判断题") == null) {
-                    row.createCell(7).setCellValue(0);
-                } else {
-                    row.createCell(7).setCellValue(ddExcelDto.getMap().get("判断题"));
-                }
+                    if (ddExcelDto.getMap().get("完型填空") == null) {
+                        row.createCell(6).setCellValue(0);
+                    } else {
+                        row.createCell(6).setCellValue(ddExcelDto.getMap().get("完型填空"));
+                    }
 
-                if (ddExcelDto.getMap().get("阅读理解") == null) {
-                    row.createCell(8).setCellValue(0);
-                } else {
-                    row.createCell(8).setCellValue(ddExcelDto.getMap().get("阅读理解"));
-                }
+                    if (ddExcelDto.getMap().get("判断题") == null) {
+                        row.createCell(7).setCellValue(0);
+                    } else {
+                        row.createCell(7).setCellValue(ddExcelDto.getMap().get("判断题"));
+                    }
 
-                if (ddExcelDto.getMap().get("问答题") == null) {
-                    row.createCell(9).setCellValue(0);
-                } else {
-                    row.createCell(9).setCellValue(ddExcelDto.getMap().get("问答题"));
-                }
+                    if (ddExcelDto.getMap().get("阅读理解") == null) {
+                        row.createCell(8).setCellValue(0);
+                    } else {
+                        row.createCell(8).setCellValue(ddExcelDto.getMap().get("阅读理解"));
+                    }
 
-                if (ddExcelDto.getMap().get("名词解释") == null) {
-                    row.createCell(10).setCellValue(0);
-                } else {
-                    row.createCell(10).setCellValue(ddExcelDto.getMap().get("名词解释"));
-                }
+                    if (ddExcelDto.getMap().get("问答题") == null) {
+                        row.createCell(9).setCellValue(0);
+                    } else {
+                        row.createCell(9).setCellValue(ddExcelDto.getMap().get("问答题"));
+                    }
 
-                //额外大题名称
-                if (ddExcelDto.getMap().get("案例分析") == null) {
-                    row.createCell(11).setCellValue(0);
-                } else {
-                    row.createCell(11).setCellValue(ddExcelDto.getMap().get("案例分析"));
-                }
+                    if (ddExcelDto.getMap().get("名词解释") == null) {
+                        row.createCell(10).setCellValue(0);
+                    } else {
+                        row.createCell(10).setCellValue(ddExcelDto.getMap().get("名词解释"));
+                    }
 
-                if (ddExcelDto.getMap().get("作文题") == null) {
-                    row.createCell(12).setCellValue(0);
-                } else {
-                    row.createCell(12).setCellValue(ddExcelDto.getMap().get("作文题"));
-                }
+                    //额外大题名称
+                    if (ddExcelDto.getMap().get("案例分析") == null) {
+                        row.createCell(11).setCellValue(0);
+                    } else {
+                        row.createCell(11).setCellValue(ddExcelDto.getMap().get("案例分析"));
+                    }
 
-                if (ddExcelDto.getMap().get("交际英语") == null) {
-                    row.createCell(13).setCellValue(0);
-                } else {
-                    row.createCell(13).setCellValue(ddExcelDto.getMap().get("交际英语"));
-                }
+                    if (ddExcelDto.getMap().get("作文题") == null) {
+                        row.createCell(12).setCellValue(0);
+                    } else {
+                        row.createCell(12).setCellValue(ddExcelDto.getMap().get("作文题"));
+                    }
 
-                if (ddExcelDto.getMap().get("简答题") == null) {
-                    row.createCell(14).setCellValue(0);
-                } else {
-                    row.createCell(14).setCellValue(ddExcelDto.getMap().get("简答题"));
-                }
+                    if (ddExcelDto.getMap().get("交际英语") == null) {
+                        row.createCell(13).setCellValue(0);
+                    } else {
+                        row.createCell(13).setCellValue(ddExcelDto.getMap().get("交际英语"));
+                    }
 
-                if (ddExcelDto.getMap().get("一、单选题") == null) {
-                    row.createCell(15).setCellValue(0);
-                } else {
-                    row.createCell(15).setCellValue(ddExcelDto.getMap().get("一、单选题"));
-                }
+                    if (ddExcelDto.getMap().get("简答题") == null) {
+                        row.createCell(14).setCellValue(0);
+                    } else {
+                        row.createCell(14).setCellValue(ddExcelDto.getMap().get("简答题"));
+                    }
 
-                if (ddExcelDto.getMap().get("名称解释") == null) {
-                    row.createCell(16).setCellValue(0);
-                } else {
-                    row.createCell(16).setCellValue(ddExcelDto.getMap().get("名称解释"));
-                }
+                    if (ddExcelDto.getMap().get("一、单选题") == null) {
+                        row.createCell(15).setCellValue(0);
+                    } else {
+                        row.createCell(15).setCellValue(ddExcelDto.getMap().get("一、单选题"));
+                    }
 
-                if (ddExcelDto.getMap().get("综合题") == null) {
-                    row.createCell(17).setCellValue(0);
-                } else {
-                    row.createCell(17).setCellValue(ddExcelDto.getMap().get("综合题"));
-                }
+                    if (ddExcelDto.getMap().get("名称解释") == null) {
+                        row.createCell(16).setCellValue(0);
+                    } else {
+                        row.createCell(16).setCellValue(ddExcelDto.getMap().get("名称解释"));
+                    }
 
-                if (ddExcelDto.getMap().get("设计题") == null) {
-                    row.createCell(18).setCellValue(0);
-                } else {
-                    row.createCell(18).setCellValue(ddExcelDto.getMap().get("设计题"));
-                }
+                    if (ddExcelDto.getMap().get("综合题") == null) {
+                        row.createCell(17).setCellValue(0);
+                    } else {
+                        row.createCell(17).setCellValue(ddExcelDto.getMap().get("综合题"));
+                    }
 
-                if (ddExcelDto.getMap().get("二、名词解释") == null) {
-                    row.createCell(19).setCellValue(0);
-                } else {
-                    row.createCell(19).setCellValue(ddExcelDto.getMap().get("二、名词解释"));
-                }
+                    if (ddExcelDto.getMap().get("设计题") == null) {
+                        row.createCell(18).setCellValue(0);
+                    } else {
+                        row.createCell(18).setCellValue(ddExcelDto.getMap().get("设计题"));
+                    }
 
-                if (ddExcelDto.getMap().get("分析题") == null) {
-                    row.createCell(20).setCellValue(0);
-                } else {
-                    row.createCell(20).setCellValue(ddExcelDto.getMap().get("分析题"));
-                }
+                    if (ddExcelDto.getMap().get("二、名词解释") == null) {
+                        row.createCell(19).setCellValue(0);
+                    } else {
+                        row.createCell(19).setCellValue(ddExcelDto.getMap().get("二、名词解释"));
+                    }
 
-                if (ddExcelDto.getMap().get("英汉翻译") == null) {
-                    row.createCell(21).setCellValue(0);
-                } else {
-                    row.createCell(21).setCellValue(ddExcelDto.getMap().get("英汉翻译"));
-                }
+                    if (ddExcelDto.getMap().get("分析题") == null) {
+                        row.createCell(20).setCellValue(0);
+                    } else {
+                        row.createCell(20).setCellValue(ddExcelDto.getMap().get("分析题"));
+                    }
 
-                if (ddExcelDto.getMap().get("作文") == null) {
-                    row.createCell(22).setCellValue(0);
-                } else {
-                    row.createCell(22).setCellValue(ddExcelDto.getMap().get("作文"));
-                }
+                    if (ddExcelDto.getMap().get("英汉翻译") == null) {
+                        row.createCell(21).setCellValue(0);
+                    } else {
+                        row.createCell(21).setCellValue(ddExcelDto.getMap().get("英汉翻译"));
+                    }
 
-                if (ddExcelDto.getMap().get("二、计算题") == null) {
-                    row.createCell(23).setCellValue(0);
-                } else {
-                    row.createCell(23).setCellValue(ddExcelDto.getMap().get("二、计算题"));
-                }
+                    if (ddExcelDto.getMap().get("作文") == null) {
+                        row.createCell(22).setCellValue(0);
+                    } else {
+                        row.createCell(22).setCellValue(ddExcelDto.getMap().get("作文"));
+                    }
 
-                if (ddExcelDto.getMap().get("作图题") == null) {
-                    row.createCell(24).setCellValue(0);
-                } else {
-                    row.createCell(24).setCellValue(ddExcelDto.getMap().get("作图题"));
-                }
+                    if (ddExcelDto.getMap().get("二、计算题") == null) {
+                        row.createCell(23).setCellValue(0);
+                    } else {
+                        row.createCell(23).setCellValue(ddExcelDto.getMap().get("二、计算题"));
+                    }
 
-                if (ddExcelDto.getMap().get("论述题") == null) {
-                    row.createCell(25).setCellValue(0);
-                } else {
-                    row.createCell(25).setCellValue(ddExcelDto.getMap().get("论述题"));
-                }
+                    if (ddExcelDto.getMap().get("作图题") == null) {
+                        row.createCell(24).setCellValue(0);
+                    } else {
+                        row.createCell(24).setCellValue(ddExcelDto.getMap().get("作图题"));
+                    }
 
-                if (ddExcelDto.getMap().get("三、计算题") == null) {
-                    row.createCell(26).setCellValue(0);
-                } else {
-                    row.createCell(26).setCellValue(ddExcelDto.getMap().get("三、计算题"));
-                }
+                    if (ddExcelDto.getMap().get("论述题") == null) {
+                        row.createCell(25).setCellValue(0);
+                    } else {
+                        row.createCell(25).setCellValue(ddExcelDto.getMap().get("论述题"));
+                    }
 
-                if (ddExcelDto.getMap().get("计算题") == null) {
-                    row.createCell(27).setCellValue(0);
-                } else {
-                    row.createCell(27).setCellValue(ddExcelDto.getMap().get("计算题"));
-                }
+                    if (ddExcelDto.getMap().get("三、计算题") == null) {
+                        row.createCell(26).setCellValue(0);
+                    } else {
+                        row.createCell(26).setCellValue(ddExcelDto.getMap().get("三、计算题"));
+                    }
 
-                if (ddExcelDto.getMap().get("算法设计题") == null) {
-                    row.createCell(28).setCellValue(0);
-                } else {
-                    row.createCell(28).setCellValue(ddExcelDto.getMap().get("算法设计题"));
-                }
+                    if (ddExcelDto.getMap().get("计算题") == null) {
+                        row.createCell(27).setCellValue(0);
+                    } else {
+                        row.createCell(27).setCellValue(ddExcelDto.getMap().get("计算题"));
+                    }
+
+                    if (ddExcelDto.getMap().get("算法设计题") == null) {
+                        row.createCell(28).setCellValue(0);
+                    } else {
+                        row.createCell(28).setCellValue(ddExcelDto.getMap().get("算法设计题"));
+                    }
+
+                    if (ddExcelDto.getMap().get("翻译题") == null) {
+                        row.createCell(29).setCellValue(0);
+                    } else {
+                        row.createCell(29).setCellValue(ddExcelDto.getMap().get("翻译题"));
+                    }
 
-                if (ddExcelDto.getMap().get("翻译题") == null) {
-                    row.createCell(29).setCellValue(0);
                 } else {
-                    row.createCell(29).setCellValue(ddExcelDto.getMap().get("翻译题"));
+                    log.debug("课程:" + ddExcelDto.getCourseName());
                 }
-
-            } else {
-                log.debug("课程:" + ddExcelDto.getCourseName());
             }
+
+            workBook.write(out);
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+        } catch (InvalidFormatException e) {
+            log.error(e.getMessage(), e);
         }
-        File file = new File(xianExcelPath);
-        OutputStream out = new FileOutputStream(file);
-        workBook.write(out);
-        out.close();
+
         log.debug("结束...");
     }
 
@@ -389,7 +401,7 @@ public class DdExcelService {
     public void exportPaperExcel(String orgId) throws Exception {
         log.debug("开始导出试卷具体信息。。。。");
         //导出Excel对象集合
-        List<DdPaperDto> excelDtoList = new ArrayList<DdPaperDto>();
+        List<DdPaperDto> excelDtoList = new ArrayList<>();
         //根据课程查询试卷
         Query query = new Query();
         query.addCriteria(Criteria.where("orgId").is(orgId));
@@ -454,27 +466,37 @@ public class DdExcelService {
         		i++;
 			}
 		}*/
+
         //生成Excel对象
         File ddExcelFile = new File(paperExcelPath);
-        Workbook workBook = new XSSFWorkbook(ddExcelFile);
-        //获取第一个工作页
-        Sheet sheet = workBook.getSheetAt(0);
-        //向Excle中写入数据,从第2行开始
-        for (int i = 0; i < excelDtoList.size(); i++) {
-            //创建一行:从第1行开始,跳过表头
-            Row row = sheet.createRow(i + 1);
-            DdPaperDto excelDto = excelDtoList.get(i);
-            //每列赋值
-            row.createCell(0).setCellValue(excelDto.getCode());
-            row.createCell(1).setCellValue(excelDto.getName());
-            row.createCell(2).setCellValue(excelDto.getPaperName());
-            row.createCell(3).setCellValue(excelDto.getDetailName());
-            row.createCell(4).setCellValue(excelDto.getNumber());
-        }
         File file = new File(paperExcelPathNew);
-        OutputStream out = new FileOutputStream(file);
-        workBook.write(out);
-        out.close();
+
+        try (Workbook workBook = new XSSFWorkbook(ddExcelFile);
+             OutputStream out = new FileOutputStream(file);) {
+
+            //获取第一个工作页
+            Sheet sheet = workBook.getSheetAt(0);
+
+            //向Excel中写入数据,从第2行开始
+            for (int i = 0; i < excelDtoList.size(); i++) {
+                //创建一行:从第1行开始,跳过表头
+                Row row = sheet.createRow(i + 1);
+                DdPaperDto excelDto = excelDtoList.get(i);
+                //每列赋值
+                row.createCell(0).setCellValue(excelDto.getCode());
+                row.createCell(1).setCellValue(excelDto.getName());
+                row.createCell(2).setCellValue(excelDto.getPaperName());
+                row.createCell(3).setCellValue(excelDto.getDetailName());
+                row.createCell(4).setCellValue(excelDto.getNumber());
+            }
+
+            workBook.write(out);
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+        } catch (InvalidFormatException e) {
+            log.error(e.getMessage(), e);
+        }
+
         log.debug("结束...");
     }
 
@@ -488,7 +510,7 @@ public class DdExcelService {
         List<DdPaperStructDto> excelDtoList = new ArrayList<>();
         //查询所有课程
         List<Course> courses = courseRepo.findByOrgId(orgId);
-        //List<Course> courses = new ArrayList<Course>();
+        //List<Course> courses = new ArrayList<>();
         log.debug("查询课程总数:" + courses.size());
 
         if (courses != null && courses.size() > 0) {
@@ -529,28 +551,38 @@ public class DdExcelService {
                 i++;
             }
         }
+
         //生成Excel对象
         File ddExcelFile = new File(paperStructExcelPath);
-        Workbook workBook = new XSSFWorkbook(ddExcelFile);
-        //获取第一个工作页
-        Sheet sheet = workBook.getSheetAt(0);
-        //向Excle中写入数据,从第2行开始
-        for (int i = 0; i < excelDtoList.size(); i++) {
-            //创建一行:从第1行开始,跳过表头
-            Row row = sheet.createRow(i + 1);
-            DdPaperStructDto excelDto = excelDtoList.get(i);
-            //每列赋值
-            row.createCell(0).setCellValue(excelDto.getCode());
-            row.createCell(1).setCellValue(excelDto.getName());
-            row.createCell(2).setCellValue(excelDto.getDetailName());
-            row.createCell(3).setCellValue(excelDto.getQuesNames());
-            row.createCell(4).setCellValue(excelDto.getNumber());
-            row.createCell(5).setCellValue(excelDto.getQuesType());
-        }
         File file = new File(paperStructExcelPathNew);
-        OutputStream out = new FileOutputStream(file);
-        workBook.write(out);
-        out.close();
+
+        try (Workbook workBook = new XSSFWorkbook(ddExcelFile);
+             OutputStream out = new FileOutputStream(file);) {
+
+            //获取第一个工作页
+            Sheet sheet = workBook.getSheetAt(0);
+
+            //向Excel中写入数据,从第2行开始
+            for (int i = 0; i < excelDtoList.size(); i++) {
+                //创建一行:从第1行开始,跳过表头
+                Row row = sheet.createRow(i + 1);
+                DdPaperStructDto excelDto = excelDtoList.get(i);
+                //每列赋值
+                row.createCell(0).setCellValue(excelDto.getCode());
+                row.createCell(1).setCellValue(excelDto.getName());
+                row.createCell(2).setCellValue(excelDto.getDetailName());
+                row.createCell(3).setCellValue(excelDto.getQuesNames());
+                row.createCell(4).setCellValue(excelDto.getNumber());
+                row.createCell(5).setCellValue(excelDto.getQuesType());
+            }
+
+            workBook.write(out);
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+        } catch (InvalidFormatException e) {
+            log.error(e.getMessage(), e);
+        }
+
         log.debug("结束...");
     }
 
@@ -625,52 +657,60 @@ public class DdExcelService {
 
         //生成Excel对象
         File ddExcelFile = new File(quesExcelPath);
-        Workbook workBook = new XSSFWorkbook(ddExcelFile);
-        //获取第一个工作页
-        Sheet sheet = workBook.getSheetAt(0);
-
-        //标题
-        Row titleRow = sheet.createRow(0);
-        titleRow.createCell(0).setCellValue("课程代码");
-        titleRow.createCell(1).setCellValue("课程名称");
-        titleRow.createCell(2).setCellValue("试卷名称");
-        titleRow.createCell(3).setCellValue("原始试卷ID");
-        titleRow.createCell(4).setCellValue("大题号");
-        titleRow.createCell(5).setCellValue("小题号");
-        titleRow.createCell(6).setCellValue("标准分");
-        titleRow.createCell(7).setCellValue("大题名称");
-        titleRow.createCell(8).setCellValue("题干");
-        titleRow.createCell(9).setCellValue("题干(含标签)");
-        titleRow.createCell(10).setCellValue("标答");
-        titleRow.createCell(11).setCellValue("标答(含标签)");
-
-        //向Excle中写入数据,从第2行开始
-        for (int i = 0; i < subjectQuestions.size(); i++) {
-            //创建一行:从第1行开始,跳过表头
-            Row row = sheet.createRow(i + 1);
-            SubQuestionDto dto = subjectQuestions.get(i);
-            //每列赋值
-            row.createCell(0).setCellValue(dto.getCourseCode());
-            row.createCell(1).setCellValue(dto.getCourseName());
-            row.createCell(2).setCellValue(dto.getPaperName());
-            row.createCell(3).setCellValue(dto.getPaperId());
-            row.createCell(4).setCellValue(dto.getdNumber());
-            row.createCell(5).setCellValue(dto.getNumber());
-            row.createCell(6).setCellValue(dto.getScore());
-            row.createCell(7).setCellValue(dto.getDetailName());
-            row.createCell(8).setCellValue(dto.getClearBody());
-            row.createCell(9).setCellValue(dto.getBody());
-            if (dto.getClearAnswer().length() < 32767) {
-                row.createCell(10).setCellValue(dto.getClearAnswer());
-            }
-            if (dto.getAnswer().length() < 32767) {
-                row.createCell(11).setCellValue(dto.getAnswer());
+        File file = new File(quesExcelPathNew);
+
+        try (Workbook workBook = new XSSFWorkbook(ddExcelFile);
+             OutputStream out = new FileOutputStream(file);) {
+
+            //获取第一个工作页
+            Sheet sheet = workBook.getSheetAt(0);
+
+            //标题
+            Row titleRow = sheet.createRow(0);
+            titleRow.createCell(0).setCellValue("课程代码");
+            titleRow.createCell(1).setCellValue("课程名称");
+            titleRow.createCell(2).setCellValue("试卷名称");
+            titleRow.createCell(3).setCellValue("原始试卷ID");
+            titleRow.createCell(4).setCellValue("大题号");
+            titleRow.createCell(5).setCellValue("小题号");
+            titleRow.createCell(6).setCellValue("标准分");
+            titleRow.createCell(7).setCellValue("大题名称");
+            titleRow.createCell(8).setCellValue("题干");
+            titleRow.createCell(9).setCellValue("题干(含标签)");
+            titleRow.createCell(10).setCellValue("标答");
+            titleRow.createCell(11).setCellValue("标答(含标签)");
+
+            //向Excel中写入数据,从第2行开始
+            for (int i = 0; i < subjectQuestions.size(); i++) {
+                //创建一行:从第1行开始,跳过表头
+                Row row = sheet.createRow(i + 1);
+                SubQuestionDto dto = subjectQuestions.get(i);
+                //每列赋值
+                row.createCell(0).setCellValue(dto.getCourseCode());
+                row.createCell(1).setCellValue(dto.getCourseName());
+                row.createCell(2).setCellValue(dto.getPaperName());
+                row.createCell(3).setCellValue(dto.getPaperId());
+                row.createCell(4).setCellValue(dto.getdNumber());
+                row.createCell(5).setCellValue(dto.getNumber());
+                row.createCell(6).setCellValue(dto.getScore());
+                row.createCell(7).setCellValue(dto.getDetailName());
+                row.createCell(8).setCellValue(dto.getClearBody());
+                row.createCell(9).setCellValue(dto.getBody());
+                if (dto.getClearAnswer().length() < 32767) {
+                    row.createCell(10).setCellValue(dto.getClearAnswer());
+                }
+                if (dto.getAnswer().length() < 32767) {
+                    row.createCell(11).setCellValue(dto.getAnswer());
+                }
             }
+
+            workBook.write(out);
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+        } catch (InvalidFormatException e) {
+            log.error(e.getMessage(), e);
         }
-        File file = new File(quesExcelPathNew);
-        OutputStream out = new FileOutputStream(file);
-        workBook.write(out);
-        out.close();
+
         log.debug("结束...");
     }