java使用EasyExcel實(shí)現(xiàn)Sheet的復(fù)制與填充
當(dāng)在后端開發(fā)中處理Excel業(yè)務(wù)時,EasyExcel是一個非常有用的工具,它提供了強(qiáng)大的模板填充功能,可以輕松解決各種業(yè)務(wù)需求。在本文中,我將首先簡要介紹EasyExcel模板填充功能的基本用法,然后提供一種根據(jù)業(yè)務(wù)需要執(zhí)行多份復(fù)制與填充的實(shí)用方案。
EasyExcel模板填充功能的基本用法
使用EasyExcel的[模板填充功能]與[簡單寫]相比,可以diy工作表(sheet)的樣式,讓生成的excel更加美觀且不必花費(fèi)更多的開發(fā)時間。
使用簡單寫完成一個Excel報表
1.創(chuàng)建Java類,方便構(gòu)建數(shù)據(jù)映射到Excel中;在類中使用注解配置單元格格式和標(biāo)題
@ExcelProperty: 設(shè)置excel中標(biāo)題名稱
@ColumnWidth: 設(shè)置單元格的寬度
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CatDTO {
@ExcelProperty(value = "姓名")
private String name;
@ExcelProperty(value = "品種")
@ColumnWidth(15)
private String breed;
@ExcelProperty(value = "年齡")
private Double age;
@ExcelProperty(value = "描述")
@ColumnWidth(30)
private String desc;
}2.使用EasyExcel提供的Write API,將數(shù)據(jù)寫入Excel中。
@SpringBootTest
class DemoApplicationTests {
@Test
void writeTest1() {
String fileName = "write-" + System.currentTimeMillis() + ".xlsx";
// 構(gòu)建數(shù)據(jù)
List<CatDTO> catWriteData = List.of(
CatDTO.builder().name("肥貓").breed("銀漸層").age(1.2).desc("每天吃吃睡睡睡睡睡").build(),
CatDTO.builder().name("虎妞").breed("德文卷毛貓").age(0.6).desc("活潑好動無敵可愛愛粘人").build()
);
EasyExcel.write(fileName, CatDTO.class).sheet().doWrite(catWriteData);
}
}生成效果

使用模板填充完成一個Excel報表
1.創(chuàng)建Java類
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CatFillDTO {
private String name;
private String breed;
private Double age;
private String desc;
}2.構(gòu)建模板,在數(shù)據(jù)填充所在列上,填上與之對應(yīng)的實(shí)體類屬性名;

3.使用EasyExcel提供的Fill API,獲取模板文件,寫入數(shù)據(jù)。
@SpringBootTest
class DemoApplicationTests {
@Test
void fillTest1() {
String fileName = "fill-" + System.currentTimeMillis() + ".xlsx";
// 構(gòu)建數(shù)據(jù)
List<CatFillDTO> catFillData = List.of(
CatFillDTO.builder().name("肥貓").breed("銀漸層").age(1.2).desc("每天吃吃睡睡睡睡睡").build(),
CatFillDTO.builder().name("虎妞").breed("德文卷毛貓").age(0.6).desc("活潑好動無敵可愛愛粘人").build()
);
FileUtil.touch(fileName);
// 加載模板
InputStream resourceAsStream = ResourceUtil.getStreamSafe("temp/cat-template.xlsx");
// 生成工作簿對象
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(resourceAsStream).build();
// 生成工作表對象
WriteSheet writeSheet = EasyExcel.writerSheet().build();
// 多組數(shù)據(jù)填充需要另起一行時 forceNewRow(true)
FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();
// 多組數(shù)據(jù)填充 List<實(shí)體類> 對象
excelWriter.fill(catFillData, fillConfig, writeSheet);
// 關(guān)閉excelWriter
excelWriter.finish();
}
}4.生成效果

