依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
写入本地的excel
1. 实体类
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExcelDataDemo {
@ExcelProperty("学生编号") //excel表头名称
private Integer stuNum;
@ExcelProperty("学生名称")
private String stuName;
}
2. Api写法
@SpringBootTest
public class EasyExcelTest {
@Test
public void test(){
//实现写操作
//1. 设置要写入的文件路径
String filename = "E:\\excel\\write.xlsx";
//2. 准备数据
List<ExcelDataDemo> data = new ArrayList<>();
data.add(new ExcelDataDemo(201430317,"诺亚"));
data.add(new ExcelDataDemo(201430329,"雷杰多"));
data.add(new ExcelDataDemo(201430365,"撒加"));
//3. 调用easyexcel的方法实现写操作。**写完后,文件流会自动关闭**
// 参数1:文件名称 参数2:实体类
// sheet:excel文件底部栏的sheet
EasyExcel.write(filename, ExcelDataDemo.class).sheet("学生列表").doWrite(data);
}
}
生成write.xlsx文件:
内容: