Skip to content

Commit 5697305

Browse files
代码优化
1 parent a71d406 commit 5697305

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

ymate-platform-commons/src/main/java/net/ymate/platform/commons/ExcelFileExportHelper.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -345,30 +345,30 @@ private File doExportCsv(List<String> columnNames, Map<String, ExportColumn> col
345345
return tempFile;
346346
}
347347

348-
private File doExport(String tmplFile, int index, Map<String, Object> data) throws IOException {
348+
private File doExport(JxlsHelper jxlsHelper, String tmplFile, int index, Map<String, Object> data) throws IOException {
349349
String fileType = FileUtils.getExtName(tmplFile).toLowerCase();
350350
if (StringUtils.isBlank(fileType)) {
351351
List<String> fileTypes = Arrays.asList(EXCEL_TYPE_XLS, EXCEL_TYPE_XLSX);
352352
for (String type : fileTypes) {
353353
try (InputStream templateStream = doGetTemplateFileInputStream(String.format("%s.%s", tmplFile, type))) {
354354
if (templateStream != null) {
355-
return doExport(templateStream, type, index, data);
355+
return doExport(jxlsHelper, templateStream, type, index, data);
356356
}
357357
}
358358
}
359359
} else if (StringUtils.endsWithAny(fileType, EXCEL_TYPE_XLS, EXCEL_TYPE_XLSX)) {
360360
try (InputStream templateStream = doGetTemplateFileInputStream(tmplFile)) {
361-
return doExport(templateStream, fileType, index, data);
361+
return doExport(jxlsHelper, templateStream, fileType, index, data);
362362
}
363363
}
364364
return null;
365365
}
366366

367-
private File doExport(InputStream templateStream, String fileExtName, int index, Map<String, Object> data) throws IOException {
367+
private File doExport(JxlsHelper jxlsHelper, InputStream templateStream, String fileExtName, int index, Map<String, Object> data) throws IOException {
368368
File tempFile = File.createTempFile("export_", String.format("_%d.%s", index, fileExtName));
369369
tempFile.deleteOnExit();
370370
try (OutputStream fileOutputStream = Files.newOutputStream(tempFile.toPath())) {
371-
JxlsHelper.getInstance().processTemplate(templateStream, fileOutputStream, new Context(data));
371+
jxlsHelper.processTemplate(templateStream, fileOutputStream, new Context(data));
372372
}
373373
return tempFile;
374374
}
@@ -390,9 +390,23 @@ private InputStream doGetTemplateFileInputStream(String tmplFilePath) throws IOE
390390
* @throws Exception 可能产生的任何异常
391391
*/
392392
public File export(String tmplFile) throws Exception {
393+
return export(tmplFile, JxlsHelper.getInstance());
394+
}
395+
396+
/**
397+
* @param tmplFile 模板文件名称
398+
* @param jxlsHelper JXLS辅助类实例对象
399+
* @return 将导出数据映射到tmplFile指定的Excel文件模板
400+
* @throws Exception 可能产生的任何异常
401+
* @since 2.1.2
402+
*/
403+
public File export(String tmplFile, JxlsHelper jxlsHelper) throws Exception {
393404
if (StringUtils.isBlank(tmplFile)) {
394405
throw new NullArgumentException("tmplFile");
395406
}
407+
if (jxlsHelper == null) {
408+
throw new NullArgumentException("jxlsHelper");
409+
}
396410
File file = null;
397411
if (processor != null) {
398412
List<File> files = new ArrayList<>();
@@ -401,11 +415,11 @@ public File export(String tmplFile) throws Exception {
401415
if (processorData == null || processorData.isEmpty()) {
402416
break;
403417
}
404-
files.add(doExport(tmplFile, idx, Collections.singletonMap("data", processorData)));
418+
files.add(doExport(jxlsHelper, tmplFile, idx, Collections.singletonMap("data", processorData)));
405419
}
406420
file = toZip(files);
407421
} else if (!data.isEmpty()) {
408-
file = doExport(tmplFile, 1, data);
422+
file = doExport(jxlsHelper, tmplFile, 1, data);
409423
}
410424
return file;
411425
}

0 commit comments

Comments
 (0)