【发布时间】:2021-09-05 19:22:27
【问题描述】:
我正在尝试使用如下所示的级联值生成报告:
| Country | City | Town |
--------------------------------------
| Country A | City X | Town 1 |
| Country A | City X | Town 2 |
| Country A | City Y | Town 1 |
| Country A | City Y | Town 2 |
| Country B | City Q | Town 1 |
| Country B | City Q | Town 2 |
| Country B | City T | Town 1 |
| Country B | City T | Town 2 |
我可以正确生成如上所示的 Country 和 City,但我为每个城市传递了一个 j 索引值,如下所示。但是,对于城镇,我还需要另一个索引变量,例如 k 和循环。
public MultipartFile exportData() throws IOException {
// code omitted for brevity
int rowCount = 0;
final List<CountryDTO> countryList = countryService.findAll();
int iSize = countryList.size();
for (int i = 0; i < iSize; i++) {
int jSize = countryList.get(i).getCityList().size();
for (int j = 0; j < jSize; j++) {
int kSize = countryList.get(i).getCityList().get(j).getTownList().size();
for (int j = 0; k < kSize; k++) {
Row row = sheet.createRow(rowCount++);
write(countryList.get(i), row, j, k);
}
}
}
// code omitted for brevity
}
private static void write(CountryDTO country, Row row, int j) {
Cell cell = row.createCell(0);
cell.setCellValue(country.getName());
cell = row.createCell(1);
cell.setCellValue(country.getCityList().get(j).getName());
cell = row.createCell(2);
cell.setCellValue(country.getCityList().get(j).getTownList().get(k).getName());
}
我不确定是否有更好的方法。由于我是使用 Java 生成报告的新手,我不知道如何继续使用以下方法(如果可以,我将使用这种方法,因为它已经在当前项目中使用过)。
【问题讨论】:
标签: java report export-to-csv reporting export-to-excel