分布式醫(yī)療掛號系統(tǒng)EasyExcel導入導出數(shù)據(jù)字典的使用
一、導出數(shù)據(jù)字典到Excel
1.創(chuàng)建導出實體類
這里導出數(shù)據(jù)時,只導出網(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 = "導出數(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)類中,首先設置響應類型、響應頭、編碼等信息。然后通過Dao層方法查詢數(shù)據(jù)庫,先將查詢到的數(shù)據(jù)放在dictList集合中,再通過BeanUtils.copyProperties
方法將數(shù)據(jù)放入DictVo中,最后加入dictVoList集合中,傳入write方法的參數(shù)中。
/** * 導出數(shù)據(jù)字典接口 * @param response */ @Override public void exportDictData(HttpServletResponse response) throws IOException { // 設置下載信息 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轉換為DictVo List<DictVo> dictVoList = new ArrayList<>(); for (Dict dict : dictList) { DictVo dictVo = new DictVo(); // 將dict中的值復制到dictVo中 BeanUtils.copyProperties(dict, dictVo); dictVoList.add(dictVo); } // 調用writer方法進行寫操作 EasyExcel.write(response.getOutputStream(), DictVo.class).sheet("數(shù)據(jù)字典") .doWrite(dictVoList); }
3.頁面導出按鈕
頁面導出按鈕設置了超鏈接屬性,單擊后自動調用后端下載接口。
<a href="http://localhost:8202/admin/cmn/dict/exportData" target="_blank"> <el-button type="text"> 數(shù)據(jù)導出 </el-button> </a>
4.測試數(shù)據(jù)導出到Excel
在頁面單擊 數(shù)據(jù)導出 按鈕后,跳出下載框,成功將頁面數(shù)據(jù)下載到本地.xlsx
文件中。
二、導入數(shù)據(jù)字典到網(wǎng)頁
1.后臺接口代碼
Controller層
Controller層通過MultipartFile得到上傳的文件。
@ApiOperation(value = "導入數(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讀取文件中的內容,并加載到數(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內容到DictVo中,再將DictVO復制到Dict中。最后調用Dao層的方法將DIct添加到數(shù)據(jù)庫。
public class DictListener extends AnalysisEventListener<DictVo> { // 調用Dao private DictMapper dictMapper; public DictListener(DictMapper dictMapper) { this.dictMapper = dictMapper; } // 讀取Excel內容 @Override public void invoke(DictVo DictVo, AnalysisContext context) { // 將DictVO對象復制到Dict中 Dict dict = new Dict(); BeanUtils.copyProperties(DictVo, dict); // 將數(shù)據(jù)添加到數(shù)據(jù)庫 dictMapper.insert(dict); } @Override public void doAfterAllAnalysed(AnalysisContext context) { } }
2.頁面導入按鈕
3.測試數(shù)據(jù)導入到網(wǎng)頁
在Excel中準備兩條測試數(shù)據(jù):
將Excel通過頁面的 數(shù)據(jù)導入 按鈕上傳到數(shù)據(jù)庫:
成功將Excel中的數(shù)據(jù)導入數(shù)據(jù)庫,進而通過網(wǎng)頁展現(xiàn):
至此,使用EasyExcel從網(wǎng)頁導入導出數(shù)據(jù)的演示已經(jīng)完成,更多關于分布式醫(yī)療掛號系統(tǒng)的資料請關注腳本之家其它相關文章!
相關文章
Mybatis/Mybatis-Plus駝峰式命名映射的實現(xiàn)
本文主要介紹了Mybatis-Plus駝峰式命名映射的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07關于動態(tài)參數(shù)使用@PathVariable的解析
這篇文章主要介紹了關于動態(tài)參數(shù)使用@PathVariable的解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02SpringBoot整合Mybatis與thymleft實現(xiàn)增刪改查功能詳解
MybatisPlus是國產(chǎn)的第三方插件,?它封裝了許多常用的CURDapi,免去了我們寫mapper.xml的重復勞動。本文將整合MybatisPlus實現(xiàn)增刪改查功能,感興趣的可以了解一下2022-12-12