可以看出,使用模板填充可以省去在代碼中配置字段名稱、單元格大小等配置,在實(shí)現(xiàn)復(fù)雜的Excel樣式或美觀修飾上更有優(yōu)勢;
根據(jù)業(yè)務(wù)需要執(zhí)行多份復(fù)制與填充的方案
在處理Excel時,你或許會遇到這樣的問題。使用指定模板填充數(shù)據(jù),數(shù)據(jù)根據(jù)某個特征進(jìn)行劃分,分別填進(jìn)不同的工作表(sheet)中,這樣,我們需要根據(jù)業(yè)務(wù)的需求, 來生成相應(yīng)的模板表數(shù)量,再分別填充上數(shù)據(jù)。
實(shí)現(xiàn)思路
- 使用POI API加載模板文件為Workbook對象
- 使用POI中的cloneSheet API復(fù)制相應(yīng)數(shù)量的模板工作表
- 將復(fù)制好數(shù)量的工作簿文件轉(zhuǎn)換成輸入流,以便在EasyExcel withTemplate API中使用
- 使用EasyExcel fill API填充數(shù)據(jù)。
(直接上業(yè)務(wù)代碼,實(shí)現(xiàn)的功能為模板表1不執(zhí)行復(fù)制,只進(jìn)行填充,模板表2進(jìn)行復(fù)制并填充)
@Service
@Slf4j
@RequiredArgsConstructor
public class SpeakerServiceImpl implements ISpeakerService {
/**
* 填充模板文件
*
* @param sheet1Data sheet1數(shù)據(jù)
* @param promptMap 提示地圖
* @return {@code File}
*/
public File fillTemplateFile(
CustomSpeakerDemandExcelDTO sheet1Data, Map<String, List<CustomSpeakerPromptDTO>> promptMap) {
try {
// 構(gòu)建模板
byte[] asInputStream = this.buildTemplateFile(promptMap);
// 創(chuàng)建臨時導(dǎo)出文件
File temporaryFile = Files.createTempFile("speaker_task_", ".xlsx").toFile();
// easyExcel API 根據(jù)is作為模板填充數(shù)據(jù),并寫入臨時文件temporaryFile
ExcelWriter excelWriter = EasyExcel.write(temporaryFile)
.withTemplate(new ByteArrayInputStream(asInputStream))
.build();
// 填充模板1的sheet
WriteSheet writeSheet1 = EasyExcel.writerSheet(0).build();
excelWriter.fill(sheet1Data, writeSheet1);
// 填充模板2的多個sheet
int sheetIndex = 1;
for (String key : promptMap.keySet()) {
WriteSheet writeSheet = EasyExcel.writerSheet(sheetIndex).build();
// 寫入數(shù)據(jù)到當(dāng)前sheet
List<CustomSpeakerPromptDTO> prompt = promptMap.get(key);
excelWriter.fill(prompt, writeSheet);
sheetIndex++;
}
excelWriter.finish();
return temporaryFile;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 構(gòu)建模板文件
*
* @param promptMap 提示地圖
* @return {@code byte[]}
* @throws IOException ioexception
*/
public byte[] buildTemplateFile(Map<String, List<CustomSpeakerPromptDTO>> promptMap) throws IOException {
// 加載模板文件
InputStream templateStream = ResourceUtil.getStreamSafe("temp/speaker-task-template.xlsx");
Workbook workbook = WorkbookFactory.create(templateStream);
// 設(shè)置模板sheet數(shù)量, 需要復(fù)制promptMap.size()-1 次工作表2
boolean first = true;
for (String key : promptMap.keySet()) {
if (first) {
// 第一組數(shù)據(jù)可以直接使用當(dāng)前模板,不需要進(jìn)行復(fù)制
workbook.setSheetName(1, key);
} else {
// 復(fù)制模板到新工作表,并設(shè)置表名
Sheet newSheet = workbook.cloneSheet(1);
workbook.setSheetName(workbook.getSheetIndex(newSheet), key);
}
first = false;
}
ByteArrayOutputStream ops = new ByteArrayOutputStream();
workbook.write(ops);
byte[] byteArray = ops.toByteArray();
// 原文件流后續(xù)已不使用,此處關(guān)閉
templateStream.close();
ops.close();
return byteArray;
}
}以上就是java使用EasyExcel實(shí)現(xiàn)Sheet的復(fù)制與填充的詳細(xì)內(nèi)容,更多關(guān)于java EasyExcel的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring AOP訪問目標(biāo)方法的參數(shù)操作示例
這篇文章主要介紹了Spring AOP訪問目標(biāo)方法的參數(shù)操作,結(jié)合實(shí)例形式詳細(xì)分析了spring面向切面AOP訪問目標(biāo)方法的參數(shù)相關(guān)實(shí)現(xiàn)步驟與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01
淺談idea live template高級知識_進(jìn)階(給方法,類,js方法添加注釋)
下面小編就為大家?guī)硪黄獪\談idea live template高級知識_進(jìn)階(給方法,類,js方法添加注釋)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
java注釋轉(zhuǎn)json插件開發(fā)實(shí)戰(zhàn)詳解
這篇文章主要為大家介紹了java注釋轉(zhuǎn)json插件開發(fā)實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
如何在Java中創(chuàng)建線程通信的四種方式你知道嗎
開發(fā)中不免會遇到需要所有子線程執(zhí)行完畢通知主線程處理某些邏輯的場景?;蛘呤蔷€程 A 在執(zhí)行到某個條件通知線程 B 執(zhí)行某個操作。下面我們來一起學(xué)習(xí)如何解決吧2021-09-09

