Map<String, List<PortReportResultVo>> relayLinkAndPortMap = devPortReportByEs.getRelayLinkAndPortMap(); 遍历这个map,key为表头,value为数据,在一个sheet页中用easyExcel实现,导包也展示
时间: 2024-02-01 13:14:42 浏览: 75
List<Map<String, Object>>转化为List<JavaBean>工具类
4星 · 用户满意度95%
好的,你可以参考以下代码实现:
```
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class ExcelUtil {
/**
* 导出到Excel
*
* @param outputStream 输出流
* @param dataMap 数据集合,key为表头,value为数据
* @throws IOException
*/
public static void exportExcel(OutputStream outputStream, Map<String, List<PortReportResultVo>> dataMap) throws IOException {
List<PortReportResultVo> dataList = new ArrayList<>();
for (Map.Entry<String, List<PortReportResultVo>> entry : dataMap.entrySet()) {
// 添加表头
PortReportResultVo header = new PortReportResultVo();
header.setPortCode(entry.getKey());
dataList.add(header);
// 添加数据
List<PortReportResultVo> list = entry.getValue();
dataList.addAll(list);
}
// 导出Excel
EasyExcel.write(outputStream, PortReportResultVo.class)
.excelType(ExcelTypeEnum.XLSX)
.sheet("Sheet1")
.doWrite(dataList);
}
/**
* 导出到文件
*
* @param filePath 文件路径
* @param dataMap 数据集合,key为表头,value为数据
* @throws IOException
*/
public static void exportExcel(String filePath, Map<String, List<PortReportResultVo>> dataMap) throws IOException {
try (OutputStream outputStream = new FileOutputStream(filePath)) {
exportExcel(outputStream, dataMap);
}
}
/**
* 导出到文件
*
* @param filePath 文件路径
* @param dataMap 数据集合,key为表头,value为数据
* @throws IOException
*/
public static void exportExcel(String filePath, Map<String, List<PortReportResultVo>> dataMap, String sheetName) throws IOException {
try (OutputStream outputStream = new FileOutputStream(filePath)) {
exportExcel(outputStream, dataMap, sheetName);
}
}
/**
* 导出到Excel
*
* @param outputStream 输出流
* @param dataMap 数据集合,key为表头,value为数据
* @param sheetName sheet页名称
* @throws IOException
*/
public static void exportExcel(OutputStream outputStream, Map<String, List<PortReportResultVo>> dataMap, String sheetName) throws IOException {
List<PortReportResultVo> dataList = new ArrayList<>();
for (Map.Entry<String, List<PortReportResultVo>> entry : dataMap.entrySet()) {
// 添加表头
PortReportResultVo header = new PortReportResultVo();
header.setPortCode(entry.getKey());
dataList.add(header);
// 添加数据
List<PortReportResultVo> list = entry.getValue();
dataList.addAll(list);
}
// 导出Excel
Sheet sheet = new Sheet(1, 0, PortReportResultVo.class);
sheet.setSheetName(sheetName);
EasyExcel.write(outputStream, PortReportResultVo.class)
.excelType(ExcelTypeEnum.XLSX)
.sheet(sheet)
.doWrite(dataList);
}
}
/**
* Excel行数据类
*/
class PortReportResultVo extends BaseRowModel {
@ExcelProperty(value = "端口编码", index = 0)
private String portCode;
@ExcelProperty(value = "端口名称", index = 1)
private String portName;
@ExcelProperty(value = "设备编码", index = 2)
private String deviceCode;
@ExcelProperty(value = "设备名称", index = 3)
private String deviceName;
// 省略getter/setter方法
}
```
其中,PortReportResultVo类为Excel行数据类,用于存储一行数据。ExcelUtil类中的exportExcel方法可以将Map<String, List<PortReportResultVo>>数据导出为一个Excel文件。你可以根据需要修改ExcelUtil类中的代码,并添加相应的依赖包,比如:
```
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>
```
阅读全文