Java使用easyExcel操作Excel案例

  这两天一直在玩些小工具,今天整了下阿里巴巴的easyExcel,下面是案例:

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.read.context.AnalysisContext;
import com.alibaba.excel.read.event.AnalysisEventListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class TestExcel {

  @Test
    public void testRead() throws FileNotFoundException {
    InputStream inputStream =getInputStream("C:\\Users\\LiGe\\Desktop\\test.xls");
    try {
      ExcelReader reader = new ExcelReader(inputStream, ExcelTypeEnum.XLS, null, new AnalysisEventListener() {
        @Override
        public void invoke(Object o, AnalysisContext analysisContext) {
          System.out.println("当前sheet"+analysisContext.getCurrentSheet().getSheetNo()+ " 当前行:" + analysisContext.getCurrentRowNum()
                  + " data:" + o);
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }
      });
      reader.read();
    }catch (Exception e){
  e.printStackTrace();
    }finally {
      try {
        inputStream.close();
      }catch (IOException e){
        e.printStackTrace();
      }
    }
  }

  @Test
  public void testWriter() throws FileNotFoundException {
    OutputStream out = new FileOutputStream("C:\\Users\\LiGe\\Desktop\\test.xls");
    try {
      ExcelWriter writer = new ExcelWriter(out,ExcelTypeEnum.XLS);
      //写第一个sheet
      Sheet sheet = new Sheet(2,3,ImportInfo.class);
      writer.write(getDate(),sheet);
      for (ImportInfo in: getDate()
           ) {
        System.out.println(in.getName());
      }
      writer.finish();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        out.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  public List getDate(){
    List list = new ArrayList();
    ImportInfo info = new ImportInfo();
    info.setAge(12);
    info.setName("zhangsan");
    info.setEmail("[email protected]");
    ImportInfo info1 = new ImportInfo();
    info1.setAge(12);
    info1.setName("zhangsan1");
    info1.setEmail("[email protected]");
    ImportInfo info2 = new ImportInfo();
    info2.setAge(12);
    info2.setName("zhangsan2");
    info2.setEmail("[email protected]");
    list.add(info);list.add(info1);list.add(info2);
    return list;
  }



  private InputStream getInputStream(String fileName) {
    try {
      return new FileInputStream(new File(fileName));
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
      return null;
  }
}

上面是测试类,这是实体类:

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;

public class ImportInfo extends BaseRowModel {
    @ExcelProperty(index = 0)
    private String name;
    @ExcelProperty(index = 1)
    private Integer age;
    @ExcelProperty(index = 2)
    private String email;
    /*
    通过 @ExcelProperty 注解与 index 变量可以标注成员变量所映射的列
    作为Excel的模型对象,需要setter方法
     */
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

你可能感兴趣的:(Java)