diff --git a/src/main/java/com/kvn/poi/exp/processor/ForeachRowProcessor.java b/src/main/java/com/kvn/poi/exp/processor/ForeachRowProcessor.java index 9266a97e8c699adfe3303bd439250870afddb4f4..8b893350456ed936f85202cb5ed5aad0244bb6e9 100644 --- a/src/main/java/com/kvn/poi/exp/processor/ForeachRowProcessor.java +++ b/src/main/java/com/kvn/poi/exp/processor/ForeachRowProcessor.java @@ -1,12 +1,10 @@ package com.kvn.poi.exp.processor; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; +import com.google.common.collect.Lists; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; @@ -116,6 +114,21 @@ public class ForeachRowProcessor implements RowProcessor { Map> cellMap = tpRow.getCellMap(); + if (ls.isEmpty()) {// 如果只是想插入一个空行 + // 获取行,这个行应该是el表达式的行,这个应该是一个动态值 + Integer integer = getDealRowNum(cellMap); + XSSFRow curRow = sheet.getRow(integer);//这里应该能动态获取到el表达式的行号 + // 处理当前行里面的每个单元格:替换内容 + Map map = cellMap.get(integer); + for (Integer key : map.keySet()) { + String cellContent = map.get(key) == null ? "" : (String) map.get(key); + XSSFCell c = curRow.getCell(key); + // 清洗el表达式 + cleanElExpression(ls, peContext, cellContent, c); + } + return; + } + for (int i = 0; i < ls.size(); i++) { Object rootObjectItem = ls.get(i); for (Integer key1 : cellMap.keySet()) { // key1为row行号 @@ -188,6 +201,37 @@ public class ForeachRowProcessor implements RowProcessor { } } + /** + * 获取需要处理的行号 + * + * @param cellMap + * @return el表达式所在的行号 + */ + private static Integer getDealRowNum(Map> cellMap) { + Set integers = cellMap.keySet(); + List list = Lists.newArrayList(integers); + Integer integer = list.get(0);// 动态行号 + return integer; + } + + /** + * 把el表达式的标记抠下来 + * + * @param ls + * @param peContext + * @param cellContent + * @param c + */ + private static void cleanElExpression(List ls, PoiExporterContext peContext, String cellContent, XSSFCell c) { + Expression expression = peContext.getSpelExpParser().parseExpression(cellContent, new TemplateParserContext()); + try { + expression.getValue(PoiExporterContext.EVAL_CONTEXT, ls, String.class); + c.setCellValue(cellContent); + } catch (EvaluationException e) { + c.setCellValue(""); + } + } + @Override public int support(XSSFRow row) { for (int k = 0; k <= row.getLastCellNum(); k++) { diff --git a/src/test/java/com/kvn/poi/export_test/ForeachTest.java b/src/test/java/com/kvn/poi/export_test/ForeachTest.java index 794844249d03fcf0b8070fb2500af3fdf509ea8d..1799d25eb48703711086826b96d9900aeaf92c02 100644 --- a/src/test/java/com/kvn/poi/export_test/ForeachTest.java +++ b/src/test/java/com/kvn/poi/export_test/ForeachTest.java @@ -24,7 +24,8 @@ public class ForeachTest { Order order2 = MockUtil.randomInstanceOfNonCollection(Order.class); Order order3 = MockUtil.randomInstanceOfNonCollection(Order.class); order1.setOrderNo(null); - List list = Lists.newArrayList(order1); + // List list = Lists.newArrayList(order1); + List list = Lists.newArrayList(); rootObjectMap.put("list", list); OutputStream des = new FileOutputStream("C:\\Users\\duguq\\Desktop\\excelexport\\test.xlsx"); InputStream is = ForeachTest.class.getClassLoader().getResourceAsStream("template/01_Template_foreach.xlsx"); diff --git a/src/test/java/com/kvn/poi/export_test/MultiPoiForeachTest.java b/src/test/java/com/kvn/poi/export_test/MultiPoiForeachTest.java index 1b7171df19314ea7c24749f4cfe734401b0466bf..d44fa7bb51ef05bab564661c367fae78cb08c508 100644 --- a/src/test/java/com/kvn/poi/export_test/MultiPoiForeachTest.java +++ b/src/test/java/com/kvn/poi/export_test/MultiPoiForeachTest.java @@ -21,20 +21,22 @@ public class MultiPoiForeachTest { public static void main(String[] args) throws Exception { Map rootObjectMap = Maps.newHashMap(); - Order order1 = MockUtil.randomInstanceOfNonCollection(Order.class); - Order order2 = MockUtil.randomInstanceOfNonCollection(Order.class); - Order order3 = MockUtil.randomInstanceOfNonCollection(Order.class); + // Order order1 = MockUtil.randomInstanceOfNonCollection(Order.class); + // Order order2 = MockUtil.randomInstanceOfNonCollection(Order.class); + // Order order3 = MockUtil.randomInstanceOfNonCollection(Order.class); - List list = Lists.newArrayList(order1, order2, order3); + // List list = Lists.newArrayList(order1, order2, order3); + List list = Lists.newArrayList(); rootObjectMap.put("list", list); - Order order4 = MockUtil.randomInstanceOfNonCollection(Order.class); - Order order5 = MockUtil.randomInstanceOfNonCollection(Order.class); - List list2 = Lists.newArrayList(order4, order5); + // Order order4 = MockUtil.randomInstanceOfNonCollection(Order.class); + // Order order5 = MockUtil.randomInstanceOfNonCollection(Order.class); + // List list2 = Lists.newArrayList(order4, order5); + List list2 = Lists.newArrayList(); rootObjectMap.put("list2", list2); - OutputStream out = new FileOutputStream("E:\\rlt\\test.xlsx"); + OutputStream out = new FileOutputStream("C:\\Users\\duguq\\Desktop\\excelexport\\test.xlsx"); InputStream is = ForeachTest.class.getClassLoader().getResourceAsStream("template/02_Template_多个foreach.xlsx"); PoiExporter.export2Destination(is, rootObjectMap, out); }