Search in sources :

Example 1 with PageReadListener

use of com.alibaba.excel.read.listener.PageReadListener in project easyexcel by alibaba.

the class ReadTest method simpleRead.

/**
 * 最简单的读
 * <p>
 * 1. 创建excel对应的实体对象 参照{@link DemoData}
 * <p>
 * 2. 由于默认一行行的读取excel,所以需要创建excel一行一行的回调监听器,参照{@link DemoDataListener}
 * <p>
 * 3. 直接读即可
 */
@Test
public void simpleRead() {
    // 写法1:JDK8+ ,不用额外写一个DemoDataListener
    // since: 3.0.0-beta1
    String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
    // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
    // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
    EasyExcel.read(fileName, DemoData.class, new PageReadListener<DemoData>(dataList -> {
        for (DemoData demoData : dataList) {
            log.info("读取到一条数据{}", JSON.toJSONString(demoData));
        }
    })).sheet().doRead();
    // 写法2:
    // 匿名内部类 不用额外写一个DemoDataListener
    fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
    // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
    EasyExcel.read(fileName, DemoData.class, new ReadListener<DemoData>() {

        /**
         * 单次缓存的数据量
         */
        public static final int BATCH_COUNT = 100;

        /**
         *临时存储
         */
        private List<DemoData> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

        @Override
        public void invoke(DemoData data, AnalysisContext context) {
            cachedDataList.add(data);
            if (cachedDataList.size() >= BATCH_COUNT) {
                saveData();
                // 存储完成清理 list
                cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
            }
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext context) {
            saveData();
        }

        /**
         * 加上存储数据库
         */
        private void saveData() {
            log.info("{}条数据,开始存储数据库!", cachedDataList.size());
            log.info("存储数据库成功!");
        }
    }).sheet().doRead();
    // 有个很重要的点 DemoDataListener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去
    // 写法3:
    fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
    // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
    EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead();
    // 写法4: 使用 try-with-resources @since 3.1.0
    fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
    // 一个文件一个reader
    try (ExcelReader excelReader = EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).build()) {
        // 构建一个sheet 这里可以指定名字或者no
        ReadSheet readSheet = EasyExcel.readSheet(0).build();
        // 读取一个sheet
        excelReader.read(readSheet);
    }
    // 写法5: 不使用 try-with-resources
    fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
    // 一个文件一个reader
    ExcelReader excelReader = null;
    try {
        excelReader = EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).build();
        // 构建一个sheet 这里可以指定名字或者no
        ReadSheet readSheet = EasyExcel.readSheet(0).build();
        // 读取一个sheet
        excelReader.read(readSheet);
    } finally {
        if (excelReader != null) {
            // 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的
            excelReader.close();
        }
    }
}
Also used : ExcelReader(com.alibaba.excel.ExcelReader) ExcelProperty(com.alibaba.excel.annotation.ExcelProperty) Test(org.junit.Test) EasyExcel(com.alibaba.excel.EasyExcel) File(java.io.File) DefaultConverterLoader(com.alibaba.excel.converters.DefaultConverterLoader) ListUtils(com.alibaba.excel.util.ListUtils) List(java.util.List) TestFileUtil(com.alibaba.easyexcel.test.util.TestFileUtil) AnalysisContext(com.alibaba.excel.context.AnalysisContext) Slf4j(lombok.extern.slf4j.Slf4j) JSON(com.alibaba.fastjson.JSON) Ignore(org.junit.Ignore) ReadSheet(com.alibaba.excel.read.metadata.ReadSheet) ReadListener(com.alibaba.excel.read.listener.ReadListener) Map(java.util.Map) PageReadListener(com.alibaba.excel.read.listener.PageReadListener) CellExtraTypeEnum(com.alibaba.excel.enums.CellExtraTypeEnum) NumberFormat(com.alibaba.excel.annotation.format.NumberFormat) DateTimeFormat(com.alibaba.excel.annotation.format.DateTimeFormat) ExcelReader(com.alibaba.excel.ExcelReader) AnalysisContext(com.alibaba.excel.context.AnalysisContext) ReadSheet(com.alibaba.excel.read.metadata.ReadSheet) Test(org.junit.Test)

Aggregations

TestFileUtil (com.alibaba.easyexcel.test.util.TestFileUtil)1 EasyExcel (com.alibaba.excel.EasyExcel)1 ExcelReader (com.alibaba.excel.ExcelReader)1 ExcelProperty (com.alibaba.excel.annotation.ExcelProperty)1 DateTimeFormat (com.alibaba.excel.annotation.format.DateTimeFormat)1 NumberFormat (com.alibaba.excel.annotation.format.NumberFormat)1 AnalysisContext (com.alibaba.excel.context.AnalysisContext)1 DefaultConverterLoader (com.alibaba.excel.converters.DefaultConverterLoader)1 CellExtraTypeEnum (com.alibaba.excel.enums.CellExtraTypeEnum)1 PageReadListener (com.alibaba.excel.read.listener.PageReadListener)1 ReadListener (com.alibaba.excel.read.listener.ReadListener)1 ReadSheet (com.alibaba.excel.read.metadata.ReadSheet)1 ListUtils (com.alibaba.excel.util.ListUtils)1 JSON (com.alibaba.fastjson.JSON)1 File (java.io.File)1 List (java.util.List)1 Map (java.util.Map)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1