我正在开发带有层次结构数据的 Excel 报告。使用 JETT(java excel 模板翻译器),当有集合集合时,我想从主循环中复制行。这是一个简短的例子。
public class JettTest {
@Test
public void run() throws IOException {
ClassLoader classLoader = getClass().getClassLoader();
InputStream template = classLoader.getResourceAsStream("template.xlsx");
try (
XSSFWorkbook wb = new XSSFWorkbook(template);
FileOutputStream fos = new FileOutputStream("target/output.xlsx")
) {
Map<String, Object> params = new HashMap<>();
Parent parent1 = new Parent("parent1", Arrays.asList("child1", "child2"));
Parent parent2 = new Parent("parent2", Arrays.asList("childX", "childY"));
List<Parent> parents = Arrays.asList(parent1, parent2);
params.put("parents", parents);
ExcelTransformer transformer = new ExcelTransformer();
transformer.transform(wb, params);
wb.write(fos);
}
}
public class Parent {
private String name;
private List<String> children;
public Parent(String name, List<String> children) {
this.name = name;
this.children = children;
}
public String getName() {
return name;
}
public List<String> getChildren() {
return children;
}
}
}
Excel 模板是
A1 是
<jt:forEach items="${parents}" var="parent">${parent.name}
B1 是
<jt:forEach items="${parent.children}" var="child">${child}</jt:forEach></jt:forEach>
这给了我
看起来不错,但是,我需要复制父名并获取
请问有什么想法吗?