Search in sources :

Example 1 with Dict

use of ebag.bean.Dict in project app-template by xtuer.

the class DictController method importDicts.

/**
 * 导入 Excel xlsx 中的字典数据到据数据库
 *
 * @param file Excel 字典文件
 */
@PostMapping(Urls.FORM_DICTS_IMPORT)
@ResponseBody
public Result importDicts(@RequestParam("file") MultipartFile file) {
    try {
        // 1. 使用 EasyPOI 解析上传的文件 file 中的内容为 Dict 列表
        InputStream in = file.getInputStream();
        ImportParams params = new ImportParams();
        // Excel 中必须有这几个列,否则格式不正确抛异常
        params.setImportFields(new String[] { "编码", "值", "分类" });
        List<Dict> dicts = ExcelImportUtil.importExcel(in, Dict.class, params);
        IOUtils.closeQuietly(in);
        // 2. 导入数据库
        for (Dict dict : dicts) {
            dict.setId(idWorker.nextId());
            dictMapper.insertOrUpdateDict(dict);
        }
    } catch (Exception ex) {
        return Result.fail(ExceptionUtils.getStackTrace(ex));
    }
    return Result.ok();
}
Also used : ImportParams(cn.afterturn.easypoi.excel.entity.ImportParams) InputStream(java.io.InputStream) Dict(ebag.bean.Dict) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

ImportParams (cn.afterturn.easypoi.excel.entity.ImportParams)1 Dict (ebag.bean.Dict)1 InputStream (java.io.InputStream)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1