栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot集成Easyexcel-写入

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot集成Easyexcel-写入

1.创建一个SpringBoot项目(SpringBoot生成)

2.导入依赖(在pom中导入)
 	
    
        org.apache.poi
        poi
        3.17
    
    
        org.apache.poi
        poi-ooxml
        3.17
    
    
    
    
        com.alibaba
        easyexcel
        2.1.1
    
3.创建一个实体类(User)
package com.huyi.easyexcel.pojo;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;



public class User {
	
	
	@ExcelProperty(value ="用户ID",index = 0)
	private Long userId;
	@ExcelProperty(value ="用户名",index = 1)
	private String userName;
	@ExcelProperty(value ="用户密码",index = 2)
	private String passWord;
	@ExcelProperty(value ="用户邮箱",index = 3)
	private String email;
	
	
	public Long getUserId() {
		return userId;
	}
	public void setUserId(Long userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassWord() {
		return passWord;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public User(Long userId, String userName, String passWord, String email) {
		this.userId = userId;
		this.userName = userName;
		this.passWord = passWord;
		this.email = email;
	}
	public User() {
		
	}
	
}
4.在项目的test中测试
package com.huyi.easyexcel;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import com.alibaba.excel.EasyExcel;
import com.huyi.easyexcel.pojo.User;

@SpringBootTest
class EasyexcelApplicationTests {

	@Test
	void contextLoads() {
			// 文件的下载路径(或写入路径)
	        String fileName = "E:\test.xlsx";
	        //模拟写入数据
	        List userList = new ArrayList();
	        //循环写入数据
	        for(int i=0;i<18;i++) {
	        	Long userId = (long)i+1;
	        	User user = new User();
	        	user.setUserId(userId);
	        	user.setUserName("name"+userId);
	        	user.setPassWord("password"+userId);
	        	user.setEmail("xxx@163.com");
	        	userList.add(user);
	        }
	        for(User user:userList) {
	        	System.err.println(user.getUserName());
	        }
	        	
	        	
	        
	        EasyExcel.write(fileName,User.class).sheet("用户表").doWrite(userList);
	}

}

  • 注意:文件如果打开的话运行会报错
5.运行结果如下

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/987700.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号