导入依赖
EasyExcel
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
EasyExcel 工具类
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
@Api(value = "Excel工具类")
public class ExcelUtils {
private static final Logger log = LoggerFactory.getLogger(ExcelUtils.class);
private final static String fileName = "D:\\ExportExcel"+"\\" + System.currentTimeMillis() + ".xlsx";
@Test
public void testExportListMap() {
String mainHead="惠农通数据表";
List<Map<String, Object>> exportList = new ArrayList<>();
Map<String,Object> map = new LinkedHashMap<>();
map.put("银行名称", "张XX");
map.put("期间新增项目", 2);
map.put("期间新增担保总额", "刘德华");
map.put("期间新增解保项目数", 4);
map.put("期间新增解保金额", "周星驰");
map.put("在保项目数", 6);
map.put("在保余额", "周润发");
map.put("银行利率", 8);
exportList.add(map);
Boolean aBoolean = exportExcel(mainHead, exportList, fileName);
if(aBoolean){
System.out.println("生成Excel成功!");
}
}
@Test
public void testExportListEntity(){
List<DemoData> dataList = new ArrayList<>();
for(int i = 0; i < 100; i++){
DemoData demoData = new DemoData();
demoData.setDate(new Date());
demoData.setString("周星星"+i);
demoData.setDoubleData(9.89);
demoData.setIgnore("不导出字段");
dataList.add(demoData);
}
Boolean aBoolean = exportExcel(dataList, DemoData.class, fileName);
if(aBoolean){
System.out.println("Excel生成成功!");
}
}
public static Boolean exportExcel(List<?> list, Class<?> tClass, String pathFileName){
try{
EasyExcel.write(pathFileName,tClass).sheet("模板").doWrite(list);
}catch (Exception e){
log.info("导出Excel发生异常:{}",e);
return false;
}
return true;
}
public static Boolean exportExcel(String mainHead, List<Map<String, Object>> list, String pathFileName){
List<String> headKeys = new ArrayList<>();
headKeys.addAll(list.get(0).keySet());
try{
exportListMap(mainHead,headKeys,list,pathFileName);
}catch (Exception e){
log.info("导出Excel发生异常:{}",e);
return false;
}
return true;
}
public static void exportListMap(String mainHead,List<String> headKeys, List<Map<String, Object>> list,String pathFileName){
List<List<String>> hs = new ArrayList<>();
for (String key : headKeys) {
hs.add(Arrays.asList(mainHead,key));
}
List<List<Object>> list2 = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
List<Object> objects = new ArrayList<>();
Collection<Object> values = list.get(i).values();
for (Object value : values) {
if(value != null){
objects.add(value.toString());
}else {
objects.add(null);
}
}
list2.add(objects);
}
EasyExcel.write(pathFileName).head(hs).sheet("模板").doWrite(list2);
}
}
实体类
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.format.NumberFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
@Data
public class DemoData {
@ExcelProperty({"主标题","字符串标题"})
private String string;
@JsonFormat(timezone="GMT+8")
@DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒")
@ExcelProperty({"主标题","日期标题"})
private Date date;
@NumberFormat("#.##")
@ExcelProperty({"主标题","数字标题"})
private Double doubleData;
@ApiModelProperty("忽略这个字段")
@ExcelIgnore
private String ignore;
}