如何使用easyexcel写excel
时间: 2023-10-12 08:05:09 浏览: 86
您可以参考以下步骤使用EasyExcel写Excel:
1.添加EasyExcel依赖:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>{latestVersion}</version>
</dependency>
```
2.创建Excel数据对象,构造excel的标题和数据:
```java
@Data
public class DemoData {
@ExcelProperty("姓名")
private String name;
@ExcelProperty("年龄")
private Integer age;
}
List<DemoData> dataList = new ArrayList<>();
dataList.add(new DemoData("张三", 20));
dataList.add(new DemoData("李四", 21));
```
3.写入Excel文件:
```java
// 写法1:使用EasyExcelWriter
String fileName = "/path/to/demo.xlsx";
EasyExcel.write(fileName, DemoData.class).sheet("示例").doWrite(dataList);
// 写法2:使用EasyExcelFactory
OutputStream outputStream = new FileOutputStream(new File(fileName));
ExcelWriter writer = EasyExcelFactory.getWriter(outputStream);
Sheet sheet = new Sheet(1, 0, DemoData.class);
sheet.setSheetName("示例");
writer.write(dataList, sheet);
writer.finish();
```
以上就是使用EasyExcel写Excel的简单步骤,具体细节可参考官方文档。祝您写Excel愉快!
阅读全文