| @@ -55,17 +55,18 @@ import org.slf4j.LoggerFactory; | |||||
| @SuppressWarnings("unchecked") | @SuppressWarnings("unchecked") | ||||
| public abstract class ExcelExportBase extends ExportBase { | public abstract class ExcelExportBase extends ExportBase { | ||||
| private static final Logger LOGGER = LoggerFactory.getLogger(ExcelExportBase.class); | |||||
| private static final Logger LOGGER = LoggerFactory | |||||
| .getLogger(ExcelExportBase.class); | |||||
| private int currentIndex = 0; | |||||
| private int currentIndex = 0; | |||||
| protected ExcelType type = ExcelType.HSSF; | |||||
| protected ExcelType type = ExcelType.HSSF; | |||||
| private Map<Integer, Double> statistics = new HashMap<Integer, Double>(); | |||||
| private Map<Integer, Double> statistics = new HashMap<Integer, Double>(); | |||||
| private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00"); | private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00"); | ||||
| private IExcelExportStyler excelExportStyler; | |||||
| protected IExcelExportStyler excelExportStyler; | |||||
| /** | /** | ||||
| * 创建 最主要的 Cells | * 创建 最主要的 Cells | ||||
| @@ -88,12 +89,12 @@ public abstract class ExcelExportBase extends ExportBase { | |||||
| if (entity.getList() != null) { | if (entity.getList() != null) { | ||||
| Collection<?> list = getListCellValue(entity, t); | Collection<?> list = getListCellValue(entity, t); | ||||
| int listC = 0; | int listC = 0; | ||||
| if(list!=null&&list.size()>0){ | |||||
| for (Object obj : list) { | |||||
| createListCells(patriarch, index + listC, cellNum, obj, entity.getList(), sheet, | |||||
| workbook); | |||||
| listC++; | |||||
| } | |||||
| if (list != null && list.size() > 0) { | |||||
| for (Object obj : list) { | |||||
| createListCells(patriarch, index + listC, cellNum, obj, entity.getList(), | |||||
| sheet, workbook); | |||||
| listC++; | |||||
| } | |||||
| } | } | ||||
| cellNum += entity.getList().size(); | cellNum += entity.getList().size(); | ||||
| if (list != null && list.size() > maxHeight) { | if (list != null && list.size() > maxHeight) { | ||||
| @@ -181,7 +182,8 @@ public abstract class ExcelExportBase extends ExportBase { | |||||
| } | } | ||||
| private int createIndexCell(Row row, int index, ExcelExportEntity excelExportEntity) { | private int createIndexCell(Row row, int index, ExcelExportEntity excelExportEntity) { | ||||
| if (excelExportEntity.getName().equals("序号")&&excelExportEntity.getFormat()!=null&&excelExportEntity.getFormat().equals(PoiBaseConstants.IS_ADD_INDEX)) { | |||||
| if (excelExportEntity.getName().equals("序号") && excelExportEntity.getFormat() != null | |||||
| && excelExportEntity.getFormat().equals(PoiBaseConstants.IS_ADD_INDEX)) { | |||||
| createStringCell(row, 0, currentIndex + "", | createStringCell(row, 0, currentIndex + "", | ||||
| index % 2 == 0 ? getStyles(false, null) : getStyles(true, null), null); | index % 2 == 0 ? getStyles(false, null) : getStyles(true, null), null); | ||||
| currentIndex = currentIndex + 1; | currentIndex = currentIndex + 1; | ||||
| @@ -19,6 +19,7 @@ import org.apache.poi.ss.usermodel.BuiltinFormats; | |||||
| import org.apache.poi.ss.usermodel.CellStyle; | import org.apache.poi.ss.usermodel.CellStyle; | ||||
| import org.apache.poi.ss.usermodel.Workbook; | import org.apache.poi.ss.usermodel.Workbook; | ||||
| import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | ||||
| import org.jeecgframework.poi.excel.entity.params.ExcelForEachParams; | |||||
| /** | /** | ||||
| * 抽象接口提供两个公共方法 | * 抽象接口提供两个公共方法 | ||||
| @@ -67,4 +68,9 @@ public abstract class AbstractExcelExportStyler implements IExcelExportStyler { | |||||
| return null; | return null; | ||||
| } | } | ||||
| @Override | |||||
| public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams) { | |||||
| return null; | |||||
| } | |||||
| } | } | ||||
| @@ -17,6 +17,7 @@ package org.jeecgframework.poi.excel.export.styler; | |||||
| import org.apache.poi.ss.usermodel.CellStyle; | import org.apache.poi.ss.usermodel.CellStyle; | ||||
| import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | ||||
| import org.jeecgframework.poi.excel.entity.params.ExcelForEachParams; | |||||
| /** | /** | ||||
| * Excel导出样式接口 | * Excel导出样式接口 | ||||
| @@ -47,4 +48,12 @@ public interface IExcelExportStyler { | |||||
| */ | */ | ||||
| public CellStyle getStyles(boolean noneStyler, ExcelExportEntity entity); | public CellStyle getStyles(boolean noneStyler, ExcelExportEntity entity); | ||||
| /** | |||||
| * 模板使用的样式设置 | |||||
| * @param isSingle | |||||
| * @param excelForEachParams | |||||
| * @return | |||||
| */ | |||||
| public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams); | |||||
| } | } | ||||
| @@ -28,6 +28,7 @@ import java.util.Set; | |||||
| import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
| import org.apache.poi.ss.usermodel.Cell; | import org.apache.poi.ss.usermodel.Cell; | ||||
| import org.apache.poi.ss.usermodel.CellStyle; | |||||
| import org.apache.poi.ss.usermodel.Drawing; | import org.apache.poi.ss.usermodel.Drawing; | ||||
| import org.apache.poi.ss.usermodel.Row; | import org.apache.poi.ss.usermodel.Row; | ||||
| import org.apache.poi.ss.usermodel.Sheet; | import org.apache.poi.ss.usermodel.Sheet; | ||||
| @@ -62,7 +63,8 @@ import org.slf4j.LoggerFactory; | |||||
| */ | */ | ||||
| public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | ||||
| private static final Logger LOGGER = LoggerFactory.getLogger(ExcelExportOfTemplateUtil.class); | |||||
| private static final Logger LOGGER = LoggerFactory | |||||
| .getLogger(ExcelExportOfTemplateUtil.class); | |||||
| /** | /** | ||||
| * 缓存TEMP 的for each创建的cell ,跳过这个cell的模板语法查找,提高效率 | * 缓存TEMP 的for each创建的cell ,跳过这个cell的模板语法查找,提高效率 | ||||
| @@ -416,11 +418,16 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| //所有的cell创建一遍 | //所有的cell创建一遍 | ||||
| for (int i = 0; i < rowspan; i++) { | for (int i = 0; i < rowspan; i++) { | ||||
| for (int j = columnIndex, max = columnIndex + colspan; j < max; j++) { | for (int j = columnIndex, max = columnIndex + colspan; j < max; j++) { | ||||
| if (row.getCell(j) == null){ | |||||
| if (row.getCell(j) == null) { | |||||
| row.createCell(j); | row.createCell(j); | ||||
| row.getCell(j).setCellStyle(row.getRowNum() % 2 == 0 ? getStyles(false, null) : getStyles(true, null)); | |||||
| CellStyle style = row.getRowNum() % 2 == 0 | |||||
| ? getStyles(false, columns.get(columnIndex - j)) | |||||
| : getStyles(true, columns.get(columnIndex - j)); | |||||
| //返回的styler不为空时才使用,否则使用Excel设置的,更加推荐Excel设置的样式 | |||||
| if (style != null) | |||||
| row.getCell(j).setCellStyle(style); | |||||
| } | } | ||||
| } | } | ||||
| if (i < rowspan - 1) { | if (i < rowspan - 1) { | ||||
| row = row.getSheet().getRow(row.getRowNum() + 1); | row = row.getSheet().getRow(row.getRowNum() + 1); | ||||
| @@ -486,6 +493,10 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| } | } | ||||
| private CellStyle getStyles(boolean isSingle, ExcelForEachParams excelForEachParams) { | |||||
| return excelExportStyler.getTemplateStyles(isSingle, excelForEachParams); | |||||
| } | |||||
| /** | /** | ||||
| * 设置合并单元格的样式 | * 设置合并单元格的样式 | ||||
| * @param row | * @param row | ||||
| @@ -543,8 +554,8 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| cellStringString = cell.getStringCellValue(); | cellStringString = cell.getStringCellValue(); | ||||
| if (StringUtils.isBlank(cellStringString) && colspan + startIndex <= index) { | if (StringUtils.isBlank(cellStringString) && colspan + startIndex <= index) { | ||||
| throw new ExcelExportException("for each 当中存在空字符串,请检查模板"); | throw new ExcelExportException("for each 当中存在空字符串,请检查模板"); | ||||
| } else | |||||
| if (StringUtils.isBlank(cellStringString) && colspan + startIndex > index) { | |||||
| } else if (StringUtils.isBlank(cellStringString) | |||||
| && colspan + startIndex > index) { | |||||
| //读取是判断,跳过,数据为空,但是不是第一次读这一列,所以可以跳过 | //读取是判断,跳过,数据为空,但是不是第一次读这一列,所以可以跳过 | ||||
| columns.add(new ExcelForEachParams(null, cell.getCellStyle(), (short) 0)); | columns.add(new ExcelForEachParams(null, cell.getCellStyle(), (short) 0)); | ||||
| continue; | continue; | ||||
| @@ -128,7 +128,7 @@ public final class PoiFunctionUtil { | |||||
| return isGt(second, first); | return isGt(second, first); | ||||
| } else if ("==".endsWith(operator)) { | } else if ("==".endsWith(operator)) { | ||||
| if (first != null && second != null) { | if (first != null && second != null) { | ||||
| return first.equals(second); | |||||
| return eq(first, second); | |||||
| } | } | ||||
| return first == second; | return first == second; | ||||
| } else if ("!=".endsWith(operator)) { | } else if ("!=".endsWith(operator)) { | ||||
| @@ -141,6 +141,27 @@ public final class PoiFunctionUtil { | |||||
| } | } | ||||
| } | } | ||||
| /** | |||||
| * 判断两个对象是不是相等 | |||||
| * @param first | |||||
| * @param second | |||||
| * @return | |||||
| */ | |||||
| private static boolean eq(Object first, Object second) { | |||||
| //要求两个对象当中至少一个对象不是字符串才进行数字类型判断 | |||||
| if (!(first instanceof String) || !(second instanceof String)) { | |||||
| try { | |||||
| double f = Double.parseDouble(first.toString()); | |||||
| double s = Double.parseDouble(second.toString()); | |||||
| return f == s; | |||||
| } catch (NumberFormatException e) { | |||||
| //可能存在的错误,忽略继续进行 | |||||
| } | |||||
| } | |||||
| return first.equals(second); | |||||
| } | |||||
| /** | /** | ||||
| * 前者是不是大于后者 | * 前者是不是大于后者 | ||||
| * @param first | * @param first | ||||
| @@ -158,5 +179,5 @@ public final class PoiFunctionUtil { | |||||
| double two = Double.valueOf(second.toString()); | double two = Double.valueOf(second.toString()); | ||||
| return one > two; | return one > two; | ||||
| } | } | ||||
| } | } | ||||