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();
}
Aggregations