Excel03SaxReader

Excel03SaxReader

Introduction

In the standard ExcelReader, reading Excel can be very slow and may cause memory overflow if the data volume is large. Therefore, Hutool encapsulates an event-based reading method for large data volume Excel.

Excel03SaxReader only supports Sax reading of Excel 2003 format.

Usage

Defining a Row Processor

First, we implement the RowHandler interface, which is the core of Sax reading. By implementing the handle method, we can write the operation we want to perform on each row of data (such as storing it in a database, in a List, or writing it to a file). Here, we just print it on the console.

private RowHandler createRowHandler() {
 return new RowHandler() {
 @Override
 public void handle(int sheetIndex, long rowIndex, List<Object> rowlist) {
 Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist);
 }
 };
}

Quick Reading with ExcelUtil

ExcelUtil.readBySax("aaa.xls", 1, createRowHandler());

Building an Object Reader

Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
reader.read("aaa.xls", 0);

The second parameter of the reader method is the index of the sheet. -1 means read all sheets, 0 means the first sheet, and so on.