概述
一、首先如何使用easyExcel参考
1、easyExcel2.2.9的工具类封装
https://blog.csdn.net/fen_fen/article/details/120212492
2、easyExcel如何使用实体映射关系直接读写Excel数据https://blog.csdn.net/fen_fen/article/details/120190845
二、easyexcel实现针对同1个对象,写入不同的sheet
1、ExcelUtil工具类:
/**
* 写到不同的sheet
* @param excelWriter excelWriter
* @param datalist 写入的数据
* @param sheetName sheet名称
*/
public static void excelWriter(ExcelWriter excelWriter,List<SDKData> datalist,String sheetName) {
//指定不同的sheet
WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).head(SDKData.class).build();
//写入excel
excelWriter.write(datalist, writeSheet);
}
/**
* finish关闭写入流
* @param excelWriter excelWriter
*/
public static void closeWriter(ExcelWriter excelWriter){
if(excelWriter !=null){
excelWriter.finish();
}
}
2、调用Demo例子1
public class ExcelTest {
public static void main(String args[]){
//写excel数据
String dateTime=getTime2();
String filename="用户信息表.xlsx";
String sheetName1="PlainData";
List<SDKData> datalist1 = new ArrayList<>();
for(int i = 0; i <= 10; i++) {
SDKData sdkdata = new SDKData();
sdkdata.setId(i+1);
sdkdata.setName("张三" + i);
sdkdata.setAge(25+i);
sdkdata.setSalary(2000.00D*i);
sdkdata.setBirthday(new Date());
datalist1.add(sdkdata);
}
ExcelWriter excelWriter = EasyExcel.write(filename).build();
ExcelUtil.excelWriter(excelWriter,datalist1,sheetName1);
String sheetName2="PlainData_2";
List<SDKData> datalist2 = new ArrayList<>();
for(int i = 0; i <= 10; i++) {
SDKData sdkdata = new SDKData();
sdkdata.setId(i+1);
sdkdata.setName("赵武" + i);
sdkdata.setAge(25+i);
sdkdata.setSalary(2000.00D*i);
sdkdata.setBirthday(new Date());
datalist2.add(sdkdata);
}
ExcelUtil.excelWriter(excelWriter,datalist2,sheetName2);
ExcelUtil.closeWriter(excelWriter);
}
写入结果:
3、调用Demo例子3:
实体类:ExcelDataBean
package com.ciphergateway.bean;
import lombok.Data;
import java.util.List;
@Data
public class ExcelDataBean {
private String sheetName;
private List<ApiDataBean> datalist;
}
服务类和用例类:
//BaseRequest.java
//初始化resultList
protected static List<ExcelDataBean> resultList=new ArrayList<>();
//用例1
public class RoleTest extends BaseRequest{
@AfterTest
public void teardown() throws Exception {
//当前用例的数据存储到list
ExcelDataBean excelData=new ExcelDataBean();
excelData.setSheetName("角色管理");
excelData.setDatalist(dataList_w);
resultList.add(excelData);
}
}
//用例2:
public class UserTest extends BaseRequest{
@AfterTest
public void teardown() throws Exception {
//当前用例的数据存储到list
ExcelDataBean excelData=new ExcelDataBean();
excelData.setSheetName("用户管理");
excelData.setDatalist(dataList_w);
resultList.add(excelData);
//最后一起写入Excel中
String filename="src/data/testcase/apiData_result.xlsx";
ExcelWriter excelWriter = EasyExcel.write(filename).build();
for(ExcelDataBean excelData2:resultList){
String sheet=excelData2.getSheetName();
List<ApiDataBean> dataList=excelData2.getDatalist();
ExcelUtil.excelWriter(excelWriter,dataList,sheet);
}
ExcelUtil.closeWriter(excelWriter);
}
}
写入结果:
参考:使用easyExcel将同一个对象、不同对象写入不同sheet_gkkljy的博客-CSDN博客
EasyExcel写文件的三种方式_向秋而晚的博客-CSDN博客_easyexcel write
EasyExcel实现写操作实践 - 简书 (jianshu.com)
最后
以上就是迷你画板为你收集整理的Java(69):easyexcel实现同1个对象,写入不同的sheet的全部内容,希望文章能够帮你解决Java(69):easyexcel实现同1个对象,写入不同的sheet所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复