| @@ -22,16 +22,18 @@ public final class ExcelCache { | |||||
| private static final Logger LOGGER = LoggerFactory.getLogger(ExcelCache.class); | 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; | InputStream is = null; | ||||
| List<Integer> sheetList = Arrays.asList(sheetNums); | List<Integer> sheetList = Arrays.asList(sheetNums); | ||||
| try { | try { | ||||
| is = POICacheManager.getFile(url); | is = POICacheManager.getFile(url); | ||||
| Workbook wb = WorkbookFactory.create(is); | Workbook wb = WorkbookFactory.create(is); | ||||
| // 删除其他的sheet | // 删除其他的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; | return wb; | ||||
| @@ -11,6 +11,10 @@ import org.jeecgframework.poi.excel.export.styler.ExcelExportStylerDefaultImpl; | |||||
| */ | */ | ||||
| public class TemplateExportParams extends ExcelBaseParams { | public class TemplateExportParams extends ExcelBaseParams { | ||||
| /** | |||||
| * 输出全部的sheet | |||||
| */ | |||||
| private boolean scanAllsheet = false; | |||||
| /** | /** | ||||
| * 模板的路径 | * 模板的路径 | ||||
| */ | */ | ||||
| @@ -24,7 +28,7 @@ public class TemplateExportParams extends ExcelBaseParams { | |||||
| /** | /** | ||||
| * 这只sheetName 不填就使用原来的 | * 这只sheetName 不填就使用原来的 | ||||
| */ | */ | ||||
| private String sheetName; | |||||
| private String[] sheetName; | |||||
| /** | /** | ||||
| * 表格列标题行数,默认1 | * 表格列标题行数,默认1 | ||||
| @@ -35,15 +39,42 @@ public class TemplateExportParams extends ExcelBaseParams { | |||||
| * 表格列标题开始行,默认1 | * 表格列标题开始行,默认1 | ||||
| */ | */ | ||||
| private int headingStartRow = 1; | private int headingStartRow = 1; | ||||
| /** | |||||
| * 设置数据源的NUM | |||||
| */ | |||||
| private int dataSheetNum = 0; | |||||
| /** | /** | ||||
| * Excel 导出style | * Excel 导出style | ||||
| */ | */ | ||||
| private Class<?> style = ExcelExportStylerDefaultImpl.class; | private Class<?> style = ExcelExportStylerDefaultImpl.class; | ||||
| /** | |||||
| * 默认构造器 | |||||
| */ | |||||
| public TemplateExportParams() { | 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) { | public TemplateExportParams(String templateUrl, Integer... sheetNum) { | ||||
| this.templateUrl = templateUrl; | this.templateUrl = templateUrl; | ||||
| if (sheetNum != null && sheetNum.length > 0) { | 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) { | public TemplateExportParams(String templateUrl, String sheetName, Integer... sheetNum) { | ||||
| this.templateUrl = templateUrl; | this.templateUrl = templateUrl; | ||||
| this.sheetName = sheetName; | |||||
| this.sheetName = new String[] { sheetName }; | |||||
| if (sheetNum != null && sheetNum.length > 0) { | if (sheetNum != null && sheetNum.length > 0) { | ||||
| this.sheetNum = sheetNum; | this.sheetNum = sheetNum; | ||||
| } | } | ||||
| @@ -67,7 +104,7 @@ public class TemplateExportParams extends ExcelBaseParams { | |||||
| return headingStartRow; | return headingStartRow; | ||||
| } | } | ||||
| public String getSheetName() { | |||||
| public String[] getSheetName() { | |||||
| return sheetName; | return sheetName; | ||||
| } | } | ||||
| @@ -87,10 +124,14 @@ public class TemplateExportParams extends ExcelBaseParams { | |||||
| this.headingStartRow = headingStartRow; | this.headingStartRow = headingStartRow; | ||||
| } | } | ||||
| public void setSheetName(String sheetName) { | |||||
| public void setSheetName(String[] sheetName) { | |||||
| this.sheetName = sheetName; | this.sheetName = sheetName; | ||||
| } | } | ||||
| public void setSheetName(String sheetName) { | |||||
| this.sheetName = new String[] { sheetName }; | |||||
| } | |||||
| public void setSheetNum(Integer[] sheetNum) { | public void setSheetNum(Integer[] sheetNum) { | ||||
| this.sheetNum = sheetNum; | this.sheetNum = sheetNum; | ||||
| } | } | ||||
| @@ -111,4 +152,20 @@ public class TemplateExportParams extends ExcelBaseParams { | |||||
| this.style = style; | 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; | |||||
| } | |||||
| } | } | ||||
| @@ -45,9 +45,9 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| private static final String START_STR = "{{"; | private static final String START_STR = "{{"; | ||||
| private static final String END_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<String> tempCreateCellSet = new HashSet<String>(); | private Set<String> tempCreateCellSet = new HashSet<String>(); | ||||
| @@ -151,11 +151,13 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| // step 2. 判断模板的Excel类型,解析模板 | // step 2. 判断模板的Excel类型,解析模板 | ||||
| try { | try { | ||||
| wb = getCloneWorkBook(params); | wb = getCloneWorkBook(params); | ||||
| if (StringUtils.isNotEmpty(params.getSheetName())) { | |||||
| wb.setSheetName(0, params.getSheetName()); | |||||
| } | |||||
| // step 3. 解析模板 | // 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); | parseTemplate(wb.getSheetAt(i), map); | ||||
| } | } | ||||
| if (dataSet != null) { | if (dataSet != null) { | ||||
| @@ -164,7 +166,8 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| if (dataHanlder != null) { | if (dataHanlder != null) { | ||||
| needHanlderList = Arrays.asList(dataHanlder.getNeedHandlerFields()); | 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) { | } catch (Exception e) { | ||||
| LOGGER.error(e.getMessage(), e); | LOGGER.error(e.getMessage(), e); | ||||
| @@ -182,7 +185,8 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| * @date 2013-11-11 | * @date 2013-11-11 | ||||
| */ | */ | ||||
| private Workbook getCloneWorkBook(TemplateExportParams params) throws Exception { | 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(); | oldString = cell.getStringCellValue(); | ||||
| if (oldString != null && oldString.indexOf(START_STR) != -1 | if (oldString != null && oldString.indexOf(START_STR) != -1 | ||||
| && !oldString.contains("foreach||")) { | && !oldString.contains("foreach||")) { | ||||
| // setp 2. 判断是否含有解析函数 | |||||
| // step 2. 判断是否含有解析函数 | |||||
| String params; | String params; | ||||
| boolean isNumber = false; | boolean isNumber = false; | ||||
| if (oldString.indexOf(NUMBER_Symbol) != -1) { | |||||
| if (oldString.contains(NUMBER_SYMBOL)) { | |||||
| isNumber = true; | isNumber = true; | ||||
| oldString = oldString.substring(2); | |||||
| oldString = oldString.replace(NUMBER_SYMBOL, ""); | |||||
| } | } | ||||
| while (oldString.indexOf(START_STR) != -1) { | while (oldString.indexOf(START_STR) != -1) { | ||||
| params = oldString.substring(oldString.indexOf(START_STR) + 2, | params = oldString.substring(oldString.indexOf(START_STR) + 2, | ||||
| @@ -26,7 +26,7 @@ public class ExcelExportTemplateTest { | |||||
| @Test | @Test | ||||
| public void one() throws Exception { | public void one() throws Exception { | ||||
| TemplateExportParams params = new TemplateExportParams( | 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.setHeadingRows(2); | ||||
| params.setHeadingStartRow(2); | params.setHeadingStartRow(2); | ||||
| params.setStyle(ExcelStyleType.BORDER.getClazz()); | params.setStyle(ExcelStyleType.BORDER.getClazz()); | ||||