EasyExcel 2 上传 下载

    技术2026-06-10  3

    Maven依赖

    <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.1.6</version> </dependency>

    实体类

    package com.ruiguo.modal; import com.alibaba.excel.annotation.ExcelProperty; import java.io.Serializable; /** * excel 导入,导出实体类对象 * @author jiaruiguo */ public class UserInfoBean implements Serializable { @ExcelProperty(value = "ID", index = 0) private String userId; @ExcelProperty(value = "NAME", index = 1) private String userName; @ExcelProperty(value = "PHONE", index = 2) private String phoneNum; @ExcelProperty(value = "GENDER", index = 3) private String gender; @ExcelProperty(value = "HEIGHT", index = 4) private String height; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPhoneNum() { return phoneNum; } public void setPhoneNum(String phoneNum) { this.phoneNum = phoneNum; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getHeight() { return height; } public void setHeight(String height) { this.height = height; } @Override public String toString() { return "UserInfoBean{" + "userId='" + userId + '\'' + ", userName='" + userName + '\'' + ", phoneNum='" + phoneNum + '\'' + ", gender='" + gender + '\'' + ", height='" + height + '\'' + '}'; } }

    Excel导入

    /** * excel 文件导入 * @param inputStream * @param clazz */ public static List<? extends Object> importExcel(InputStream inputStream, Class clazz,List<? extends Object> list) { try { return EasyExcel.read(inputStream) .head(clazz) .sheet(0) .doReadSync(); } catch (Exception e) { e.printStackTrace(); } return null; }

    Excel导出

    /** * excel 文件导出 */ public static void exportExcel(OutputStream outputStream, Class clazz, List<? extends Object> list) { EasyExcel.write(outputStream,clazz) .excelType(ExcelTypeEnum.XLSX) .sheet("sheet1") .doWrite(list); }

    功能测试

    导入测试

    // 导入上传 String filePath = "F:\\Download\\inputExcel.xlsx"; List<UserInfoBean> list = new ArrayList<>(); try { InputStream inputStream = new FileInputStream(new File(filePath)); list = (List<UserInfoBean>) importExcel(inputStream,UserInfoBean.class,list); System.out.println("输出:" + list.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); }

    导出测试

    // 导出下载 OutputStream outputStream = null; List<UserInfoBean> list1 = new ArrayList<>(); for (int i = 0; i < 5; i++ ){ UserInfoBean userInfoBean = new UserInfoBean(); userInfoBean.setUserId("id_" + i); userInfoBean.setUserName("name_" + i); userInfoBean.setGender("男" + i); userInfoBean.setHeight("17" + i + "cm"); userInfoBean.setPhoneNum("12345"+ i); list1.add(userInfoBean); } try { outputStream = new FileOutputStream(new File("F:\\Download\\outExcel.xlsx")); exportExcel(outputStream, UserInfoBean.class, list1); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != outputStream) { outputStream.close(); } } catch (IOException e) { e.printStackTrace(); } }

    结果查看

    输出: [ UserInfoBean{userId=‘1’, userName=‘jack’, phoneNum=‘1234567’, gender=‘男’, height=‘165cm’}, UserInfoBean{userId=‘2’, userName=‘lisi’, phoneNum=‘2345678’, gender=‘女’, height=‘175cm’}, UserInfoBean{userId=‘3’, userName=‘zhangsan’, phoneNum=‘3456789’, gender=‘男’, height=‘170cm’} ]

    Processed: 0.027, SQL: 9