Map<String, List<PortReportResultVo>> relayLinkAndPortMap = new HashMap<>(); 遍历这个map,value为要导出的数据,都在一个sheet页中分分成多块内容,每块内容的表头可以自定义为2行,用easyExcel实现详细代码,包括导包
时间: 2024-02-01 16:15:08 浏览: 67
java 读取csv 并转为Map<String,Object>
请参考以下代码:
```
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class ExcelExporter {
public static void export(Map<String, List<PortReportResultVo>> relayLinkAndPortMap) throws IOException {
ExcelWriterBuilder writerBuilder = EasyExcel.write("output.xlsx");
for (Map.Entry<String, List<PortReportResultVo>> entry : relayLinkAndPortMap.entrySet()) {
String sheetName = entry.getKey();
List<PortReportResultVo> data = entry.getValue();
WriteSheet sheet = EasyExcel.writerSheet(sheetName).build();
// 写入表头
sheet.setHead(createHeader());
// 写入数据
writerBuilder.sheet(sheetName)
.head(createHeader())
.registerWriteHandler(new CustomizedCellStyleStrategy()) // 自定义单元格样式
.doWrite(data);
}
writerBuilder.build().finish();
}
private static List<List<String>> createHeader() {
List<List<String>> header = new ArrayList<>();
header.add(Arrays.asList("列1", "列2", "列3"));
header.add(Arrays.asList("列1备注", "列2备注", "列3备注"));
return header;
}
}
```
其中,`PortReportResultVo` 为要导出的数据类型,`CustomizedCellStyleStrategy` 是自定义的单元格样式策略,可以根据需要进行修改。
需要导入的包有:
```
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Arrays;
```
阅读全文