Ali Open Source Quick and easy to avoid OOM Excel processing tools

Ali Open Source Quick and easy to avoid OOM Excel processing tools

2022-09-02 0 736
Resource Number 38023 Last Updated 2025-02-24
¥ 0USD Upgrade VIP
Download Now Matters needing attention
Can't download? Please contact customer service to submit a link error!
Value-added Service: Installation Guide Environment Configuration Secondary Development Template Modification Source Code Installation

This issue recommends an Ali open source Java-based Excel parsing tool——EasyExcel。

Ali Open Source Quick and easy to avoid OOM Excel processing tools插图

Java parsing, the more famous framework has Apache poi, jxl, but they all have a serious problem is memory consumption, poi has a special SAX pattern can solve some memory problems to a certain extent, but poi still has some defects, For example, some versions of Excel decompression and storage after decompression are completed in memory, memory is still a lot of consumption. easyexcel rewrites the poi analysis of Excel, a 3M Excel file using poi analysis still needs about 100M memory, after switching to easyexcel can be reduced to a few M, no matter how large excel will not appear memory calls.

latest version

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>easyexcel</artifactId>

<version>3.0.5</version>

</dependency>

give a typical example

  • read Excel

Ali Open Source Quick and easy to avoid OOM Excel processing tools插图1

object

@Getter
@Setter
@EqualsAndHashCode
public class DemoData {
    private String string;
    private Date date;
    private Double doubleData;
}

audio monitor

// There's a very important point DemoDataListener Cannot be managed by spring,You want to call new every time you read excel,And then you use spring inside and you can pass constructors in
@Slf4j
public class DemoDataListener implements ReadListener<DemoData> {

    /**
     * Every 5 pieces of storage database, in actual use can be 100, and then clear the list, convenient memory recycling
     */
    private static final int BATCH_COUNT = 100;
    /**
     * Cached data
     */
    private List<DemoData> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    /**
     * Let's say this is a DAO, and of course there's business logic and this could also be a service. Of course it's useless if you don't have to store this object.
     */
    private DemoDAO demoDAO;

    public DemoDataListener() {
        // This is a demo, so just make a new one. If you are in spring, use the following parameterized constructor
        demoDAO = new DemoDAO();
    }

    /**
     * If spring is used, use this constructor. Each time you create a Listener, you need to pass in a Spring-managed class
     *
     * @param demoDAO
     */
    public DemoDataListener(DemoDAO demoDAO) {
        this.demoDAO = demoDAO;
    }

    /**
     * This will be called for every piece of data parsing
     *
     * @param data    one row value. Is is same as {@link AnalysisContext#readRowHolder()}
     * @param context
     */
    @Override
    public void invoke(DemoData data, AnalysisContext context) {
        log.info("Parse a piece of data:{}", JSON.toJSONString(data));
        cachedDataList.add(data);
        // When BATCH_COUNT is reached, the database needs to be stored once to prevent tens of thousands of data from being stored in memory, which is easy to OOM
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData();
            // Memory completion cleanup list
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    /**
     * It's called when all the data is parsed
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // This is also where data is saved to ensure that the last remaining data is also stored in the database
        saveData();
        log.info("All data analysis completed!");
    }

    /**
     * Plus storage database
     */
    private void saveData() {
        log.info("{}Start to store the database!", cachedDataList.size());
        demoDAO.save(cachedDataList);
        log.info("Save database successfully!");
    }
}

persistent layer

/**
 * Let's say this is your DAO store. Of course you need this class for spring to manage, of course you don't need storage, and you don't need this class.
 **/
public class DemoDAO {
    public void save(List<DemoData> list) {
        // If it is mybatis, try not to call insert directly many times, write a new mapper method batchInsert, all the data is inserted at once
    }
}

The simplest read sample code

