一蒜茴、導(dǎo)出數(shù)據(jù)字典到Excel
1.創(chuàng)建導(dǎo)出實(shí)體類
- 這里導(dǎo)出數(shù)據(jù)時(shí),只導(dǎo)出網(wǎng)頁上每條記錄的id疆栏、父id曾掂、名稱、編碼壁顶、值珠洗。
@Data
public class DictEeVo {
@ExcelProperty(value = "id", index = 0)
private Long id;
@ExcelProperty(value = "上級(jí)id", index = 1)
private Long parentId;
@ExcelProperty(value = "名稱", index = 2)
private String name;
@ExcelProperty(value = "值", index = 3)
private String value;
@ExcelProperty(value = "編碼", index = 4)
private String dictCode;
}
2.后臺(tái)接口代碼
Controller層
為了實(shí)現(xiàn)下載數(shù)據(jù),Controller層傳入
HttpServletResponse
參數(shù)若专。
@ApiOperation(value = "導(dǎo)出數(shù)據(jù)字典接口")
@GetMapping("exportData")
public void exportDictData(HttpServletResponse response) throws IOException {
dictService.exportDictData(response);
}
Service層
Service接口:
void exportDictData(HttpServletResponse response) throws IOException;
Service實(shí)現(xiàn)類:
實(shí)現(xiàn)類中许蓖,首先設(shè)置響應(yīng)類型、響應(yīng)頭调衰、編碼等信息膊爪。然后通過Dao層方法查詢數(shù)據(jù)庫,先將查詢到的數(shù)據(jù)放在dictList集合中嚎莉,再通過
BeanUtils.copyProperties
方法將數(shù)據(jù)放入DictVo中米酬,最后加入dictVoList集合中,傳入write方法的參數(shù)中萝喘。
/**
* 導(dǎo)出數(shù)據(jù)字典接口
* @param response
*/
@Override
public void exportDictData(HttpServletResponse response) throws IOException {
// 設(shè)置下載信息
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("數(shù)據(jù)字典", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=" + fileName + ".xlsx");
// 查詢數(shù)據(jù)庫
List<Dict> dictList = baseMapper.selectList(null);
// 將Dict轉(zhuǎn)換為DictVo
List<DictVo> dictVoList = new ArrayList<>();
for (Dict dict : dictList) {
DictVo dictVo = new DictVo();
// 將dict中的值復(fù)制到dictVo中
BeanUtils.copyProperties(dict, dictVo);
dictVoList.add(dictVo);
}
// 調(diào)用writer方法進(jìn)行寫操作
EasyExcel.write(response.getOutputStream(), DictVo.class).sheet("數(shù)據(jù)字典")
.doWrite(dictVoList);
}
3.頁面導(dǎo)出按鈕
頁面導(dǎo)出按鈕設(shè)置了超鏈接屬性淮逻,單擊后自動(dòng)調(diào)用后端下載接口。
<a href="http://localhost:8202/admin/cmn/dict/exportData" target="_blank">
<el-button type="text">
數(shù)據(jù)導(dǎo)出
</el-button>
</a>
4.測(cè)試數(shù)據(jù)導(dǎo)出到Excel
在頁面單擊 數(shù)據(jù)導(dǎo)出 按鈕后阁簸,跳出下載框爬早,成功將頁面數(shù)據(jù)下載到本地.xlsx
文件中。
二启妹、導(dǎo)入數(shù)據(jù)字典到網(wǎng)頁
1.后臺(tái)接口代碼
Controller層
Controller層通過MultipartFile得到上傳的文件筛严。
@ApiOperation(value = "導(dǎo)入數(shù)據(jù)字典到網(wǎng)頁")
@PostMapping("importData")
public Result importDictData(MultipartFile file){
dictService.importDictData(file);
return Result.ok();
}
Service層
Service接口
void importDictData(MultipartFile file);
Service實(shí)現(xiàn)類
Service中直接使用EasyExcel讀取文件中的內(nèi)容,并加載到數(shù)據(jù)庫
@Override
public void importDictData(MultipartFile file) {
try {
EasyExcel.read(file.getInputStream(), DictVo.class, new DictListener(baseMapper)).sheet().doRead();
} catch (IOException e) {
e.printStackTrace();
}
}
配置監(jiān)聽器
監(jiān)聽器中饶米,讀取Excel內(nèi)容到DictVo中桨啃,再將DictVO復(fù)制到Dict中。最后調(diào)用Dao層的方法將DIct添加到數(shù)據(jù)庫檬输。
public class DictListener extends AnalysisEventListener<DictVo> {
// 調(diào)用Dao
private DictMapper dictMapper;
public DictListener(DictMapper dictMapper) {
this.dictMapper = dictMapper;
}
// 讀取Excel內(nèi)容
@Override
public void invoke(DictVo DictVo, AnalysisContext context) {
// 將DictVO對(duì)象復(fù)制到Dict中
Dict dict = new Dict();
BeanUtils.copyProperties(DictVo, dict);
// 將數(shù)據(jù)添加到數(shù)據(jù)庫
dictMapper.insert(dict);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}
}
2.頁面導(dǎo)入按鈕
3.測(cè)試數(shù)據(jù)導(dǎo)入到網(wǎng)頁
在Excel中準(zhǔn)備兩條測(cè)試數(shù)據(jù):
將Excel通過頁面的 數(shù)據(jù)導(dǎo)入 按鈕上傳到數(shù)據(jù)庫:
成功將Excel中的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫照瘾,進(jìn)而通過網(wǎng)頁展現(xiàn):
至此,使用EasyExcel從網(wǎng)頁導(dǎo)入導(dǎo)出數(shù)據(jù)的演示已經(jīng)完成丧慈。