Map<String, List<PortReportResultVo>> relayLinkAndPortMap = new HashMap<>(); relayLinkAndPortMap.put("RelayLingId" + "_" + "RelayLinkName",portReportResultVos); 遍历这个map,key为表头,value为数据,在一个sheet页中用easyExcel实现
时间: 2024-02-01 21:14:30 浏览: 66
HashMap遍历
5星 · 资源好评率100%
可以按照以下步骤实现:
1. 创建一个ExcelWriter对象,指定输出文件路径和sheet名称:
```
String outputFilePath = "output.xlsx";
ExcelWriter writer = new ExcelWriter(outputFilePath);
Sheet sheet = writer.createSheet("Sheet1");
```
2. 创建表头,添加到sheet中:
```
Row row = sheet.createRow(0);
int columnIndex = 0;
for (String key : relayLinkAndPortMap.keySet()) {
Cell cell = row.createCell(columnIndex++);
cell.setCellValue(key);
}
```
3. 遍历map中的数据,逐行添加到sheet中:
```
int rowIndex = 1;
for (Map.Entry<String, List<PortReportResultVo>> entry : relayLinkAndPortMap.entrySet()) {
row = sheet.createRow(rowIndex++);
columnIndex = 0;
row.createCell(columnIndex++).setCellValue(entry.getKey()); // 添加表头
for (PortReportResultVo vo : entry.getValue()) {
row.createCell(columnIndex++).setCellValue(vo.getPortName());
row.createCell(columnIndex++).setCellValue(vo.getPortStatus());
// 添加其他需要显示的字段
}
}
```
4. 最后,关闭ExcelWriter对象:
```
writer.finish();
```
阅读全文