一.引入EasyPoi依賴
<!-- 導(dǎo)入導(dǎo)出的工具包,可以完成Excel導(dǎo)出,導(dǎo)入,Word的導(dǎo)出,Excel的導(dǎo)出功能 -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<!--耦合了spring-mvc 基于AbstractView,極大的簡化spring-mvc下的導(dǎo)出功能 -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<!-- 基礎(chǔ)注解包,作用與實(shí)體對(duì)象上,拆分后方便maven多工程的依賴管理 -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
二.導(dǎo)出示例(合并單元格)
1.導(dǎo)出模板
文章來源地址http://www.zghlxwxcb.cn/news/detail-622208.html
2.實(shí)體類加注釋
@Excel(name = "真愛粉", width = 15)
private String heizi;
@Excel(name = "唱", width = 15)
private String chang;
@Excel(name = "跳", width = 15)
private String tiao;
@Excel(name = "rap", width = 15)
private String rap;
@Excel(name = "籃球", width = 15)
private String qiu;
3.編寫邏輯
public void exportXls(KunKun ikun, HttpServletResponse response) {
//查詢導(dǎo)出的信息列表
List<KunKun> ikuns = kunKunMapper.allList(ikun);
//存放excel的表頭 ExcelExportEntity是EasyPoi提供的excel屬性實(shí)體類
List<ExcelExportEntity> entityList = new ArrayList<ExcelExportEntity>();
//表頭
ExcelExportEntity title = new ExcelExportEntity("序號(hào)","num");
entityList.add(title);
//真愛粉對(duì)應(yīng)key,heizi對(duì)應(yīng)的是表頭也是你實(shí)體類的字段
title = new ExcelExportEntity("真愛粉","heizi");
entityList.add(title);
//合并坤坤單元格
ExcelExportEntity groupOne = new ExcelExportEntity("坤坤", "kunkun");
List<ExcelExportEntity> exportEntitieOne = new ArrayList<>();
exportEntitieOne.add(new ExcelExportEntity("唱","chang"));
exportEntitieOne.add(new ExcelExportEntity("跳","tiao"));
exportEntitieOne.add(new ExcelExportEntity("rap","rap"));
exportEntitieOne.add(new ExcelExportEntity("籃球","qiu"));
groupOne.setList(exportEntitieOne);
entityList.add(groupOne);
//存放全部的數(shù)據(jù)
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//定義序號(hào)
int num = 1;
for (KunKun ikun: ikuns) {
//存放坤坤表頭下的數(shù)據(jù)
List<Map<String, Object>> listOne = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<>();
Map<String, Object> mapOne = new HashMap<>();
map.put("num",num++);
map.put("heizi",ikun.getHeizi());
mapOne.put("chang",ikun.getChang());
mapOne.put("tiao",ikun.getTiao());
mapOne.put("rap",ikun.getRap());
mapOne.put("qiu",ikun.getQiu());
listOne.add(mapOne);
map.put("kunkun",listOne);
list.add(map);
}
//獲取當(dāng)前時(shí)間
String date = LocalDate.now().toString();
String fileName = date + "坤坤記錄導(dǎo)出";
//ExcelExportUtil是EasyPoi提供的導(dǎo)出工具類。
//參數(shù)對(duì)應(yīng):導(dǎo)出文件名字、表頭、對(duì)應(yīng)的數(shù)據(jù)
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(fileName, "sheet1"), entityList, list);
try {
workbook.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
workbook.close();
response.getOutputStream().close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
二.導(dǎo)入示例
public Result<?> importExcel(MultipartFile file) {
String extNane = FileUtil.getSuffix(file.getOriginalFilename());
if (!"xls,xlsx".contains(extNane)){
return Result.error("請(qǐng)選擇Excel文件!");
}
ExcelReader reader = null;
try {
reader = ExcelUtil.getReader(file.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
//從第2行開始讀取數(shù)據(jù)(0開始計(jì)數(shù))
List<List<Object>> readAll = reader.read(2);
if (CollectionUtils.isEmpty(readAll)){
return Result.error("數(shù)據(jù)為空,請(qǐng)正確填寫數(shù)據(jù)后再上傳!");
}
//存放導(dǎo)入的數(shù)據(jù)
List<KunKun> insertList = new ArrayList<>();
try{
readAll.forEach(e ->{
KunKun ikun = new KunKun();
//從1開始取,因?yàn)?列是序號(hào)列
//真愛粉
fuelSupplyStore.setHeizi(ObjectUtil.isNull(e.get(1)) ? null : ObjectUtil.toString(e.get(1)).trim());
//唱
fuelSupplyStore.setChang(ObjectUtil.isNull(e.get(2)) ? null : ObjectUtil.toString(e.get(2)).trim());
//跳
fuelSupplyStore.setTiao(ObjectUtil.isNull(e.get(3)) ? null : ObjectUtil.toString(e.get(3)).trim());
//rap
fuelSupplyStore.setRap(ObjectUtil.isNull(e.get(4)) ? null : ObjectUtil.toString(e.get(4)).trim());
//籃球
fuelSupplyStore.setQiu(ObjectUtil.isNull(e.get(5)) ? null : ObjectUtil.toString(e.get(5)).trim());
}catch (Exception ex){
return Result.error("導(dǎo)入失敗");
}
//批量新增
kunKunMapper.saveBatch(insertList);
return Result.OK("導(dǎo)入成功");
}
文章來源:http://www.zghlxwxcb.cn/news/detail-622208.html
到了這里,關(guān)于【java】EasyPoi導(dǎo)出導(dǎo)入(合并單元格)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!