diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/cache/ExcelCache.java b/easypoi-base/src/main/java/org/jeecgframework/poi/cache/ExcelCache.java index af3e411..59bca23 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/cache/ExcelCache.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/cache/ExcelCache.java @@ -22,16 +22,18 @@ public final class ExcelCache { private static final Logger LOGGER = LoggerFactory.getLogger(ExcelCache.class); - public static Workbook getWorkbook(String url, Integer[] sheetNums) { + public static Workbook getWorkbook(String url, Integer[] sheetNums, boolean needAll) { InputStream is = null; List sheetList = Arrays.asList(sheetNums); try { is = POICacheManager.getFile(url); Workbook wb = WorkbookFactory.create(is); // 删除其他的sheet - for (int i = wb.getNumberOfSheets() - 1; i >= 0; i--) { - if (!sheetList.contains(i)) { - wb.removeSheetAt(i); + if (!needAll) { + for (int i = wb.getNumberOfSheets() - 1; i >= 0; i--) { + if (!sheetList.contains(i)) { + wb.removeSheetAt(i); + } } } return wb; diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/TemplateExportParams.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/TemplateExportParams.java index 9338678..e9cc3cf 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/TemplateExportParams.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/TemplateExportParams.java @@ -11,6 +11,10 @@ import org.jeecgframework.poi.excel.export.styler.ExcelExportStylerDefaultImpl; */ public class TemplateExportParams extends ExcelBaseParams { + /** + * 输出全部的sheet + */ + private boolean scanAllsheet = false; /** * 模板的路径 */ @@ -24,7 +28,7 @@ public class TemplateExportParams extends ExcelBaseParams { /** * 这只sheetName 不填就使用原来的 */ - private String sheetName; + private String[] sheetName; /** * 表格列标题行数,默认1 @@ -35,15 +39,42 @@ public class TemplateExportParams extends ExcelBaseParams { * 表格列标题开始行,默认1 */ private int headingStartRow = 1; + /** + * 设置数据源的NUM + */ + private int dataSheetNum = 0; /** * Excel 导出style */ private Class style = ExcelExportStylerDefaultImpl.class; + /** + * 默认构造器 + */ public TemplateExportParams() { } + /** + * 构造器 + * @param templateUrl 模板路径 + * @param scanAllsheet 是否输出全部的sheet + * @param sheetName sheet的名称,可不填 + */ + public TemplateExportParams(String templateUrl, boolean scanAllsheet, String... sheetName) { + this.templateUrl = templateUrl; + this.scanAllsheet = scanAllsheet; + if (sheetName != null && sheetName.length > 0) { + this.sheetName = sheetName; + + } + } + + /** + * 构造器 + * @param templateUrl 模板路径 + * @param sheetNum sheet 的位置,可不填 + */ public TemplateExportParams(String templateUrl, Integer... sheetNum) { this.templateUrl = templateUrl; if (sheetNum != null && sheetNum.length > 0) { @@ -51,9 +82,15 @@ public class TemplateExportParams extends ExcelBaseParams { } } + /** + * 单个sheet输出构造器 + * @param templateUrl 模板路径 + * @param sheetName sheet的名称 + * @param sheetNum sheet的位置,可不填 + */ public TemplateExportParams(String templateUrl, String sheetName, Integer... sheetNum) { this.templateUrl = templateUrl; - this.sheetName = sheetName; + this.sheetName = new String[] { sheetName }; if (sheetNum != null && sheetNum.length > 0) { this.sheetNum = sheetNum; } @@ -67,7 +104,7 @@ public class TemplateExportParams extends ExcelBaseParams { return headingStartRow; } - public String getSheetName() { + public String[] getSheetName() { return sheetName; } @@ -87,10 +124,14 @@ public class TemplateExportParams extends ExcelBaseParams { this.headingStartRow = headingStartRow; } - public void setSheetName(String sheetName) { + public void setSheetName(String[] sheetName) { this.sheetName = sheetName; } + public void setSheetName(String sheetName) { + this.sheetName = new String[] { sheetName }; + } + public void setSheetNum(Integer[] sheetNum) { this.sheetNum = sheetNum; } @@ -111,4 +152,20 @@ public class TemplateExportParams extends ExcelBaseParams { this.style = style; } + public int getDataSheetNum() { + return dataSheetNum; + } + + public void setDataSheetNum(int dataSheetNum) { + this.dataSheetNum = dataSheetNum; + } + + public boolean isScanAllsheet() { + return scanAllsheet; + } + + public void setScanAllsheet(boolean scanAllsheet) { + this.scanAllsheet = scanAllsheet; + } + } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java index fedff4d..2349c0f 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java @@ -45,9 +45,9 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { private static final String START_STR = "{{"; private static final String END_STR = "}}"; - private static final String NUMBER_Symbol = "N:"; + private static final String NUMBER_SYMBOL = "N:"; /** - * 缓存temp 的for each创建的cell ,跳过这个cell的模板语法查找,提高效率 + * 缓存TEMP 的for each创建的cell ,跳过这个cell的模板语法查找,提高效率 */ private Set tempCreateCellSet = new HashSet(); @@ -151,11 +151,13 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { // step 2. 判断模板的Excel类型,解析模板 try { wb = getCloneWorkBook(params); - if (StringUtils.isNotEmpty(params.getSheetName())) { - wb.setSheetName(0, params.getSheetName()); - } // step 3. 解析模板 - for (int i = 0; i < params.getSheetNum().length; i++) { + for (int i = 0, le = params.isScanAllsheet() ? wb.getNumberOfSheets() : params + .getSheetNum().length; i < le; i++) { + if (params.getSheetName() != null && params.getSheetName().length > i + && StringUtils.isNotEmpty(params.getSheetName()[i])) { + wb.setSheetName(i, params.getSheetName()[i]); + } parseTemplate(wb.getSheetAt(i), map); } if (dataSet != null) { @@ -164,7 +166,8 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { if (dataHanlder != null) { needHanlderList = Arrays.asList(dataHanlder.getNeedHandlerFields()); } - addDataToSheet(params, pojoClass, dataSet, wb.getSheetAt(0), wb); + addDataToSheet(params, pojoClass, dataSet, wb.getSheetAt(params.getDataSheetNum()), + wb); } } catch (Exception e) { LOGGER.error(e.getMessage(), e); @@ -182,7 +185,8 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { * @date 2013-11-11 */ private Workbook getCloneWorkBook(TemplateExportParams params) throws Exception { - return ExcelCache.getWorkbook(params.getTemplateUrl(), params.getSheetNum()); + return ExcelCache.getWorkbook(params.getTemplateUrl(), params.getSheetNum(), + params.isScanAllsheet()); } @@ -260,12 +264,12 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { oldString = cell.getStringCellValue(); if (oldString != null && oldString.indexOf(START_STR) != -1 && !oldString.contains("foreach||")) { - // setp 2. 判断是否含有解析函数 + // step 2. 判断是否含有解析函数 String params; boolean isNumber = false; - if (oldString.indexOf(NUMBER_Symbol) != -1) { + if (oldString.contains(NUMBER_SYMBOL)) { isNumber = true; - oldString = oldString.substring(2); + oldString = oldString.replace(NUMBER_SYMBOL, ""); } while (oldString.indexOf(START_STR) != -1) { params = oldString.substring(oldString.indexOf(START_STR) + 2, diff --git a/easypoi-test/src/main/java/org/jeecgframework/poi/test/excel/test/ExcelExportTemplateTest.java b/easypoi-test/src/main/java/org/jeecgframework/poi/test/excel/test/ExcelExportTemplateTest.java index b1fce97..4b01280 100644 --- a/easypoi-test/src/main/java/org/jeecgframework/poi/test/excel/test/ExcelExportTemplateTest.java +++ b/easypoi-test/src/main/java/org/jeecgframework/poi/test/excel/test/ExcelExportTemplateTest.java @@ -26,7 +26,7 @@ public class ExcelExportTemplateTest { @Test public void one() throws Exception { TemplateExportParams params = new TemplateExportParams( - "org/jeecgframework/poi/test/excel/doc/exportTemp.xls", 0, 1); + "org/jeecgframework/poi/test/excel/doc/exportTemp.xls", true); params.setHeadingRows(2); params.setHeadingStartRow(2); params.setStyle(ExcelStyleType.BORDER.getClazz());