封装一个Excel工具类,com.alibaba easyexcel2.2.3
public class ExcelHelper {
public static String writeExcel(List list ,Class clazz) {
//获取系统文件路径
String tmpPath = System.getProperty("java.io.tmpdir")+ File.separator;
//获取文件名
String fileName = System.currentTimeMillis() + ".xlsx";
String filePath = tmpPath + fileName;
writeExcelFile(list,filePath,clazz);
return filePath;
}
public static String writeExcelFile(List list,String filePath ,Class clazz) {
File file = new File(filePath);
if (!file.exists()) {
try {
log.info("将导出{}条数据,文件路径{}",list.size(),filePath);
file.createNewFile();
} catch (IOException e) {
log.error("文件创建失败:{}",e.getMessage());
return "";
}
}
//执行写入操作
EasyExcel.write(filePath,clazz).sheet("sheet1").doWrite(list);
//(拓展)一般会在这调用文件上传接口上传到服务器,成功后将本地文件删除
return file.getName();
}
}
@Data
@ColumnWidth(value = 30)
public class ExcelUserInfoBO implements Serializable {
private static final long serialVersionUID = 3901876623626426187L;
@ExcelProperty(value = "用户id",index = 0)
private Long userId;
@ExcelProperty(value = "用户姓名",index = 1)
private String userName;
@ExcelProperty(value = "权限id",index = 2)
private Long authId;
@ExcelProperty(value ="密码" ,index = 3)
private String userPassword;
@ExcelProperty(value = "权限名称",index = 4)
private String authName;
}
eg:
C:UsersADMINI~1AppDataLocalTemp\1637054665712.xlsx
表格内容;



