diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/CsvExportUtil.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/CsvExportUtil.java index acaae11..8da1340 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/CsvExportUtil.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/CsvExportUtil.java @@ -60,7 +60,6 @@ public final class CsvExportUtil { * @param pojoClass Excel对象Class */ public static IWriter exportCsv(CsvExportParams params, Class pojoClass, OutputStream outputStream) { - CsvExportUtil ce = new CsvExportUtil(); return new CsvExportService(outputStream, params, pojoClass); } diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelForEachParams.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelForEachParams.java index bf2d8ec..0e4c709 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelForEachParams.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelForEachParams.java @@ -1,6 +1,7 @@ package cn.afterturn.easypoi.excel.entity.params; import lombok.Data; +import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.CellStyle; import java.io.Serializable; @@ -51,7 +52,9 @@ public class ExcelForEachParams implements Serializable { * 行合并 */ private boolean collectCell; - + /** + * 需要统计 + */ private boolean needSum; public ExcelForEachParams() { @@ -72,4 +75,9 @@ public class ExcelForEachParams implements Serializable { } + public void addConstValue(int num) { + if (StringUtils.isNotEmpty(constValue)) { + this.constValue = Integer.parseInt(constValue) + 1 + ""; + } + } } diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java index d4e68f4..218bc95 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java @@ -134,17 +134,18 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { .replace(FOREACH, EMPTY).replace(START_STR, EMPTY); String[] keys = name.replaceAll("\\s{1,}", " ").trim().split(" "); Collection datas = (Collection) PoiPublicUtil.getParamsValue(keys[0], map); - Object[] columnsInfo = getAllDataColumns(cell, name.replace(keys[0], EMPTY), - mergedRegionHelper); if (datas == null) { return; } + Object[] columnsInfo = getAllDataColumns(cell, name.replace(keys[0], EMPTY), + mergedRegionHelper); Iterator its = datas.iterator(); int rowspan = (Integer) columnsInfo[0], colspan = (Integer) columnsInfo[1]; @SuppressWarnings("unchecked") List columns = (List) columnsInfo[2]; - Row row = null; - int rowIndex = cell.getRow().getRowNum() + 1; + Row row = null; + int rowIndex = cell.getRow().getRowNum() + 1; + ExcelForEachParams indexColumn = getIndexColumn(columns); //处理当前行 int loopSize = 0; if (its.hasNext()) { @@ -165,12 +166,23 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { while (its.hasNext()) { Object t = its.next(); row = createRow(rowIndex, cell.getSheet(), isCreate, rowspan); + indexColumn.addConstValue(1); loopSize = setForeachRowCellValue(isCreate, row, cell.getColumnIndex(), t, columns, map, rowspan, colspan, mergedRegionHelper)[0]; rowIndex += rowspan + loopSize - 1; } } + private ExcelForEachParams getIndexColumn(List columns) { + for (int i = 0; i < columns.size(); i++) { + if (INDEX.equals(columns.get(i).getConstValue())) { + columns.get(i).setConstValue("1"); + return columns.get(i); + } + } + return new ExcelForEachParams(); + } + /** * 下移数据 * @@ -1021,6 +1033,11 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { params.setName(null); params.setConstValue(EMPTY); } + //是否是当前索引 + if (INDEX.equals(name)) { + params.setName(null); + params.setConstValue(INDEX); + } //获取合并单元格的数据 if (mergedRegionHelper.isMergedRegion(cell.getRowIndex() + 1, cell.getColumnIndex())) { Integer[] colAndrow = mergedRegionHelper.getRowAndColSpan(cell.getRowIndex() + 1, diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiElUtil.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiElUtil.java index 95903bd..fb50ce1 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiElUtil.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiElUtil.java @@ -48,6 +48,7 @@ public final class PoiElUtil { public static final String EMPTY = ""; public static final String CONST = "'"; public static final String NULL = "&NULL&"; + public static final String INDEX = "&INDEX&"; public static final String LEFT_BRACKET = "("; public static final String RIGHT_BRACKET = ")"; public static final String CAL = "cal:"; diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiReflectorUtil.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiReflectorUtil.java index 917565d..7e1e975 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiReflectorUtil.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiReflectorUtil.java @@ -1,9 +1,6 @@ package cn.afterturn.easypoi.util; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.ReflectPermission; +import java.lang.reflect.*; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -23,10 +20,11 @@ public final class PoiReflectorUtil { private static final Map, PoiReflectorUtil> CACHE_REFLECTOR = new ConcurrentHashMap, PoiReflectorUtil>(); - private Map getMethods = new HashMap(); - private Map setMethods = new HashMap(); - private Map enumMethods = new HashMap(); - private List fieldList = new ArrayList(); + private Map getMethods = new HashMap<>(); + private Map setMethods = new HashMap<>(); + private Map enumMethods = new HashMap<>(); + private Map publicField = new HashMap<>(); + private List fieldList = new ArrayList<>(); private Class type; @@ -203,6 +201,9 @@ public final class PoiReflectorUtil { if (field.isAccessible() && !"serialVersionUID".equalsIgnoreCase(field.getName())) { this.fieldList.add(field); } + if (Modifier.isPublic(field.getModifiers())) { + this.publicField.put(field.getName(),field); + } } if (clazz.getSuperclass() != null) { addFields(clazz.getSuperclass()); @@ -297,6 +298,12 @@ public final class PoiReflectorUtil { } public Method getGetMethod(String propertyName) { + if (publicField.get(propertyName) != null){ + try { + return this.getClass().getMethod("publicField",Void.class); + } catch (NoSuchMethodException e) { + } + } Method method = getMethods.get(propertyName); if (method == null) { throw new RuntimeException( @@ -330,6 +337,13 @@ public final class PoiReflectorUtil { } catch (Exception ex) { } } + if (publicField.get(property) != null){ + try { + value = publicField.get(property).get(obj); + } catch (IllegalAccessException e) { + + } + } return value; } @@ -342,6 +356,14 @@ public final class PoiReflectorUtil { * @return */ public boolean setValue(Object obj, String property, Object object) { + if (publicField.get(property) != null){ + try { + publicField.get(property).set(obj, object); + return true; + } catch (IllegalAccessException e) { + + } + } Method m = setMethods.get(property); if (m != null) { try { @@ -377,4 +399,6 @@ public final class PoiReflectorUtil { throw new RuntimeException(e); } } + + public void publicField(){} } diff --git a/pom.xml b/pom.xml index 4557053..cd0b4bb 100644 --- a/pom.xml +++ b/pom.xml @@ -46,7 +46,7 @@ 4.1.3 - 4.1.0 + 4.1.1 2.9.1 16.0.1 3.2.1