快速入门EasyExcel
        【摘要】 快速入门EasyExcel
    
    
    
    环境介绍
| 
 技术栈  | 
 springboot+mybatis-plus+mysql+easyexcel  | 
| 
 软件  | 
 版本  | 
| 
 8  | 
|
| 
 IDEA  | 
 IntelliJ IDEA 2022.2.1  | 
| 
 JDK  | 
 1.8  | 
| 
 Spring Boot  | 
 2.7.13  | 
| 
 mybatis-plus  | 
 3.5.3.2  | 
EasyExcel是一个基于Java的、快速、简洁、解决大文件内存溢出的Excel处理工具。
他能让你在不用考虑性能、内存的等因素的情况下,快速完成Excel的读、写等功能。
官网https://easyexcel.opensource.alibaba.com/
加入依赖
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.3.2</version>
</dependency>
读Excel

- 编写实体类
 
@TableName(value ="product")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Product implements Serializable {
    /**
     * 序号_自动生成
     */
    @TableId(type = IdType.AUTO)
    @ExcelProperty("序号")
    private Integer number;
    /**
     * 创建时间
     */
    @ExcelProperty("创建时间")
    private Date createtime;
    /**
     * 产品名称
     */
    @ExcelProperty("产品名称")
    private String productname;
    /**
     * 产品编号
     */
    @ExcelProperty("产品编号")
    private String productnumber;
    /**
     * 产品型号
     */
    @ExcelProperty("产品型号")
    private String manufacturer;
    /**
     * 产品位置
     */
    @ExcelProperty("产品位置")
    private String producepath;
    /**
     * 图片位置
     */
    @ExcelProperty("图片位置")
    private String imagepath;
    /**
     * 使用单位
     */
    @ExcelProperty("使用单位")
    private String unit;
    /**
     * 金额
     */
    @ExcelProperty("金额")
    private Integer money;
    /**
     * 入库时间
     */
    @ExcelProperty("入库时间")
    private Date intime;
    /**
     * 出库时间
     */
    @ExcelProperty("出库时间")
    private Date puttime;
    /**
     * 操作人
     */
    @ExcelProperty("操作人")
    private String operator;
    /**
     * 创建人
     */
    @ExcelProperty("创建人")
    private String createduser;
    /**
     * 备注
     */
    @ExcelProperty("备注")
    private String notes;
    /**
     * 产品数量
     */
    @ExcelProperty("产品数量")
    private Integer producedigit;
    /**
     * 产品单位
     */
    @ExcelProperty("产品单位")
    private String productunit;
    @TableField(exist = false)
    private static final long serialVersionUID = 1L;
}
- 监听器
 
使用官方默认提供的监听器
- 调用API
 
@Test
public void simpleRead() {
    //String fileName = TestFileUtil.getPath() + "simpleWriteTest1702391756908.xlsx";
    String fileName = "C:\\Users\\1111\\Desktop\\simpleWriteTest1702391756908.xlsx";
    // 这里默认每次会读取100条数据 然后返回过来 直接调用使用数据就行
    // 具体需要返回多少行可以在`PageReadListener`的构造函数设置
    EasyExcel.read(fileName, Product.class, new PageReadListener<Product>(dataList -> {
        for (Product product : dataList) {
            System.out.println(product);
            //log.info("读取到一条数据{}", JSON.toJSONString(product));
        }
    })).sheet().doRead();
}
写Excel

