分布式醫(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>
頁面導(dǎo)出按鈕

4.測試數(shù)據(jù)導(dǎo)出到Excel

在頁面單擊 數(shù)據(jù)導(dǎo)出 按鈕后,跳出下載框,成功將頁面數(shù)據(jù)下載到本地.xlsx文件中。

成功將數(shù)據(jù)下載到本地

二、導(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)入按鈕

前端頁面導(dǎo)入代碼

3.測試數(shù)據(jù)導(dǎo)入到網(wǎng)頁

在Excel中準(zhǔn)備兩條測試數(shù)據(jù):


Excel中準(zhǔn)備兩條測試數(shù)據(jù)

將Excel通過頁面的 數(shù)據(jù)導(dǎo)入 按鈕上傳到數(shù)據(jù)庫:


在頁面點擊數(shù)據(jù)導(dǎo)入

成功將Excel中的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫,進(jìn)而通過網(wǎng)頁展現(xiàn):
成功將Excel數(shù)據(jù)導(dǎo)入

至此,使用EasyExcel從網(wǎng)頁導(dǎo)入導(dǎo)出數(shù)據(jù)的演示已經(jīng)完成。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容