公司注銷網(wǎng)站備案申請表網(wǎng)站推廣和優(yōu)化系統(tǒng)
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 {private int age;//年齡private String name;//姓名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();*///寫入io流//ByteArrayOutputStream outputStream=new ByteArrayOutputStream(1024);// 寫入excelWriter對象//ExcelWriter excelWriter= EasyExcel.write(outputStream).withTemplate(templateInputStream ).build();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()申明別名//填充第一個listList<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);//結(jié)束填充excelWriter.finish();}
}
效果
總結(jié)
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,更改即可。