Search in sources :

Example 1 with ImportParams

use of cn.afterturn.easypoi.excel.entity.ImportParams in project my_curd by qinyou.

the class ExSingleTableController method importExcel.

/**
 * 导入excel
 */
@Before(Tx.class)
public void importExcel() {
    UploadFile uploadFile = getFile();
    if (uploadFile == null) {
        renderFail("上传文件不可为空");
        return;
    }
    if (!FilenameUtils.getExtension(uploadFile.getFileName()).equals("xls")) {
        FileUtils.deleteFile(uploadFile.getFile());
        renderFail("上传文件后缀必须是xls");
        return;
    }
    List<ExSingleTable> list;
    try {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        list = ExcelImportUtil.importExcel(uploadFile.getFile(), ExSingleTable.class, params);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        FileUtils.deleteFile(uploadFile.getFile());
        renderFail("模板文件格式错误");
        return;
    }
    for (ExSingleTable exSingleTable : list) {
        System.out.println(exSingleTable);
        System.out.println();
        exSingleTable.setId(IdUtils.id()).setCreater(WebUtils.getSessionUsername(this)).setCreateTime(new Date()).save();
    }
    FileUtils.deleteFile(uploadFile.getFile());
    renderSuccess(IMPORT_SUCCESS);
}
Also used : UploadFile(com.jfinal.upload.UploadFile) ImportParams(cn.afterturn.easypoi.excel.entity.ImportParams) ExSingleTable(com.github.qinyou.example.model.ExSingleTable) Date(java.util.Date) Before(com.jfinal.aop.Before)

Example 2 with ImportParams

use of cn.afterturn.easypoi.excel.entity.ImportParams 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)

Example 3 with ImportParams

use of cn.afterturn.easypoi.excel.entity.ImportParams in project my_curd by qinyou.

the class SysSettingController method importExcel.

/**
 * 导入excel
 */
@Before(Tx.class)
public void importExcel() {
    UploadFile uploadFile = getFile();
    if (uploadFile == null) {
        renderFail("上传文件不可为空");
        return;
    }
    if (!FilenameUtils.getExtension(uploadFile.getFileName()).equals("xls")) {
        FileUtils.deleteFile(uploadFile.getFile());
        renderFail("上传文件后缀必须是xls");
        return;
    }
    List<SysSetting> list;
    try {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        list = ExcelImportUtil.importExcel(uploadFile.getFile(), SysSetting.class, params);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        FileUtils.deleteFile(uploadFile.getFile());
        renderFail("模板文件格式错误");
        return;
    }
    for (SysSetting sysSetting : list) {
        sysSetting.setId(IdUtils.id()).setUpdater(WebUtils.getSessionUsername(this)).setUpdateTime(new Date()).save();
    }
    FileUtils.deleteFile(uploadFile.getFile());
    refreshSetting();
    renderSuccess(IMPORT_SUCCESS);
}
Also used : UploadFile(com.jfinal.upload.UploadFile) SysSetting(com.github.qinyou.system.model.SysSetting) ImportParams(cn.afterturn.easypoi.excel.entity.ImportParams) Date(java.util.Date) Before(com.jfinal.aop.Before)

Aggregations

ImportParams (cn.afterturn.easypoi.excel.entity.ImportParams)3 Before (com.jfinal.aop.Before)2 UploadFile (com.jfinal.upload.UploadFile)2 Date (java.util.Date)2 ExSingleTable (com.github.qinyou.example.model.ExSingleTable)1 SysSetting (com.github.qinyou.system.model.SysSetting)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