使用easyexcel不创建实体类导出数据
时间: 2024-04-24 13:06:24 浏览: 242
使用easyexcel不创建实体类导出数据可以使用Map作为数据源。
首先,我们需要创建一个Map类型的数据源,其中key为表头,value为数据。例如:
```java
List<Map<String, Object>> dataList = new ArrayList<>();
Map<String, Object> dataMap = new LinkedHashMap<>();
dataMap.put("姓名", "张三");
dataMap.put("年龄", 20);
dataMap.put("性别", "男");
dataList.add(dataMap);
```
然后,我们可以使用EasyExcel提供的WriteMapSheet方法将数据写入Excel文件中。例如:
```java
ExcelWriter excelWriter = EasyExcel.write(fileName).build();
WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).build();
excelWriter.write(dataList, writeSheet);
excelWriter.finish();
```
完整代码示例:
```java
public static void writeExcel(String fileName, String sheetName, List<Map<String, Object>> dataList) {
ExcelWriter excelWriter = EasyExcel.write(fileName).build();
WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).build();
excelWriter.write(dataList, writeSheet);
excelWriter.finish();
}
```
使用方法:
```java
List<Map<String, Object>> dataList = new ArrayList<>();
Map<String, Object> dataMap = new LinkedHashMap<>();
dataMap.put("姓名", "张三");
dataMap.put("年龄", 20);
dataMap.put("性别", "男");
dataList.add(dataMap);
writeExcel("test.xlsx", "Sheet1", dataList);
```
阅读全文