easyexcel实现导出excel
时间: 2023-11-21 15:55:26 浏览: 252
以下是使用EasyExcel实现导出Excel的示例代码:
```python
// 定义Excel表头
List<List<String>> head = new ArrayList<>();
List<String> headCoulumn1 = new ArrayList<>();
headCoulumn1.add("姓名");
List<String> headCoulumn2 = new ArrayList<>();
headCoulumn2.add("年龄");
List<String> headCoulumn3 = new ArrayList<>();
headCoulumn3.add("性别");
head.add(headCoulumn1);
head.add(headCoulumn2);
head.add(headCoulumn3);
// 定义Excel表格内容
List<List<Object>> data = new ArrayList<>();
List<Object> rowData1 = new ArrayList<>();
rowData1.add("张三");
rowData1.add(18);
rowData1.add("男");
List<Object> rowData2 = new ArrayList<>();
rowData2.add("李四");
rowData2.add(20);
rowData2.add("女");
data.add(rowData1);
data.add(rowData2);
// 导出Excel
String fileName = "test.xlsx";
String sheetName = "Sheet1";
ExcelWriter excelWriter = EasyExcel.write(fileName).build();
WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).build();
excelWriter.write(head, writeSheet);
excelWriter.write(data, writeSheet);
excelWriter.finish();
```
以上代码中,我们首先定义了Excel表头和表格内容,然后使用EasyExcel的API来导出Excel文件。其中,`EasyExcel.write(fileName)`用于指定导出的文件名,`EasyExcel.writerSheet(sheetName)`用于指定导出的Sheet名称,`excelWriter.write(head, writeSheet)`用于写入表头,`excelWriter.write(data, writeSheet)`用于写入表格内容,`excelWriter.finish()`用于完成Excel文件的导出。
阅读全文