| @@ -1,6 +1,5 @@ | |||||
| package org.jeecgframework.poi.cache; | package org.jeecgframework.poi.cache; | ||||
| import java.io.IOException; | |||||
| import java.io.InputStream; | import java.io.InputStream; | ||||
| import org.apache.poi.openxml4j.exceptions.InvalidFormatException; | import org.apache.poi.openxml4j.exceptions.InvalidFormatException; | ||||
| @@ -35,12 +34,12 @@ public final class ExcelCache { | |||||
| return wb; | return wb; | ||||
| } catch (InvalidFormatException e) { | } catch (InvalidFormatException e) { | ||||
| LOGGER.error(e.getMessage(),e); | LOGGER.error(e.getMessage(),e); | ||||
| } catch (IOException e) { | |||||
| } catch (Exception e) { | |||||
| LOGGER.error(e.getMessage(),e); | LOGGER.error(e.getMessage(),e); | ||||
| } finally { | } finally { | ||||
| try { | try { | ||||
| is.close(); | is.close(); | ||||
| } catch (IOException e) { | |||||
| } catch (Exception e) { | |||||
| LOGGER.error(e.getMessage(),e); | LOGGER.error(e.getMessage(),e); | ||||
| } | } | ||||
| } | } | ||||
| @@ -1,6 +1,5 @@ | |||||
| package org.jeecgframework.poi.cache; | package org.jeecgframework.poi.cache; | ||||
| import java.io.IOException; | |||||
| import java.io.InputStream; | import java.io.InputStream; | ||||
| import org.jeecgframework.poi.cache.manager.POICacheManager; | import org.jeecgframework.poi.cache.manager.POICacheManager; | ||||
| @@ -29,7 +28,7 @@ public class WordCache { | |||||
| } finally { | } finally { | ||||
| try { | try { | ||||
| is.close(); | is.close(); | ||||
| } catch (IOException e) { | |||||
| } catch (Exception e) { | |||||
| LOGGER.error(e.getMessage(),e); | LOGGER.error(e.getMessage(),e); | ||||
| } | } | ||||
| } | } | ||||
| @@ -35,7 +35,7 @@ public final class ExcelExportUtil { | |||||
| public static Workbook exportExcel(ExportParams entity, Class<?> pojoClass, | public static Workbook exportExcel(ExportParams entity, Class<?> pojoClass, | ||||
| Collection<?> dataSet) { | Collection<?> dataSet) { | ||||
| Workbook workbook; | Workbook workbook; | ||||
| if (entity.getType().equals(ExcelType.HSSF)) { | |||||
| if (ExcelType.HSSF.equals(entity.getType())) { | |||||
| workbook = new HSSFWorkbook(); | workbook = new HSSFWorkbook(); | ||||
| } else if (dataSet.size() < 1000) { | } else if (dataSet.size() < 1000) { | ||||
| workbook = new XSSFWorkbook(); | workbook = new XSSFWorkbook(); | ||||
| @@ -58,7 +58,7 @@ public final class ExcelExportUtil { | |||||
| public static Workbook exportExcel(ExportParams entity, List<ExcelExportEntity> entityList, | public static Workbook exportExcel(ExportParams entity, List<ExcelExportEntity> entityList, | ||||
| Collection<? extends Map<?, ?>> dataSet) { | Collection<? extends Map<?, ?>> dataSet) { | ||||
| Workbook workbook; | Workbook workbook; | ||||
| if (entity.getType().equals(ExcelType.HSSF)) { | |||||
| if (ExcelType.HSSF.equals(entity.getType())) { | |||||
| workbook = new HSSFWorkbook(); | workbook = new HSSFWorkbook(); | ||||
| } else if (dataSet.size() < 1000) { | } else if (dataSet.size() < 1000) { | ||||
| workbook = new XSSFWorkbook(); | workbook = new XSSFWorkbook(); | ||||
| @@ -79,7 +79,7 @@ public final class ExcelExportUtil { | |||||
| */ | */ | ||||
| public static Workbook exportExcel(List<Map<String, Object>> list, String type) { | public static Workbook exportExcel(List<Map<String, Object>> list, String type) { | ||||
| Workbook workbook; | Workbook workbook; | ||||
| if (type.equals(ExcelType.HSSF)) { | |||||
| if (ExcelType.HSSF.equals(type)) { | |||||
| workbook = new HSSFWorkbook(); | workbook = new HSSFWorkbook(); | ||||
| } else { | } else { | ||||
| workbook = new XSSFWorkbook(); | workbook = new XSSFWorkbook(); | ||||
| @@ -1,16 +0,0 @@ | |||||
| package org.jeecgframework.poi.excel.entity.params; | |||||
| import java.util.Comparator; | |||||
| /** | |||||
| * 按照升序排序 | |||||
| * @author JueYue | |||||
| * | |||||
| */ | |||||
| public class ComparatorExcelField implements Comparator<ExcelExportEntity> { | |||||
| public int compare(ExcelExportEntity prev, ExcelExportEntity next) { | |||||
| return prev.getOrderNum() - next.getOrderNum(); | |||||
| } | |||||
| } | |||||
| @@ -8,7 +8,7 @@ import java.util.List; | |||||
| * @author JueYue | * @author JueYue | ||||
| * @version 1.0 2013年8月24日 | * @version 1.0 2013年8月24日 | ||||
| */ | */ | ||||
| public class ExcelExportEntity extends ExcelBaseEntity { | |||||
| public class ExcelExportEntity extends ExcelBaseEntity implements Comparable<ExcelExportEntity> { | |||||
| /** | /** | ||||
| * 如果是MAP导出,这个是map的key | * 如果是MAP导出,这个是map的key | ||||
| @@ -172,4 +172,9 @@ public class ExcelExportEntity extends ExcelBaseEntity { | |||||
| this.isStatistics = isStatistics; | this.isStatistics = isStatistics; | ||||
| } | } | ||||
| @Override | |||||
| public int compareTo(ExcelExportEntity prev) { | |||||
| return this.getOrderNum() - prev.getOrderNum(); | |||||
| } | |||||
| } | } | ||||
| @@ -15,7 +15,6 @@ import org.apache.commons.lang.StringUtils; | |||||
| import org.jeecgframework.poi.excel.annotation.Excel; | import org.jeecgframework.poi.excel.annotation.Excel; | ||||
| import org.jeecgframework.poi.excel.annotation.ExcelCollection; | import org.jeecgframework.poi.excel.annotation.ExcelCollection; | ||||
| import org.jeecgframework.poi.excel.annotation.ExcelEntity; | import org.jeecgframework.poi.excel.annotation.ExcelEntity; | ||||
| import org.jeecgframework.poi.excel.entity.params.ComparatorExcelField; | |||||
| import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | ||||
| import org.jeecgframework.poi.handler.inter.IExcelDataHandler; | import org.jeecgframework.poi.handler.inter.IExcelDataHandler; | ||||
| import org.jeecgframework.poi.util.POIPublicUtil; | import org.jeecgframework.poi.util.POIPublicUtil; | ||||
| @@ -296,10 +295,10 @@ public class ExportBase { | |||||
| * 对字段根据用户设置排序 | * 对字段根据用户设置排序 | ||||
| */ | */ | ||||
| public void sortAllParams(List<ExcelExportEntity> excelParams) { | public void sortAllParams(List<ExcelExportEntity> excelParams) { | ||||
| Collections.sort(excelParams, new ComparatorExcelField()); | |||||
| Collections.sort(excelParams); | |||||
| for (ExcelExportEntity entity : excelParams) { | for (ExcelExportEntity entity : excelParams) { | ||||
| if (entity.getList() != null) { | if (entity.getList() != null) { | ||||
| Collections.sort(entity.getList(), new ComparatorExcelField()); | |||||
| Collections.sort(entity.getList()); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -42,6 +42,9 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| private static final Logger LOGGER = LoggerFactory | private static final Logger LOGGER = LoggerFactory | ||||
| .getLogger(ExcelExportOfTemplateUtil.class); | .getLogger(ExcelExportOfTemplateUtil.class); | ||||
| private static final String START_STR = "{{"; | |||||
| private static final String END_STR = "}}"; | |||||
| /** | /** | ||||
| * 缓存temp 的for each创建的cell ,跳过这个cell的模板语法查找,提高效率 | * 缓存temp 的for each创建的cell ,跳过这个cell的模板语法查找,提高效率 | ||||
| */ | */ | ||||
| @@ -188,7 +191,8 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| private String getParamsValue(String params, Map<String, Object> map) throws Exception { | private String getParamsValue(String params, Map<String, Object> map) throws Exception { | ||||
| if (params.indexOf(".") != -1) { | if (params.indexOf(".") != -1) { | ||||
| String[] paramsArr = params.split("\\."); | String[] paramsArr = params.split("\\."); | ||||
| return getValueDoWhile(map.get(paramsArr[0]), paramsArr, 1); | |||||
| return String | |||||
| .valueOf(POIPublicUtil.getValueDoWhile(map.get(paramsArr[0]), paramsArr, 1)); | |||||
| } | } | ||||
| return map.containsKey(params) ? map.get(params).toString() : ""; | return map.containsKey(params) ? map.get(params).toString() : ""; | ||||
| } | } | ||||
| @@ -221,33 +225,6 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| } | } | ||||
| /** | |||||
| * 通过遍历过去对象值 | |||||
| * | |||||
| * @param object | |||||
| * @param paramsArr | |||||
| * @param index | |||||
| * @return | |||||
| * @throws Exception | |||||
| * @throws java.lang.reflect.InvocationTargetException | |||||
| * @throws IllegalAccessException | |||||
| * @throws IllegalArgumentException | |||||
| */ | |||||
| @SuppressWarnings("rawtypes") | |||||
| private String getValueDoWhile(Object object, String[] paramsArr, int index) throws Exception { | |||||
| if (object == null) { | |||||
| return ""; | |||||
| } | |||||
| if (object instanceof Map) { | |||||
| object = ((Map) object).get(paramsArr[index]); | |||||
| } else { | |||||
| object = POIPublicUtil.getMethod(paramsArr[index], object.getClass()).invoke(object, | |||||
| new Object[] {}); | |||||
| } | |||||
| return (index == paramsArr.length - 1) ? (object == null ? "" : object.toString()) | |||||
| : getValueDoWhile(object, paramsArr, ++index); | |||||
| } | |||||
| private void parseTemplate(Sheet sheet, Map<String, Object> map) throws Exception { | private void parseTemplate(Sheet sheet, Map<String, Object> map) throws Exception { | ||||
| Row row = null; | Row row = null; | ||||
| int index = 0; | int index = 0; | ||||
| @@ -274,14 +251,17 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| try {// step 1. 判断这个cell里面是不是函数 | try {// step 1. 判断这个cell里面是不是函数 | ||||
| oldString = cell.getStringCellValue(); | oldString = cell.getStringCellValue(); | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| LOGGER.error(e.getMessage(), e); | |||||
| return; | return; | ||||
| } | } | ||||
| if (oldString != null && oldString.indexOf("{{") != -1 && !oldString.contains("foreach||")) { | |||||
| if (oldString != null && oldString.indexOf(START_STR) != -1 | |||||
| && !oldString.contains("foreach||")) { | |||||
| // setp 2. 判断是否含有解析函数 | // setp 2. 判断是否含有解析函数 | ||||
| String params; | String params; | ||||
| while (oldString.indexOf("{{") != -1) { | |||||
| params = oldString.substring(oldString.indexOf("{{") + 2, oldString.indexOf("}}")); | |||||
| oldString = oldString.replace("{{" + params + "}}", | |||||
| while (oldString.indexOf(START_STR) != -1) { | |||||
| params = oldString.substring(oldString.indexOf(START_STR) + 2, | |||||
| oldString.indexOf(END_STR)); | |||||
| oldString = oldString.replace(START_STR + params + END_STR, | |||||
| getParamsValue(params.trim(), map)); | getParamsValue(params.trim(), map)); | ||||
| } | } | ||||
| cell.setCellValue(oldString); | cell.setCellValue(oldString); | ||||
| @@ -305,7 +285,7 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| throws Exception { | throws Exception { | ||||
| boolean isCreate = !name.startsWith("!"); | boolean isCreate = !name.startsWith("!"); | ||||
| Collection<?> datas = (Collection<?>) map.get(name.substring(name.indexOf("||") + 2, | Collection<?> datas = (Collection<?>) map.get(name.substring(name.indexOf("||") + 2, | ||||
| name.indexOf("{{"))); | |||||
| name.indexOf(START_STR))); | |||||
| List<String> columns = getAllDataColumns(cell, name); | List<String> columns = getAllDataColumns(cell, name); | ||||
| if (datas == null) { | if (datas == null) { | ||||
| return; | return; | ||||
| @@ -339,7 +319,8 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| row.createCell(i); | row.createCell(i); | ||||
| } | } | ||||
| for (int i = 0, max = columns.size(); i < max; i++) { | for (int i = 0, max = columns.size(); i < max; i++) { | ||||
| String val = getValueDoWhile(t, columns.get(i).split("\\."), 0); | |||||
| String val = String.valueOf(POIPublicUtil.getValueDoWhile(t, columns.get(i) | |||||
| .split("\\."), 0)); | |||||
| row.getCell(i + columnIndex).setCellValue(val); | row.getCell(i + columnIndex).setCellValue(val); | ||||
| tempCreateCellSet.add(row.getRowNum() + "_" + (i + columnIndex)); | tempCreateCellSet.add(row.getRowNum() + "_" + (i + columnIndex)); | ||||
| } | } | ||||
| @@ -354,12 +335,12 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| */ | */ | ||||
| private List<String> getAllDataColumns(Cell cell, String name) { | private List<String> getAllDataColumns(Cell cell, String name) { | ||||
| List<String> columns = new ArrayList<String>(); | List<String> columns = new ArrayList<String>(); | ||||
| if (name.contains("}}")) { | |||||
| columns.add(name.substring(name.indexOf("{{") + 2, name.indexOf("}}")).trim()); | |||||
| if (name.contains(END_STR)) { | |||||
| columns.add(name.substring(name.indexOf(START_STR) + 2, name.indexOf(END_STR)).trim()); | |||||
| cell.setCellValue(""); | cell.setCellValue(""); | ||||
| return columns; | return columns; | ||||
| } | } | ||||
| columns.add(name.substring(name.indexOf("{{") + 2).trim()); | |||||
| columns.add(name.substring(name.indexOf(START_STR) + 2).trim()); | |||||
| int index = cell.getColumnIndex(); | int index = cell.getColumnIndex(); | ||||
| Cell tempCell; | Cell tempCell; | ||||
| while (true) { | while (true) { | ||||
| @@ -368,14 +349,15 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| try {//不允许为空 | try {//不允许为空 | ||||
| cellStringString = tempCell.getStringCellValue(); | cellStringString = tempCell.getStringCellValue(); | ||||
| if (StringUtils.isBlank(cellStringString)) { | if (StringUtils.isBlank(cellStringString)) { | ||||
| throw new RuntimeException(); | |||||
| throw new ExcelExportException(); | |||||
| } | } | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| throw new ExcelExportException("for each 当中存在空字符串,请检查模板"); | throw new ExcelExportException("for each 当中存在空字符串,请检查模板"); | ||||
| } | } | ||||
| //把读取过的cell 置为空 | |||||
| cell.setCellValue(""); | cell.setCellValue(""); | ||||
| if (cellStringString.contains("}}")) { | |||||
| columns.add(cellStringString.trim().replace("}}", "")); | |||||
| if (cellStringString.contains(END_STR)) { | |||||
| columns.add(cellStringString.trim().replace(END_STR, "")); | |||||
| break; | break; | ||||
| } else { | } else { | ||||
| columns.add(cellStringString.trim()); | columns.add(cellStringString.trim()); | ||||
| @@ -8,8 +8,9 @@ package org.jeecgframework.poi.exception.word.enmus; | |||||
| */ | */ | ||||
| public enum WordExportEnum { | public enum WordExportEnum { | ||||
| EXCEL_PARAMS_ERROR("Excel 导出 参数错误"), EXCEL_HEAD_HAVA_NULL("Excel 表头 有的字段为空"), EXCEL_NO_HEAD( | |||||
| "Excel 没有表头"); | |||||
| EXCEL_PARAMS_ERROR ("Excel 导出 参数错误") , | |||||
| EXCEL_HEAD_HAVA_NULL ("Excel 表头 有的字段为空") , | |||||
| EXCEL_NO_HEAD ("Excel 没有表头"); | |||||
| private String msg; | private String msg; | ||||
| @@ -37,8 +37,12 @@ import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | |||||
| import org.jeecgframework.poi.word.entity.WordImageEntity; | import org.jeecgframework.poi.word.entity.WordImageEntity; | ||||
| import org.jeecgframework.poi.word.entity.params.ExcelListEntity; | import org.jeecgframework.poi.word.entity.params.ExcelListEntity; | ||||
| import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker; | import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker; | ||||
| public class POIPublicUtil { | |||||
| /** | |||||
| * EASYPOI 的公共基础类 | |||||
| * @author JueYue | |||||
| * @date 2015年4月5日 上午12:59:22 | |||||
| */ | |||||
| public final class POIPublicUtil { | |||||
| private POIPublicUtil() { | private POIPublicUtil() { | ||||
| @@ -168,7 +172,7 @@ public class POIPublicUtil { | |||||
| public static Map<String, PictureData> getSheetPictrues03(HSSFSheet sheet, HSSFWorkbook workbook) { | public static Map<String, PictureData> getSheetPictrues03(HSSFSheet sheet, HSSFWorkbook workbook) { | ||||
| Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>(); | Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>(); | ||||
| List<HSSFPictureData> pictures = workbook.getAllPictures(); | List<HSSFPictureData> pictures = workbook.getAllPictures(); | ||||
| if (pictures.size() != 0) { | |||||
| if (!pictures.isEmpty()) { | |||||
| for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) { | for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) { | ||||
| HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor(); | HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor(); | ||||
| if (shape instanceof HSSFPicture) { | if (shape instanceof HSSFPicture) { | ||||
| @@ -412,8 +416,7 @@ public class POIPublicUtil { | |||||
| if (object instanceof Map) { | if (object instanceof Map) { | ||||
| object = ((Map) object).get(paramsArr[index]); | object = ((Map) object).get(paramsArr[index]); | ||||
| } else { | } else { | ||||
| object = POIPublicUtil.getMethod(paramsArr[index], object.getClass()).invoke(object, | |||||
| new Object[] {}); | |||||
| object = getMethod(paramsArr[index], object.getClass()).invoke(object, new Object[] {}); | |||||
| } | } | ||||
| return (index == paramsArr.length - 1) ? (object == null ? "" : object) : getValueDoWhile( | return (index == paramsArr.length - 1) ? (object == null ? "" : object) : getValueDoWhile( | ||||
| object, paramsArr, ++index); | object, paramsArr, ++index); | ||||