Introduce the technical principles of the "Streaming Excel Reader" framework in the Java class library
"Streaming Excel Reader" is a Java class library for reading data from the Excel file.Its technical principle is based on streaming processing, which can efficiently read large Excel files without loading the entire file into memory.
Under normal circumstances, the use of event -based parsing to read Excel files is relatively low, because it needs to load the entire file to the memory and then analyze it.However, Streaming Excel Reader uses a different method. It reads the excel file by streaming processing to avoid the problem of excessive memory consumption.
Streaming Excel Reader is based on the Apache Poi library, which uses the POI event model to analyze the Excel file.First, it creates an XSSFWORKBOOK object that represents the entire Excel document.Then, the sheet object can be obtained through the Workbook object, indicating the worksheet in Excel.Next, you can use the sheet object to traverse each line and obtain the value of each cell.
The following is an example code that uses Streaming Excel Reader to read the excel file:
import com.github.pjfanning.xlsx.StreamingReader;
import com.github.pjfanning.xlsx.impl.StreamingCell;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class ExcelReader {
public static void main(String[] args) throws IOException {
InputStream inputStream = new FileInputStream("path/to/excel/file.xlsx");
StreamingReader reader = StreamingReader.builder()
.Rowcachesize (1000) // Cache lines (the default value is 10)
.buffersize (4096) // The cache size (the default value is 1024)
.open(inputStream);
for (StreamingRow row : reader) {
for (StreamingCell cell : row.getCells()) {
String value = cell.getStringValue();
System.out.println(value);
}
}
reader.close();
inputStream.close();
}
}
In this code example, we first created an INPUTSTRAM object to read the Excel file.Then, we use the StreamingReader's constructor to create a StreamingReader instance, and specify some configuration parameters, such as line cache size and cache size.
Next, we traverse each line and each cell through the StreamingReader object and print the value of the cell.
Finally, we manually close the StreamingReader and InputStream manually after reading it.
By using Streaming Excel Reader, we can read Excel files efficiently without having to worry about memory overflow.This is a very practical and convenient Java class library that is suitable for handling large Excel files.