jar包除了easypoi还要导入poi的相关jar包,有些依赖easypoi jar包里面没有需要poi提供。
<!-- easypoi -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.4.0</version>
</dependency>
<!-- poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
这里就不贴出测试用例直接放实例,看不懂可以私聊打钱就完事10元保教包会(被折磨了好久不能这样被轻易白嫖)
//进行Excel导出数据处理
List<CDKInfo> CDKInfoList=new ArrayList<>();
ExportParams exportParams = new ExportParams();
exportParams.setSheetName("CDKey");
//生成workbook导出
Workbook workbook =null;
List<JSONObject> finalCdklist = cdklist;
int talpage=(finalCdklist.size()/10000)+1;
workbook=ExcelExportUtil.exportBigExcel(exportParams,CDKInfo.class,new IExcelExportServer() {
/**
* obj 就是下面的talpage,限制条件
* page 是页数,他是在分页进行文件转换,page每次+1
*/
@Override
public List<Object> selectListForExcelExport(Object obj, int page) {
//page每次加一,当等于obj的值时返回空,代码结束;
if (((int) obj) == page) {
return null;
}
int start=-1;
if (page==1){
start=0;
}
else {
start=(page-1)*10000;
}
//不是空时:一直循环运行selectListForExcelExport。每次返回1万条数据。
List<Object> list = new ArrayList<Object>();
for (int i = start; i < (start+10000); i++) {
if(i<finalCdklist.size()) {
CDKInfo cdkInfo = new CDKInfo();
cdkInfo.setCdkey(finalCdklist.get(i).getString("cdkey"));
cdkInfo.setGame_userid(finalCdklist.get(i).getString("game_userid"));
cdkInfo.setUsercount(Integer.parseInt(finalCdklist.get(i).getString("usercount")));
cdkInfo.setGift(gift);
if ("".equals(finalCdklist.get(i).getString("duihuanTime")) || finalCdklist.get(i).getString("duihuanTime") == null) {
cdkInfo.setDuihuanTime("");
} else {
cdkInfo.setDuihuanTime(finalCdklist.get(i).getString("duihuanTime"));
}
MsgClientGroup group = new MsgClientGroup();
group.setGroupName("测试");
cdkInfo.setGroup(group);
log.warn("cdk导出cdkInfo" + JSONObject.toJSONString(cdkInfo));
list.add(cdkInfo);
}
else{
break;
}
}
return list;
}
}, talpage);
Date date = new Date();
String filename="cdk_"+DateFormatUtil.yyyy_MM_dd_HH_mm_ss(date)+".xlsx";
rsp.setContentType("application/ms-excel;charset=UTF-8");
rsp.setHeader("Content-Disposition", "attachment;filename="
.concat(String.valueOf(URLEncoder.encode(filename, "UTF-8"))));
OutputStream out = rsp.getOutputStream();
workbook.write(out);
out.close();
BaseResponse br = new BaseResponse();
br.put("cdklist",cdklist);
return br;
上面代码用到的实体类 CDKInfo
package com.cmge.mj.http.tools.pojo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class CDKInfo implements java.io.Serializable {
public String getCdkey() {
return cdkey;
}
public void setCdkey(String cdkey) {
this.cdkey = cdkey;
}
public String getGame_userid() {
return game_userid;
}
public void setGame_userid(String game_userid) {
this.game_userid = game_userid;
}
public int getUsercount() {
return usercount;
}
public void setUsercount(int usercount) {
this.usercount = usercount;
}
public String getGift() {
return gift;
}
public void setGift(String gift) {
this.gift = gift;
}
public String getDuihuanTime() {
return duihuanTime;
}
public void setDuihuanTime(String duihuanTime) {
this.duihuanTime = duihuanTime;
}
public String getMark() {
return mark;
}
public void setMark(String mark) {
this.mark = mark;
}
public MsgClientGroup getGroup() {
return group;
}
public void setGroup(MsgClientGroup group) {
this.group = group;
}
public CDKInfo() {
}
/**
* name 指定导出excel是生成的列名
* orderNum可指定导出的该属性对应的所在列的位置
* width设置单元格宽度
* type设置导出类型 1是文本 2是图片 3是函数 10数字 默认是文本
*/
@Excel(name="兑换码",orderNum = "1",type = 1,width = 15)
private String cdkey;
@Excel(name="兑换者ID",orderNum="2",width = 15)
private String game_userid;
@Excel(name="兑换次数",orderNum="3",type = 10,width=15)
private int usercount;
@Excel(name="兑换内容",orderNum="4",width=30)
private String gift;
@Excel(name="标记",orderNum="5",width=30)
private String mark;
@Excel(name="兑换时间",orderNum="6",width=20)
private String duihuanTime;
@Excel(name="分组",orderNum="7",width=20)
private MsgClientGroup group;
}
MsgClientGroup
package com.cmge.mj.http.tools.pojo;
import java.io.Serializable;
import cn.afterturn.easypoi.excel.annotation.Excel;
/**
* 客户分组表
*
* @author yjl
*
*/
public class MsgClientGroup implements Serializable {
/**
*
*/
private static final long serialVersionUID = 6946265640897464878L;
// 组名
@Excel(name = "分组")
private String groupName = null;
/**
* 创建人
*/
private String createBy;
public String getCreateBy() {
return createBy;
}
public String getGroupName() {
return groupName;
}
public void setCreateBy(String createBy) {
this.createBy = createBy;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
}