excel国际化加锁

This commit is contained in:
chenjianxing 2020-05-21 10:10:51 +08:00
parent 8853b39dd7
commit 0fee46ceaf
3 changed files with 19 additions and 10 deletions

View File

@ -32,8 +32,11 @@ public abstract class EasyExcelListener <T> extends AnalysisEventListener<T> {
public EasyExcelListener(){
Type type = getClass().getGenericSuperclass();
this.clazz = (Class<T>) ((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();
}
}
/**

View File

@ -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() {

View File

@ -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);