From 0fee46ceafb013edcd28b59a849057824e7e4ea1 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Thu, 21 May 2020 10:10:51 +0800 Subject: [PATCH] =?UTF-8?q?excel=E5=9B=BD=E9=99=85=E5=8C=96=E5=8A=A0?= =?UTF-8?q?=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/listener/EasyExcelListener.java | 7 +++++-- .../excel/utils/EasyExcelExporter.java | 18 ++++++++++++------ .../track/service/TestCaseService.java | 4 ++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/backend/src/main/java/io/metersphere/excel/listener/EasyExcelListener.java b/backend/src/main/java/io/metersphere/excel/listener/EasyExcelListener.java index a069d73bcd..aa524b267f 100644 --- a/backend/src/main/java/io/metersphere/excel/listener/EasyExcelListener.java +++ b/backend/src/main/java/io/metersphere/excel/listener/EasyExcelListener.java @@ -32,8 +32,11 @@ public abstract class EasyExcelListener extends AnalysisEventListener { public EasyExcelListener(){ Type type = getClass().getGenericSuperclass(); this.clazz = (Class) ((ParameterizedType) type).getActualTypeArguments()[0]; - this.easyExcelI18nTranslator = new EasyExcelI18nTranslator(this.clazz); - this.easyExcelI18nTranslator.translateExcelProperty(); + //防止多线程修改运行时类注解后,saveOriginalExcelProperty保存的是修改后的值 + synchronized (EasyExcelI18nTranslator.class) { + this.easyExcelI18nTranslator = new EasyExcelI18nTranslator(this.clazz); + this.easyExcelI18nTranslator.translateExcelProperty(); + } } /** diff --git a/backend/src/main/java/io/metersphere/excel/utils/EasyExcelExporter.java b/backend/src/main/java/io/metersphere/excel/utils/EasyExcelExporter.java index c2896048a9..7f2d4858fc 100644 --- a/backend/src/main/java/io/metersphere/excel/utils/EasyExcelExporter.java +++ b/backend/src/main/java/io/metersphere/excel/utils/EasyExcelExporter.java @@ -24,17 +24,24 @@ public class EasyExcelExporter { EasyExcelI18nTranslator easyExcelI18nTranslator; - public EasyExcelExporter() { - easyExcelI18nTranslator = new EasyExcelI18nTranslator(TestCaseExcelData.class); - easyExcelI18nTranslator.translateExcelProperty(); + private Class clazz; + + public EasyExcelExporter(Class clazz) { + this.clazz = clazz; + //防止多线程修改运行时类注解后,saveOriginalExcelProperty保存的是修改后的值 + synchronized (EasyExcelI18nTranslator.class) { + easyExcelI18nTranslator = new EasyExcelI18nTranslator(clazz); + easyExcelI18nTranslator.translateExcelProperty(); + } + } - public void export(HttpServletResponse response, Class clazz, List data, String fileName, String sheetName) { + public void export(HttpServletResponse response, List data, String fileName, String sheetName) { response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); try { response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx"); - EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(data); + EasyExcel.write(response.getOutputStream(), this.clazz).sheet(sheetName).doWrite(data); } catch (UnsupportedEncodingException e) { LogUtil.error(e.getMessage(), e); throw new ExcelException("Utf-8 encoding is not supported"); @@ -42,7 +49,6 @@ public class EasyExcelExporter { LogUtil.error(e.getMessage(), e); throw new ExcelException("IO exception"); } - } public void close() { diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java index f71b47396c..324f3b757f 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java @@ -221,8 +221,8 @@ public class TestCaseService { public void testCaseTemplateExport(HttpServletResponse response) { EasyExcelExporter easyExcelExporter = null; try { - easyExcelExporter = new EasyExcelExporter(); - easyExcelExporter.export(response, TestCaseExcelData.class, generateExportTemplate(), + easyExcelExporter = new EasyExcelExporter(TestCaseExcelData.class); + easyExcelExporter.export(response, generateExportTemplate(), Translator.get("test_case_import_template_name"), Translator.get("test_case_import_template_sheet")); } catch (Exception e) { MSException.throwException(e);