    /**
     * The easiest read
     * <p>
     * 1. To create an entity object corresponding to excel, go to {@link DemoData}
     * <p>
     * 2. Since excel is read line by line by default, you need to create a callback listener for excel line by line, see {@link DemoDataListener}
     * <p>
     * 3. Just read
     */
    @Test
    public void simpleRead() {
        // Script 1: JDK8+, no need to write an additional DemoDataListener
        // since: 3.0.0-beta1
        String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
        // Here you need to specify which class to read, and then read the first sheet file stream will automatically close
        // So you're going to read 3000 pieces of data at a time and then you're going to go back and you're just going to use the data
        EasyExcel.read(fileName, DemoData.class, new PageReadListener<DemoData>(dataList -> {
            for (DemoData demoData : dataList) {
                log.info("A piece of data was read{}", JSON.toJSONString(demoData));
            }
        })).sheet().doRead();

        // Writing Method 2:
        // Anonymous inner classes don't have to write an extra one DemoDataListener
        fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
        // Here you need to specify which class to read, and then read the first sheet file stream will automatically close
        EasyExcel.read(fileName, DemoData.class, new ReadListener<DemoData>() {
            /**
             * The amount of data cached at a time
             */
            public static final int BATCH_COUNT = 100;
            /**
             *temporary storage
             */
            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();
                    // Memory completion cleanup list
                    cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
                }
            }

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

            /**
             * Plus storage database
             */
            private void saveData() {
                log.info("{}Start to store the database!", cachedDataList.size());
                log.info("Save database successfully!");
            }
        }).sheet().doRead();

        // There's a very important point DemoDataListener It can't be managed by spring, you need to get new every time you read excel, and then you can use spring to build methods into it
        // Writing Method 3:
        fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
        // Here you need to specify which class to read, and then read the first sheet file stream will automatically close
        EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead();

        // Writing Method 4:
        fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
        // File by file reader
        ExcelReader excelReader = null;
        try {
            excelReader = EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).build();
            // Build a sheet here you can specify a name or no
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            // Read one sheet
            excelReader.read(readSheet);
        } finally {
            if (excelReader != null) {
                // Don't forget to close it here, it will create a temporary file while reading, and the disk will crash
                excelReader.finish();
            }
        }
    }

64M内存20秒读取75M(46W行25列)的Excel:

Ali Open Source Quick and easy to avoid OOM Excel processing tools插图2

Of course, the quick mode is faster, but the memory usage is a little more than 100M.

You can read more on your own.

资源下载此资源为免费资源立即下载
Telegram:@John_Software

Disclaimer: This article is published by a third party and represents the views of the author only and has nothing to do with this website. This site does not make any guarantee or commitment to the authenticity, completeness and timeliness of this article and all or part of its content, please readers for reference only, and please verify the relevant content. The publication or republication of articles by this website for the purpose of conveying more information does not mean that it endorses its views or confirms its description, nor does it mean that this website is responsible for its authenticity.

Ictcoder Free source code Ali Open Source Quick and easy to avoid OOM Excel processing tools https://ictcoder.com/kyym/ali-open-source-quick-and-easy-to-avoid-oom-excel-processing-tools.html

Share free open-source source code

Q&A
  • 1, automatic: after taking the photo, click the (download) link to download; 2. Manual: After taking the photo, contact the seller to issue it or contact the official to find the developer to ship.
View details
  • 1, the default transaction cycle of the source code: manual delivery of goods for 1-3 days, and the user payment amount will enter the platform guarantee until the completion of the transaction or 3-7 days can be issued, in case of disputes indefinitely extend the collection amount until the dispute is resolved or refunded!
View details
  • 1. Heptalon will permanently archive the process of trading between the two parties and the snapshots of the traded goods to ensure that the transaction is true, effective and safe! 2, Seven PAWS can not guarantee such as "permanent package update", "permanent technical support" and other similar transactions after the merchant commitment, please identify the buyer; 3, in the source code at the same time there is a website demonstration and picture demonstration, and the site is inconsistent with the diagram, the default according to the diagram as the dispute evaluation basis (except for special statements or agreement); 4, in the absence of "no legitimate basis for refund", the commodity written "once sold, no support for refund" and other similar statements, shall be deemed invalid; 5, before the shooting, the transaction content agreed by the two parties on QQ can also be the basis for dispute judgment (agreement and description of the conflict, the agreement shall prevail); 6, because the chat record can be used as the basis for dispute judgment, so when the two sides contact, only communicate with the other party on the QQ and mobile phone number left on the systemhere, in case the other party does not recognize self-commitment. 7, although the probability of disputes is very small, but be sure to retain such important information as chat records, mobile phone messages, etc., in case of disputes, it is convenient for seven PAWS to intervene in rapid processing.
View details
  • 1. As a third-party intermediary platform, Qichou protects the security of the transaction and the rights and interests of both buyers and sellers according to the transaction contract (commodity description, content agreed before the transaction); 2, non-platform online trading projects, any consequences have nothing to do with mutual site; No matter the seller for any reason to require offline transactions, please contact the management report.
View details

Related Article

make a comment
No comments available at the moment
Official customer service team

To solve your worries - 24 hours online professional service