Java利用EasyExcel實現(xiàn)模板讀取和復雜表格填充
EasyExcel
?Excel表格中用{}或者{.} 來表示包裹要填充的變量,如果單元格文本中本來就有{、}左右大括號,需要在括號前面使用斜杠轉(zhuǎn)義\{ 、\}。
?代碼中被填充數(shù)據(jù)的實體對象的成員變量名或被填充map集合的key需要和Excel中被{}包裹的變量名稱一致。
ExcelWriter的fill()方法 用于填充數(shù)據(jù)
FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();//表示開啟組合填充換行填充
樣例

測試代碼
TeacherInfo 對象
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder //通過自動生成的構建器模式創(chuàng)建對象
public class TeacherInfo {
@ExcelProperty("年齡")
private int age;//年齡
@ExcelProperty("姓名")
private String name;//姓名
@ExcelProperty("科目")
private String subject;//科目
}
StudentInfo對象
package com.fasterres.demo.blog;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class StudentInfo {
private String id;//編號
private String studentname;//姓名
private String sex;//性別
@DateTimeFormat("yyyy/MM/dd")
private Date birthday;//生日
}
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.fasterres.demo.blog.StudentInfo;
import com.fasterres.demo.blog.TeacherInfo;
import org.springframework.core.io.ClassPathResource;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
public class ExcelWriteTest {
public static void main(String[] args) throws Exception {
// 1.寫入文件方式
String filePath = "D:\\log\\stduent_01.xlsx";
InputStream templateFile = new FileInputStream(filePath);
String targetFile = "D:\\學生信息統(tǒng)計2.xlsx";
ExcelWriter excelWriter =EasyExcel.write(targetFile).withTemplate(templateFile).build();
// 2.加載模板,配置在項目工程下的 寫入io
/* ClassPathResource resource=new ClassPathResource("template/stduent_01.xlsx");
InputStream templateInputStream = resource.getInputStream();*/
//1.寫入io流
//ByteArrayOutputStream outputStream=new ByteArrayOutputStream(1024);
// 寫入excelWriter對象
//ExcelWriter excelWriter= EasyExcel.write(outputStream).withTemplate(templateInputStream ).build();
//2.最簡單的寫入是讀取數(shù)據(jù)寫入單行正常表頭
//List<TeacherInfo> teacherInfos=new ArrayList<>();
//EasyExcel.write(outputStream,TeacherInfo.class).sheet("老師信息統(tǒng)計").doWrite(teacherInfos);
WriteSheet sheet = EasyExcel.writerSheet().build();
// 當前日期
HashMap<String, String> dateMap = new HashMap<String, String>();
dateMap.put("date", "2025-02-06");
//填充單個字段信息
excelWriter.fill(dateMap,sheet);
//填充單個對象信息
/*
TeacherInfo teacher=TeacherInfo.builder()
.age(33)
.name("張竹")
.build();
excelWriter.fill(teacher,sheet);
*/
//換行填充
FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();
//多次填充list,可以用new FillWrapper()申明別名
//填充第一個list
List<TeacherInfo> teacherInfoList=new ArrayList();
TeacherInfo teacher2=TeacherInfo.builder()
.age(22)
.name("張竹")
.subject("數(shù)學")
.build();
TeacherInfo teacher3=TeacherInfo.builder()
.age(33)
.name("power")
.subject("英語")
.build();
teacherInfoList.add(teacher2);
teacherInfoList.add(teacher3);
excelWriter.fill(new FillWrapper("t1",teacherInfoList),fillConfig ,sheet);
//填充第二個list對象
List<StudentInfo> studentInfo = new ArrayList<>();
StudentInfo Student1=new StudentInfo("1", "張三", "男", DateUtil.parse("2022/12/12"));
StudentInfo Student2=new StudentInfo("2", "王芳", "女", DateUtil.parse("2025/02/15"));
studentInfo.add(Student1);
studentInfo.add(Student2);
excelWriter.fill(new FillWrapper("t2",studentInfo),fillConfig ,sheet);
//結束填充
excelWriter.finish();
}
}
效果

總結
1.占位符
在根據(jù)模版導出數(shù)據(jù)時,要預先設置占位符。包括,單個數(shù)據(jù)占位符和列表數(shù)據(jù)占位符。
單個占位符:{字段名} 如:{name}
列表占位符:{.字段名} 如:{.age},如果一個表格中有多個數(shù)據(jù)列,占位符前要加前綴,如:{t1.id}、{t2.name}
2.如果需要配置多個list,可以用new FillWrapper 申請別名:
excelWriter.fill(new FillWrapper(“t1”,teacherInfoList),fillConfig ,sheet);
3.碰到輸入的文件名和文件內(nèi)容亂碼:
引起原因是項目的編碼或者文件的編碼不是UTF-8,更改即可。
到此這篇關于Java利用EasyExcel實現(xiàn)模板讀取和復雜表格填充的文章就介紹到這了,更多相關Java EasyExcel復雜表格填充內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java中在時間戳計算的過程中遇到的數(shù)據(jù)溢出問題解決
這篇文章主要介紹了Java中在時間戳計算的過程中遇到的數(shù)據(jù)溢出問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-06-06
MyBatis的CRUD中的不同參數(shù)綁定查詢實現(xiàn)
本文主要介紹了MyBatis的CRUD中的不同參數(shù)綁定查詢實現(xiàn),主要包括單個參數(shù)傳遞綁定,序號參數(shù)傳遞綁定,注解參數(shù)傳遞綁定,pojo(對象)參數(shù)傳遞綁定,map參數(shù)傳遞綁定這幾種類型,具有一定的參考價值,感興趣的可以了解一下2023-12-12
源碼分析Java中ThreadPoolExecutor的底層原理
這篇文章主要帶大家從源碼分析一下Java中ThreadPoolExecutor的底層原理,文中的示例代碼講解詳細,具有一定的學習價值,需要的可以參考一下2023-05-05
java數(shù)據(jù)結構之二分查找法 binarySearch的實例
這篇文章主要介紹了java數(shù)據(jù)結構之二分查找法 binarySearch的實例的相關資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10
springboot掃描自定義的servlet和filter代碼詳解
本文是一篇根據(jù)作者工作經(jīng)歷總結出來的關于springboot掃描自定義的servlet和filter代碼詳解的文章,小編覺得非常不錯,這里給大家分享下,和朋友們一起學習,進步。2017-10-10
SpringBoot Arthas實現(xiàn)線上監(jiān)控診斷
本文主要介紹了SpringBoot Arthas實現(xiàn)線上監(jiān)控診斷,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2026-03-03

