分布式醫(yī)療掛號系統(tǒng)EasyExcel導(dǎo)入導(dǎo)出數(shù)據(jù)字典的使用
一、導(dǎo)出數(shù)據(jù)字典到Excel
1.創(chuàng)建導(dǎo)出實體類
這里導(dǎo)出數(shù)據(jù)時,只導(dǎo)出網(wǎng)頁上每條記錄的id、父id、名稱、編碼、值。
@Data public class DictEeVo { @ExcelProperty(value = "id", index = 0) private Long id; @ExcelProperty(value = "上級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.后臺接口代碼
Controller層
為了實現(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實現(xiàn)類:
實現(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è)置了超鏈接屬性,單擊后自動調(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.測試數(shù)據(jù)導(dǎo)出到Excel
在頁面單擊 數(shù)據(jù)導(dǎo)出 按鈕后,跳出下載框,成功將頁面數(shù)據(jù)下載到本地.xlsx
文件中。
二、導(dǎo)入數(shù)據(jù)字典到網(wǎng)頁
1.后臺接口代碼
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實現(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對象復(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.測試數(shù)據(jù)導(dǎo)入到網(wǎng)頁
在Excel中準(zhǔn)備兩條測試數(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)完成,更多關(guān)于分布式醫(yī)療掛號系統(tǒng)的資料請關(guān)注腳本之家其它相關(guān)文章!
- 分布式醫(yī)療掛號系統(tǒng)整合Gateway網(wǎng)關(guān)解決跨域問題
- 分布式醫(yī)療掛號系統(tǒng)Nacos微服務(wù)Feign遠(yuǎn)程調(diào)用數(shù)據(jù)字典
- 實戰(zhàn)分布式醫(yī)療掛號系統(tǒng)開發(fā)醫(yī)院科室及排班的接口
- 分布式醫(yī)療掛號系統(tǒng)SpringCache與Redis為數(shù)據(jù)字典添加緩存
- 分布式開發(fā)醫(yī)療掛號系統(tǒng)數(shù)據(jù)字典模塊前后端實現(xiàn)
- 實戰(zhàn)分布式醫(yī)療掛號系統(tǒng)登錄接口整合阿里云短信詳情
相關(guān)文章
Java嵌入式開發(fā)的優(yōu)勢及有點總結(jié)
在本篇內(nèi)容里小編給大家整理了關(guān)于Java嵌入式開發(fā)的優(yōu)勢及相關(guān)知識點內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2022-11-11Mybatis/Mybatis-Plus駝峰式命名映射的實現(xiàn)
本文主要介紹了Mybatis-Plus駝峰式命名映射的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07關(guān)于動態(tài)參數(shù)使用@PathVariable的解析
這篇文章主要介紹了關(guān)于動態(tài)參數(shù)使用@PathVariable的解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02SpringBoot整合Mybatis與thymleft實現(xiàn)增刪改查功能詳解
MybatisPlus是國產(chǎn)的第三方插件,?它封裝了許多常用的CURDapi,免去了我們寫mapper.xml的重復(fù)勞動。本文將整合MybatisPlus實現(xiàn)增刪改查功能,感興趣的可以了解一下2022-12-12關(guān)于mybatis-plus插件使用時的一些問題小結(jié)
這篇文章主要給大家介紹了關(guān)于mybatis-plus插件使用時的一些問題的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-03-03