Map<String, List<PortReportResultVo>> relayLinkAndPortMap = new HashMap<>(); relayLinkAndPortMap.put("RelayLingId" + "_" + "RelayLinkName",portReportResultVos); 遍历这个map,key为表头,value为数据,在一个sheet页中用easyExcel实现,导包也展示一下
时间: 2024-02-01 18:14:31 浏览: 129
好的,这里是遍历该Map并使用EasyExcel将数据导出到Excel文件的示例代码:
```java
// 导入EasyExcel的相关类
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
// 创建ExcelWriterBuilder对象
ExcelWriterBuilder writerBuilder = EasyExcel.write("output.xlsx");
// 遍历Map,将每个键值对写入Excel文件
for (Map.Entry<String, List<PortReportResultVo>> entry : relayLinkAndPortMap.entrySet()) {
// 获取表头和数据
String header = entry.getKey();
List<PortReportResultVo> data = entry.getValue();
// 创建ExcelWriterSheetBuilder对象,并指定表头和数据
ExcelWriterSheetBuilder sheetBuilder = writerBuilder.sheet(header);
sheetBuilder.head(PortReportResultVo.class);
sheetBuilder.doWrite(data);
}
// 关闭ExcelWriterBuilder对象
writerBuilder.finish();
```
需要注意的是,该代码需要引入EasyExcel的相关依赖,如下所示:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.3.0</version>
</dependency>
```
同时,该代码还需要将`PortReportResultVo`类替换为实际的数据类型。
阅读全文