EasyExcel 2 上传 下载
生活随笔
收集整理的這篇文章主要介紹了
EasyExcel 2 上传 下载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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;}@Overridepublic 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’}
]
總結
以上是生活随笔為你收集整理的EasyExcel 2 上传 下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring boot集成webserv
- 下一篇: spring boot + zookee