1. 程式人生 > >Java使用easyExcel操作Excel案例

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<ImportInfo> getDate(){
    List<ImportInfo> list = new ArrayList<ImportInfo>();
    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;
    }
}