Streaming Excel Reader framework introduction and usage guideline in the Java class library
Streaming Excel Reader framework introduction and usage guideline in the Java class library
Overview:
Streaming Excel Reader is a very useful tool in the Java class library. It allows developers to read and process Excel files in a streamlined manner.Compared with the traditional Excel reading method, the Stream Excel Reader can handle large Excel files more efficiently while reducing memory occupation.
Instructions:
The following will introduce how to use the Streaming Excel Reader framework to read and process the Excel file.
Step 1: Introduce dependencies
First, you need to introduce the dependence of Streaming Excel Reader in your project.You can achieve the following dependencies by adding the following dependencies in the construction of your project (such as Maven or Gradle):
Maven:
<dependency>
<groupId>com.monitorjbl</groupId>
<artifactId>excel-streaming-reader</artifactId>
<version>2.2.0</version>
</dependency>
Gradle:
groovy
implementation 'com.monitorjbl:excel-streaming-reader:2.2.0'
Step 2: Read the excel file
Once you introduce the dependencies of Streaming Excel Reader, you can use it to read the Excel file.The following is a simple example. It shows how to use Streaming Excel Reader to read all the lines in the Excel file:
import com.monitorjbl.xlsx.StreamingReader;
import com.monitorjbl.xlsx.impl.StreamingSheet;
import com.monitorjbl.xlsx.impl.StreamingWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class ExcelReaderExample {
public static void main(String[] args) {
File excelFile = new File("path/to/excel/file.xlsx");
try (InputStream is = new FileInputStream(excelFile);
StreamingWorkbook workbook = StreamingReader.builder()
.Rowcachesize (100) // cache size
.buffersize (4096) // Cushion size
.open(is)) {
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
StreamingSheet sheet = (StreamingSheet) workbook.getSheetAt(i);
sheet.forEach(row -> {
row.forEach(cell -> {
// Process data of each cell
System.out.print(cell.getStringCellValue() + "\t");
});
System.out.println();
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the above example, we first create a file object to indicate the Excel file to read, and then use the `FileInputStream` to convert the file to` InputStream`.Next, we use Streaming Excel Reader's `StreamingReader` Constructioner to open the input stream and generate an object of` Streamingworkbook.We can then process Excel data by iteration by iterating each sheet and each ROW.
Step 3: process Excel data
Once we can access lines and cells, we can process Excel data according to business needs.For example, we can store the reading data into the database, perform some calculations, or generate reports.In the above example, we simply print out the values of each cell, but in fact you can handle it accordingly according to your needs.
Summarize:
The Streaming Excel Reader framework provides an efficient, low memory occupation method to read and handle large Excel files.By following the above steps, developers can easily integrate the framework into their Java projects to achieve rapid reading and processing of Excel data.I hope this article will help you understand the use of the Streaming Excel Reader framework!