千家信息网

如何使用easypoi导入excel

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,这篇文章给大家介绍如何使用easypoi导入excel,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。使用easypoi导入exceleasypoi导入excel需要引入依赖:
千家信息网最后更新 2026年10月31日如何使用easypoi导入excel

这篇文章给大家介绍如何使用easypoi导入excel,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

使用easypoi导入excel

easypoi导入excel需要引入依赖:

                            cn.afterturn                easypoi-base                3.2.0                                        cn.afterturn                easypoi-web                3.2.0                                        cn.afterturn                easypoi-annotation                3.2.0            

首先我们准备一个excel模板并带有一些数据

第二步:准备一个实体对象

package com.xash.quartzDemo.entity;import cn.afterturn.easypoi.excel.annotation.Excel;import cn.afterturn.easypoi.excel.annotation.ExcelTarget;import com.fasterxml.jackson.annotation.JsonFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;@ExcelTarget("Layer")public class Layer {    private String layerId;    @Excel(name = "法规名称", isImportField = "true_st")    private String layerName;    @Excel(name = "法规描述", isImportField = "true_st")    private String description;       @Excel(name = "法规发布日期",importFormat = "yyyy-MM-dd")    private String releaseTime;     @Excel(name = "法规上传日期",importFormat = "yyyy-MM-dd")    private String recordTime;    private String fileName;    public String getLayerId() {        return layerId;    }    public void setLayerId(String layerId) {        this.layerId = layerId == null ? null : layerId.trim();    }    public String getLayerName() {        return layerName;    }    public void setLayerName(String layerName) {        this.layerName = layerName == null ? null : layerName.trim();    }    public String getDescription() {        return description;    }    public void setDescription(String description) {        this.description = description == null ? null : description.trim();    }   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    public String getReleaseTime() {        return releaseTime;    }      public void setReleaseTime(Date releaseTime) throws ParseException {                    this.releaseTime =sdf.format(releaseTime);    }    public String getRecordTime() {        return recordTime;    }    public void setRecordTime(Date recordTime) throws ParseException {             this.recordTime =sdf.format(recordTime);         }    public String getFileName() {        return fileName;    }    public void setFileName(String fileName) {        this.fileName = fileName == null ? null : fileName.trim();    }}

此处,日期格式对象excel到实体不能自动按格式转换,也不知道原因,所以手动格式化了日期

第三步

创建导入工具类

package com.xash.quartzDemo.util;import java.io.File;import java.util.List;import org.apache.commons.lang3.StringUtils;import org.springframework.web.multipart.MultipartFile;import cn.afterturn.easypoi.excel.ExcelImportUtil;import cn.afterturn.easypoi.excel.entity.ImportParams;public class Upload {          public static  String executeUpload1(String uploadDir,MultipartFile file,String fileName) throws Exception            {                //文件后缀名                String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));                //上传文件名                String filename = fileName + suffix;                //服务器端保存的文件对象                File serverFile = new File(uploadDir + filename);                //将上传的文件写入到服务器端文件内                file.transferTo(serverFile);                return filename;            }            public static   List importExcel(String filePath, Integer titleRows, Integer headerRows, Class pojoClass){                if (StringUtils.isBlank(filePath)){                    return null;                }                ImportParams params = new ImportParams();                params.setTitleRows(titleRows);                params.setHeadRows(headerRows);                List list = null;                try {                    list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);                }catch (Exception e) {                    e.printStackTrace();                }                return list;            }            public static   List importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class pojoClass){                if (file == null){                    return null;                }                ImportParams params = new ImportParams();                params.setTitleRows(titleRows);                params.setHeadRows(headerRows);                List list = null;                try {                    list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);                } catch (Exception e) {                    e.printStackTrace();                }                return list;            }}

本例使用 public static List importExcel(String filePath, Integer titleRows, Integer headerRows, Class pojoClass)方法

OK,准备工作做好后,就可以实现excel的导入工作了

准备一个controller前端控制器,

package com.xash.quartzDemo.controller;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.UUID;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.xash.quartzDemo.entity.Layer;import com.xash.quartzDemo.util.Upload;import cn.afterturn.easypoi.excel.entity.ImportParams;@RestControllerpublic class Eecelmport {           @RequestMapping("/excel")        public List excel() {          String fliename="模板导入.xlsx";          Listlist=null;;          try {                  list=Upload.importExcel("C:/Users/gaofei/Desktop/"+fliename, 1, 3, Layer.class);        } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();        }                                return list;        }}

前端访问:

http://192.168.2.81:8082/excel,后端响应并返回:

[{"layerId":null,"layerName":"hello","description":"hello","releaseTime":"2015-07-17","recordTime":"2015-09-17","fileName":null},{"layerId":null,"layerName":"world","description":"world","releaseTime":"2015-08-17","recordTime":"2015-10-17","fileName":null}]

关于如何使用easypoi导入excel就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

文件 日期 法规 准备 对象 格式 内容 前端 实体 更多 服务器 模板 工作 帮助 服务 不错 兴趣 原因 名称 后缀 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 重生之互联网科技帝国 疫情对互联网科技公司影响 互联网科技垄断 科技类互联网 互联网科技有限公司 翻译 广西拼得多互联网科技有限公司 成都互联网科技公司100强 互联网还是高科技吗 黑科技闪耀互联网之光 速维有品互联网信息科技(苏州)有限公司 互联网科技大奖都有哪些公司 互联网科技和法律行业 徐州市互联网科技有限公司 保险科技服务互联网营销体系 互联网公司高科技产品 西安有哪些互联网高科技企业 抖集互联网科技技术咨询 万物互联网智能科技 互联网领先科技活动 互联网大会出现的黑科技 科技金融是不是互联网金融 互联网科技巨头监管 工业互联网科技新基建 合肥互联网科技公司 卫星互联网科技股票 西藏七维空间互联网科技有限公司 恒银科技与互联网金融 辽宁互联网新科技 产业互联网科技服务公司 三亚科技创新互联网基地
0