GitHub user bengbengbalabalabeng added a comment to the discussion:
这样的表格我如何才能填充?
基于我的了解,fesod 暂时不支持多行循环填充。
不过,可以通过“手动拆分数据 + 合并单元格”的方式实现同样的效果:
版本:v1.3.0
<details>
<summary>查看完整 Demo</summary>
- Model
```java
@Data
public static class ExcelModel {
@ExcelProperty("项号")
private Integer no;
@ExcelProperty("编码")
private String code;
@ExcelProperty("名称")
private String name;
@ContentLoopMerge(eachRow = 2)
@ExcelProperty("箱数")
private Integer totalCount;
@ContentLoopMerge(eachRow = 2)
@ExcelProperty("毛重")
private Integer totalWeight;
@ExcelProperty("数量")
private Integer count1;
@ExcelIgnore
private Integer count2;
public List<ExcelModel> splitModels() {
List<ExcelModel> result = new ArrayList<>();
ExcelModel one = new ExcelModel();
one.setNo(no);
one.setCode(code);
one.setName("密封垫");
one.setTotalCount(totalCount);
one.setTotalWeight(totalWeight);
one.setCount1(count1);
result.add(one);
ExcelModel two = new ExcelModel();
two.setCode("填写申报要素→");
two.setName("材质");
two.setCount1(count2);
result.add(two);
return result;
}
}
```
- mock datas
```java
public List<ExcelModel> datas() {
List<ExcelModel> list = new ArrayList<>();
ExcelModel m1 = new ExcelModel();
m1.setNo(1);
m1.setCode("A001");
m1.setTotalCount(10);
m1.setTotalWeight(25);
m1.setCount1(100);
m1.setCount2(200);
list.add(m1);
ExcelModel m2 = new ExcelModel();
m2.setNo(2);
m2.setCode("A002");
m2.setTotalCount(5);
m2.setTotalWeight(12);
m2.setCount1(50);
m2.setCount2(80);
list.add(m2);
ExcelModel m3 = new ExcelModel();
m3.setNo(3);
m3.setCode("A003");
m3.setTotalCount(8);
m3.setTotalWeight(18);
m3.setCount1(60);
m3.setCount2(120);
list.add(m3);
return list.stream()
.<ExcelModel>mapMulti((model, downstream) -> {
List<ExcelModel> models = model.splitModels();
for (ExcelModel em : models) {
downstream.accept(em);
}
})
.collect(Collectors.toCollection(LinkedList::new));
}
```
- demo
```java
FastExcel.write("/pathname/output.xlsx")
.head(ExcelModel.class)
.sheet()
.doWrite(datas());
```
</details>
Output:
<img width="536" height="186" alt="08401f1ce9fce669f1d0ba8d74f3e18c"
src="https://github.com/user-attachments/assets/36f88058-9ccd-4398-960c-ee107c1d129d"
/>
通过这种方式,就可以生成与你示例中一致的两行结构。还可以配合自定义样式进一步调整格式。如果有其他好的想法也欢迎补充。
GitHub link:
https://github.com/apache/fesod/discussions/832#discussioncomment-15616991
----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]