| @@ -1,6 +1,8 @@ | |||||
| package org.jeecgframework.poi.cache; | package org.jeecgframework.poi.cache; | ||||
| import java.io.InputStream; | import java.io.InputStream; | ||||
| import java.util.Arrays; | |||||
| import java.util.List; | |||||
| import org.apache.poi.openxml4j.exceptions.InvalidFormatException; | import org.apache.poi.openxml4j.exceptions.InvalidFormatException; | ||||
| import org.apache.poi.ss.usermodel.Workbook; | import org.apache.poi.ss.usermodel.Workbook; | ||||
| @@ -20,27 +22,28 @@ 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, int index) { | |||||
| public static Workbook getWorkbook(String url, Integer[] sheetNums) { | |||||
| InputStream is = null; | InputStream is = null; | ||||
| 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--) { | for (int i = wb.getNumberOfSheets() - 1; i >= 0; i--) { | ||||
| if (i != index) { | |||||
| if (!sheetList.contains(i)) { | |||||
| wb.removeSheetAt(i); | wb.removeSheetAt(i); | ||||
| } | } | ||||
| } | } | ||||
| return wb; | return wb; | ||||
| } catch (InvalidFormatException e) { | } catch (InvalidFormatException e) { | ||||
| LOGGER.error(e.getMessage(),e); | |||||
| LOGGER.error(e.getMessage(), e); | |||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| LOGGER.error(e.getMessage(),e); | |||||
| LOGGER.error(e.getMessage(), e); | |||||
| } finally { | } finally { | ||||
| try { | try { | ||||
| is.close(); | is.close(); | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| LOGGER.error(e.getMessage(),e); | |||||
| LOGGER.error(e.getMessage(), e); | |||||
| } | } | ||||
| } | } | ||||
| return null; | return null; | ||||
| @@ -93,7 +93,7 @@ public final class ExcelExportUtil { | |||||
| } | } | ||||
| /** | /** | ||||
| * 导出文件通过模板解析 | |||||
| * 导出文件通过模板解析,不推荐这个了,推荐全部通过模板来执行处理 | |||||
| * | * | ||||
| * @param params | * @param params | ||||
| * 导出参数类 | * 导出参数类 | ||||
| @@ -14,54 +14,49 @@ public class TemplateExportParams extends ExcelBaseParams { | |||||
| /** | /** | ||||
| * 模板的路径 | * 模板的路径 | ||||
| */ | */ | ||||
| private String templateUrl; | |||||
| private String templateUrl; | |||||
| /** | /** | ||||
| * 需要导出的第几个 sheetNum,默认是第0个 | * 需要导出的第几个 sheetNum,默认是第0个 | ||||
| */ | */ | ||||
| private int sheetNum = 0; | |||||
| private Integer[] sheetNum = new Integer[] { 0 }; | |||||
| /** | /** | ||||
| * 这只sheetName 不填就使用原来的 | * 这只sheetName 不填就使用原来的 | ||||
| */ | */ | ||||
| private String sheetName; | |||||
| private String sheetName; | |||||
| /** | /** | ||||
| * 表格列标题行数,默认1 | * 表格列标题行数,默认1 | ||||
| */ | */ | ||||
| private int headingRows = 1; | |||||
| private int headingRows = 1; | |||||
| /** | /** | ||||
| * 表格列标题开始行,默认1 | * 表格列标题开始行,默认1 | ||||
| */ | */ | ||||
| private int headingStartRow = 1; | |||||
| private int headingStartRow = 1; | |||||
| /** | /** | ||||
| * Excel 导出style | * Excel 导出style | ||||
| */ | */ | ||||
| private Class<?> style = ExcelExportStylerDefaultImpl.class; | |||||
| private Class<?> style = ExcelExportStylerDefaultImpl.class; | |||||
| public TemplateExportParams() { | public TemplateExportParams() { | ||||
| } | } | ||||
| public TemplateExportParams(String templateUrl) { | |||||
| public TemplateExportParams(String templateUrl, Integer... sheetNum) { | |||||
| this.templateUrl = templateUrl; | this.templateUrl = templateUrl; | ||||
| if (sheetNum != null && sheetNum.length > 0) { | |||||
| this.sheetNum = sheetNum; | |||||
| } | |||||
| } | } | ||||
| public TemplateExportParams(String templateUrl, int sheetNum) { | |||||
| this.templateUrl = templateUrl; | |||||
| this.sheetNum = sheetNum; | |||||
| } | |||||
| public TemplateExportParams(String templateUrl, String sheetName) { | |||||
| public TemplateExportParams(String templateUrl, String sheetName, Integer... sheetNum) { | |||||
| this.templateUrl = templateUrl; | this.templateUrl = templateUrl; | ||||
| this.sheetName = sheetName; | this.sheetName = sheetName; | ||||
| } | |||||
| public TemplateExportParams(String templateUrl, String sheetName, int sheetNum) { | |||||
| this.templateUrl = templateUrl; | |||||
| this.sheetName = sheetName; | |||||
| this.sheetNum = sheetNum; | |||||
| if (sheetNum != null && sheetNum.length > 0) { | |||||
| this.sheetNum = sheetNum; | |||||
| } | |||||
| } | } | ||||
| public int getHeadingRows() { | public int getHeadingRows() { | ||||
| @@ -76,7 +71,7 @@ public class TemplateExportParams extends ExcelBaseParams { | |||||
| return sheetName; | return sheetName; | ||||
| } | } | ||||
| public int getSheetNum() { | |||||
| public Integer[] getSheetNum() { | |||||
| return sheetNum; | return sheetNum; | ||||
| } | } | ||||
| @@ -96,10 +91,14 @@ public class TemplateExportParams extends ExcelBaseParams { | |||||
| this.sheetName = sheetName; | this.sheetName = sheetName; | ||||
| } | } | ||||
| public void setSheetNum(int sheetNum) { | |||||
| public void setSheetNum(Integer[] sheetNum) { | |||||
| this.sheetNum = sheetNum; | this.sheetNum = sheetNum; | ||||
| } | } | ||||
| public void setSheetNum(Integer sheetNum) { | |||||
| this.sheetNum = new Integer[] { sheetNum }; | |||||
| } | |||||
| public void setTemplateUrl(String templateUrl) { | public void setTemplateUrl(String templateUrl) { | ||||
| this.templateUrl = templateUrl; | this.templateUrl = templateUrl; | ||||
| } | } | ||||
| @@ -156,7 +156,9 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| wb.setSheetName(0, params.getSheetName()); | wb.setSheetName(0, params.getSheetName()); | ||||
| } | } | ||||
| // step 3. 解析模板 | // step 3. 解析模板 | ||||
| parseTemplate(wb.getSheetAt(0), map); | |||||
| for (int i = 0; i < params.getSheetNum().length; i++) { | |||||
| parseTemplate(wb.getSheetAt(i), map); | |||||
| } | |||||
| if (dataSet != null) { | if (dataSet != null) { | ||||
| // step 4. 正常的数据填充 | // step 4. 正常的数据填充 | ||||
| dataHanlder = params.getDataHanlder(); | dataHanlder = params.getDataHanlder(); | ||||
| @@ -92,7 +92,7 @@ public class ExcelExportTemplateTest { | |||||
| map.put("i" + i, temp); | map.put("i" + i, temp); | ||||
| } | } | ||||
| params.setTemplateUrl("org/jeecgframework/poi/test/excel/doc/exportTemp.xls"); | params.setTemplateUrl("org/jeecgframework/poi/test/excel/doc/exportTemp.xls"); | ||||
| params.setSheetNum(1); | |||||
| params.setSheetNum(new Integer[]{1}); | |||||
| Workbook book = ExcelExportUtil.exportExcel(params, CourseEntity.class, list, map); | Workbook book = ExcelExportUtil.exportExcel(params, CourseEntity.class, list, map); | ||||
| File savefile = new File("d:/"); | File savefile = new File("d:/"); | ||||
| if (!savefile.exists()) { | if (!savefile.exists()) { | ||||