Java : 阿里EasyExcel读取不OOM工具类应用

开发过程中,对excel读写操作应用频繁,通常使用比较多的是Apache下pio,关于pio读写时出现的OOM一直是困惑大家的问题,pio比较消耗内存;而阿里的EasyExcel基于pio基础上进行了优化,大大提高的excel读取操作效率;阿里出品,必属精品。


阿里EasyExcel在工作中经过测试,写入10W条数据到excel耗时2~3秒,这样的效率确实很6;而且重要的是,代码实现方面也极其简单,几行代码就可以实现读取操作,相比较pio比较复杂的操作,EasyExcel用得更加顺手。


整个操作流程如下:

  1. 引入依赖
  2. 
        com.alibaba
        easyexcel
        1.1.2-beat1
    
  3. 写操作示例代码
        @Test
    	public void writeModeTest() throws Exception {
    		Instant start = Instant.now();
    		OutputStream out = new FileOutputStream("D:/home/writer.xlsx");
    		ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
    		
    		Sheet sheet1 = new Sheet(1, 0, EasyExcelModel.class, "第一个sheet", null);
    		// sheet1.setSheetName("第一个sheet");
    		writer.write(this.simpleData(), sheet1);
    
    		// 第2个sheet
    		Sheet sheet2 = new Sheet(2, 0, EasyExcelModel.class, "第二个sheet", null);
    		// sheet1.setSheetName("第二个sheet");
    		writer.write(this.simpleData().subList(100, 300), sheet2);
    		
    		writer.finish();
    		out.close();
    		System.out.println(Duration.between(start, Instant.now()).toMillis());
    		// 10000 : 1083
    		// 100000 : 2165
    	}
        private List simpleData() {
    		List list = new ArrayList<>();
    		EasyExcelModel user = null;
    		for (int i = 0; i < 3000; i++) {
    			user = new EasyExcelModel();
    			user.setName("姓名" + i + i);
    			user.setAge(i + 10 + "岁");
    			user.setEmail(i + "@qq.com");
    			user.setAddress("深圳罗湖" + i);
    			user.setGender(i % 2 == 0 ? "男" : "女");
    			list.add(user);
    		}
    		return list;
    	}

    4.读操作示例代码

    	@Test
    	public void readModeTest() throws Exception {
    		InputStream in = new FileInputStream("D:/home/writer.xlsx");
    		
    		// 大文件读取,10000以上
    //		List datas = new ArrayList<>();
    //		EasyExcelFactory.readBySax(in, new Sheet(2, 1, EasyExcelModel.class), new AnalysisEventListener() {
    //			@Override
    //			public void invoke(EasyExcelModel object, AnalysisContext context) {
    //				System.out.println("当前行 : " + context.getCurrentRowNum() + JSON.toJSONString(object));
    //				datas.add(object);
    //				// doSomething(object);//调用其他方法处理业务
    //			}
    //
    //			@Override
    //			public void doAfterAllAnalysed(AnalysisContext context) {
    //				// datas.clear();
    //			}
    //		});
    //		System.err.println("List : " + JSON.toJSONString(datas));
    
    		// 快速阅读的小文件,不超过10000行
    		List data = EasyExcelFactory.read(in, new Sheet(2, 0, EasyExcelModel.class));
    		System.out.println(data.size() + " - READ : " + JSON.toJSONString(data));
    
    		in.close();
    	} 
    package com.zy.demo;
    
    import java.io.Serializable;
    
    import com.alibaba.excel.annotation.ExcelProperty;
    import com.alibaba.excel.metadata.BaseRowModel;
    
    public class EasyExcelModel extends BaseRowModel implements Serializable {
    
    	private static final long serialVersionUID = 3277058737576734656L;
    
    	@ExcelProperty(value = "姓名", index = 0)
    	private String name;
    	@ExcelProperty(value = "年龄", index = 1)
    	private String age;
    	@ExcelProperty(value = "邮箱", index = 2)
    	private String email;
    	@ExcelProperty(value = "地址", index = 3)
    	private String address;
    	@ExcelProperty(value = "性别", index = 4)
    	private String gender;
    
    	public EasyExcelModel() {
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public String getAge() {
    		return age;
    	}
    
    	public String getEmail() {
    		return email;
    	}
    
    	public String getAddress() {
    		return address;
    	}
    
    	public String getGender() {
    		return gender;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public void setAge(String age) {
    		this.age = age;
    	}
    
    	public void setEmail(String email) {
    		this.email = email;
    	}
    
    	public void setAddress(String address) {
    		this.address = address;
    	}
    
    	public void setGender(String gender) {
    		this.gender = gender;
    	}
    
    }

     

    你可能感兴趣的:(Java实战)