To make the provided ExcelModelListener class parse Excel files and implement the function of batch storing databases, it is necessary to combine the EasyExcel library to read Excel data. Specifically, you can use the () method to read an Excel file and specify an ExcelModelListener as the event listener.
Here is the complete sample code for calling ExcelModelListener for Excel file parsing:
1. First, make sure that the EasyExcel dependency has been added
If you have not introduced EasyExcel in your Maven project, you can add the following dependencies to the file:
<dependency> <groupId></groupId> <artifactId>easyexcel</artifactId> <version>3.0.5</version> <!-- Make sure to use the latest version --> </dependency>
2. Code that calls ExcelModelListener
Suppose you have an Excel file and ExcelMode is the Java class corresponding to Excel data, you can call the listener to parse the Excel file:
ExcelMode class example (simplified version)
package ; public class ExcelMode { private String field1; private String field2; // Here are the corresponding fields for each column in your Excel // Omit getter and setter}
Use ExcelModelListener to parse Excel files
package ; import ; import ; import ; import ; public class ExcelImportService { public static void main(String[] args) { String fileName = "path/to/your/excel/"; // Excel file path // Call EasyExcel to read the file (fileName, , new ExcelModelListener()) .sheet() .doRead(); } }
3. Code parsing
(fileName, , new ExcelModelListener()):
This line of code calls the EasyExcel read method, specifies the path fileName of the Excel file, and the Java class ExcelMode used to parse Excel data.
ExcelModelListener is passed to EasyExcel as an event listener, so that when EasyExcel reads each row of data, it calls the invoke() method in ExcelModelListener.
.sheet():
This method specifies reading a sheet in an Excel file. If the Excel file has multiple sheets, you can specify other sheets, such as .sheet(1) or .sheet("sheetName").
.doRead():
This method triggers the actual Excel data read operation.
4. ExcelModelListener's workflow
Read data line by line:
The invoke() method of ExcelModelListener is called when each row of data is read. You can process each row of data in this method, in an example storing the data into a list.
Batch processing:
Whenever the read data reaches the set batch size (such as BATCH_COUNT = 5), the saveData() method is called to store the data in the database, and then the list is cleared.
After all data parsing is completed:
doAfterAllAnalysed() will be called after the parsing of the Excel file. It is usually used to perform some final operations, such as storing the last batch of data or releasing resources.
5. Complete code example
Assuming there are two columns of data in an Excel file (such as field1 and field2), the following is a complete code example:
package ; public class ExcelMode { private String field1; private String field2; // Getter and Setter methods public String getField1() { return field1; } public void setField1(String field1) { this.field1 = field1; } public String getField2() { return field2; } public void setField2(String field2) { this.field2 = field2; } @Override public String toString() { return "ExcelMode{" + "field1='" + field1 + '\'' + ", field2='" + field2 + '\'' + '}'; } }
(Main Class)
package ; import ; import ; import ; import ; import ; public class ExcelModelListener extends AnalysisEventListener<ExcelMode> { private static final int BATCH_COUNT = 5; List<ExcelMode> list = new ArrayList<ExcelMode>(); private static int count = 1; @Override public void invoke(ExcelMode data, AnalysisContext context) { ("Parsed to a data: { " + () + " }"); (data); count++; if (() >= BATCH_COUNT) { saveData(count); (); } } @Override public void doAfterAllAnalysed(AnalysisContext context) { saveData(count); ("All data parsing is completed!"); ("count :" + count); } private void saveData(int count) { ("{ " + count + " }Data,Start storing the database!" + ()); // Here you can store the data in the list into the database ("Storing the database successfully!"); } }
(Call and execute)
package ; import ; import ; import ; public class ExcelImportService { public static void main(String[] args) { String fileName = "path/to/your/excel/"; // Excel file path // Call EasyExcel to read the file (fileName, , new ExcelModelListener()) .sheet() .doRead(); } }
Summarize
You read the Excel file through () and specify the ExcelModelListener as the event listener.
ExcelModelListener processes each row of data and performs batch storage when the set batch size is reached.
After the data parsing is completed, you can do some finalization operations through doAfterAllAnalysed().
This process is very suitable for processing Excel files with large amounts of data. It can ensure efficient memory while also performing batch data persistence operations.
This is the end of this article about Java using easyExcel to implement Excel file parsing. For more related Java easyExcel parsing content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!