1. 程式人生 > >Java-使用easyexcel讀大檔案

Java-使用easyexcel讀大檔案

使用easyexcel讀大excel檔案,避免OOM錯誤。

依賴:

compile group: 'com.alibaba', name: 'easyexcel', version: '1.0.4'

程式碼:

package com.tsaoko.sched.service.task;

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 java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Walker
 * @since 1.0 2018-09-28
 */
public class EasyExcelTest {

    public static void main(String[] args) {
        read();
       //write();
    }

    private static void read() {
        try(InputStream inputStream = new FileInputStream("C:\\shopify stores.xlsx")) {
            ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.XLSX, null, new AnalysisEventListener<List<String>>() {

                @Override
                public void invoke(List<String> object, AnalysisContext context) {
                    System.out.println("當前sheet:" + context.getCurrentSheet().getSheetNo() + ",當前行:" +
                            context.getCurrentRowNum());
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext context) {

                }
            });
            excelReader.read();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void write() {
        try(OutputStream outputStream = new FileOutputStream("C:\\outputtest.xlsx")) {
            ExcelWriter writer = new ExcelWriter(outputStream, ExcelTypeEnum.XLSX);
            Sheet sheet = new Sheet(1, 0, ExcelPropertyIndexModel.class);
            writer.write(getData(), sheet);
            writer.finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static List<ExcelPropertyIndexModel> getData() {
        List<ExcelPropertyIndexModel> list = new ArrayList<>();
        list.add(new ExcelPropertyIndexModel("t1"));
        list.add(new ExcelPropertyIndexModel("t2"));
        list.add(new ExcelPropertyIndexModel("t3"));
        list.add(new ExcelPropertyIndexModel("t4"));
        list.add(new ExcelPropertyIndexModel("t5"));
        return list;
    }

}

雖然讀大檔案沒有報OOM錯誤,但讀取過程比較慢,可以換個思路,將excel檔案轉換為csv格式,再根據需要將大檔案分隔成小檔案,處理後合併結果。