TestFileUtil工具类
public class TestFileUtil {
    public static InputStream getResourcesFileInputStream(String fileName) {
        return Thread.currentThread().getContextClassLoader().getResourceAsStream("" + fileName);
    }
    public static String getPath() {
        return TestFileUtil.class.getResource("/").getPath();
    }
    public static TestPathBuild pathBuild() {
        return new TestPathBuild();
    }
    public static File createNewFile(String pathName) {
        File file = new File(getPath() + pathName);
        if (file.exists()) {
            file.delete();
        } else {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
        }
        return file;
    }
    public static File readFile(String pathName) {
        return new File(getPath() + pathName);
    }
    public static File readUserHomeFile(String pathName) {
        return new File(System.getProperty("user.home") + File.separator + pathName);
    }
    /**
     * build to test file path
     **/
    public static class TestPathBuild {
        private TestPathBuild() {
            subPath = new ArrayList<>();
        }
        private final List<String> subPath;
        public TestPathBuild sub(String dirOrFile) {
            subPath.add(dirOrFile);
            return this;
        }
        public String getPath() {
            if (CollectionUtils.isEmpty(subPath)) {
                return TestFileUtil.class.getResource("/").getPath();
            }
            if (subPath.size() == 1) {
                return TestFileUtil.class.getResource("/").getPath() + subPath.get(0);
            }
            StringBuilder path = new StringBuilder(TestFileUtil.class.getResource("/").getPath());
            path.append(subPath.get(0));
            for (int i = 1; i < subPath.size(); i++) {
                path.append(File.separator).append(subPath.get(i));
            }
            return path.toString();
        }
    }
}
#
- 实体类编写
 
@TableName(value ="product")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Product implements Serializable {
    /**
     * 序号_自动生成
     */
    @TableId(type = IdType.AUTO)
    @ExcelProperty("序号")
    private Integer number;
    /**
     * 创建时间
     */
    @ExcelProperty("创建时间")
    private Date createtime;
    /**
     * 产品名称
     */
    @ExcelProperty("产品名称")
    private String productname;
    /**
     * 产品编号
     */
    @ExcelProperty("产品编号")
    private String productnumber;
    /**
     * 产品型号
     */
    @ExcelProperty("产品型号")
    private String manufacturer;
    /**
     * 产品位置
     */
    @ExcelProperty("产品位置")
    private String producepath;
    /**
     * 图片位置
     */
    @ExcelProperty("图片位置")
    private String imagepath;
    /**
     * 使用单位
     */
    @ExcelProperty("使用单位")
    private String unit;
    /**
     * 金额
     */
    @ExcelProperty("金额")
    private Integer money;
    /**
     * 入库时间
     */
    @ExcelProperty("入库时间")
    private Date intime;
    /**
     * 出库时间
     */
    @ExcelProperty("出库时间")
    private Date puttime;
    /**
     * 操作人
     */
    @ExcelProperty("操作人")
    private String operator;
    /**
     * 创建人
     */
    @ExcelProperty("创建人")
    private String createduser;
    /**
     * 备注
     */
    @ExcelProperty("备注")
    private String notes;
    /**
     * 产品数量
     */
    @ExcelProperty("产品数量")
    private Integer producedigit;
    /**
     * 产品单位
     */
    @ExcelProperty("产品单位")
    private String productunit;
    @TableField(exist = false)
    private static final long serialVersionUID = 1L;
}
- 数据生成方法
 
private List<Product> getData(int count) {
    List<Product> list = ListUtils.newArrayList();
    for (int i = 0; i < count; i++) {
        Product product = new Product();
        product.setCreatetime(new Date());
        product.setProductname("服务器00"+i);
        product.setProductnumber(DigestUtils.md5Hex("hello"+i));
        product.setManufacturer("huawei");
        product.setProducepath("/test");
        product.setImagepath("/test");
        product.setIntime(new Date());
        product.setOperator("张三");
        product.setPuttime(new Date());
        list.add(product);
    }
    return list;
}
- 写入excel
 
@Test
public void simpleWrite() {
    // 写法2
    String fileName = TestFileUtil.getPath() + "simpleWriteTest" + System.currentTimeMillis() + ".xlsx";
    // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
    // 如果这里想使用03 则 传入excelType参数即可
    EasyExcel.write(fileName, Product.class).sheet("测试01").doWrite(getData(10));
}

 
            【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
                cloudbbs@huaweicloud.com
                
            
        
        
        
        
        
        
        - 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)