Use the EASYEXCEL framework to realize the excel file read and write
Use the EASYEXCEL framework to implement the reading and writing of the excel file
EasyExcel is a simple and easy -to -use Excel operating tool written by Java language.It provides the function of reading, writing, and processing of Excel files, which can greatly simplify the process of Excel file processing and support the processing of large data volume.
Before using the EasyExcel framework, you need to add EasyExcel dependencies to the project.You can add the following configuration to the pom.xml file:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.1</version>
</dependency>
## Excel file read
First, create a Java class to read the excel file.You can use the `EasyExcel.read () method in EasyExcel to specify the files and data processors to be read, as shown below:
import com.alibaba.excel.EasyExcel;
public class ExcelReader {
public static void main(String[] args) {
String filename = "path/to/excel/file.xlsx"; // Specify the Excel file path to be read
// Define the data processor to process the read -read data
DataListener<ExcelData> dataListener = new ExcelDataListener();
// Start reading Excel file, call the easyExcel.read () method, and specify the file and data processor you want to read
EasyExcel.read(fileName, ExcelData.class, dataListener).sheet().doRead();
}
// Data processor, used to process the read data
public static class ExcelDataListener extends AnalysisEventListener<ExcelData> {
@Override
public void invoke(ExcelData data, AnalysisContext context) {
// Process the data you read
System.out.println(data);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
// The processing operation after reading
}
}
// Define the Excel data entity class
public static class ExcelData {
// Define the corresponding attributes in the column in excel
private String name;
private int age;
// Add the corresponding getter and setter method
}
}
In the above code, first specify the path of the Excel file to be read, and then create a data processor `ExcelDataListener` to process the read data.The data reading each line can be processed in the `Invoke () method in the` ExcelDataListener`.`EasyExcel.read ()` Method specifies the excel files, data entity class and data processors to be read, call `.sheet (). DOREAD ()` Method to start reading the excel file.
It should be noted that reading the excel file needs to define a data entity class (`ExcelData) to store each line of data in Excel, and you need to add the corresponding Getter and Setter method to this class.
## Excel file writing
Next, create a Java class to write to the Excel file.You can use the method and data you want to write with `EasyExcel.write ()` in EasyExcel, as shown below:
import com.alibaba.excel.EasyExcel;
import java.util.ArrayList;
import java.util.List;
public class ExcelWriter {
public static void main(String[] args) {
String filename = "path/to/excel/file.xlsx"; // Specify the Excel file path to be written
// Define the data to be written, use the list collection to store data
List<ExcelData> dataList = new ArrayList<>();
datalist.add (New Exceldata ("Zhang San", 20);
DataList.add (New Exceldata ("Li Si", 25);
datalist.add (New Exceldata ("Wang Wu", 30));
// Start writing to the excel file, call the EasyExcel.write () method, and specify the file and data entity class you want to write
EasyExcel.write(fileName, ExcelData.class).sheet("Sheet1").doWrite(dataList);
}
// Define the Excel data entity class
public static class ExcelData {
// Define the corresponding attributes in the column in excel
private String name;
private int age;
// Add constructor and Getter and Setter method
}
}
In the above code, first specify the path of the Excel file to be written, and then create a list set (`datalist`) that stored data, and add the data to be written to the collection.`EasyExcel.write ()` The method specifies the excel file and data entity class to be written, call the `.sheet (" sheet1 "). Dowrite (dataList)` Method Start writing the excel file, where `sheet1" `means indicatingWrite the name of the worksheet.
Similarly, writing an excel file also needs to define a data entity class (`ExcelData) to store data to be written, and you need to add the corresponding constructor and getter and setter method to this class.
Through the EasyExcel framework, we can easily perform the reading and writing operation of the excel file, which greatly simplifies the process processing process of Excel file and improves development efficiency.