EasyExcel framework and use tutorial
EasyExcel is an open source Java framework that is used to simplify the Excel document reading and writing operation.It provides a convenient way to operate the Excel file that can read, write, and convert Excel files, while supporting a large number of configuration options and custom operations.
1. Introduction
The Easianexcel framework is based on Alibaba's open source project Rexxar, which realizes the reading and writing of Excel documents through POI.Compared with the use of POI for Excel operation, EasyExcel provides a simpler API interface, making Excel read and write operations easier and efficient.
2. Features and advantages
1. Convenient and easy to use: EasyExcel can simplify the API interface, so that the Excel reading and writing operation is no longer cumbersome, and you can get started quickly.
2. High performance: EasyExcel uses a POI -based streaming and writing method, which greatly improves the performance and efficiency of Excel read and write.
3. Big data support: When the EasyExcel design is designed, the amount of big data can be easily processed by millions of lines or even more data.
4. Support multiple data formats: EasyExcel supports a variety of common Excel data formats, including numbers, dates, texts, etc., which can accurately read and write various data types.
5. Powerful data conversion function: EasyExcel provides a flexible data conversion function, which can easily convert data in different formats.
6. Strong configuration: EasyExcel provides a large number of configuration options, which can be flexibly configured according to specific needs to meet various complex Excel operation requirements.
Third, use tutorial
Below is a simple example of using EasyExcel to read and write excel files:
1. Add dependencies
Add EasyExcel Maven dependencies to the pom.xml file of the project:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.0.0-beta2</version>
</dependency>
2. Read the excel file
public class ExcelReader {
public static void main(String[] args) {
String fileName = "path/to/excel.xlsx";
// Create ExcelReader objects
ExcelReader excelReader = EasyExcel.read(fileName).build();
// Read the first sheet
ReadSheet readSheet = EasyExcel.readSheet(0).build();
// Register the listener, read Excel data
excelReader.registerReadListener(new DemoDataListener());
// Start reading
excelReader.read(readSheet);
// Close the resource
excelReader.finish();
}
}
3. Write data reading and obtaining listening device
public class DemoDataListener extends AnalysisEventListener<DemoData> {
@Override
public void invoke(DemoData data, AnalysisContext context) {
// Process the data you read
System.out.println ("Read the data:" + data);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
// Treatment after reading is completed
}
}
4. Write into Excel file
public class ExcelWriter {
public static void main(String[] args) {
String fileName = "path/to/excel.xlsx";
// Write into the excel file
EasyExcel.write(fileName, DemoData.class).sheet("Sheet1").doWrite(data());
}
private static List<DemoData> data() {
// Construction data
List<DemoData> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
DemoData data = new DemoData();
data.setId(i + 1);
data.setName("Name " + (i + 1));
list.add(data);
}
return list;
}
}
5. Writing data class
@Data
public class DemoData {
private Integer id;
private String name;
}
The above example demonstrates the basic operation of reading and writing using EasyExcel.Through the EasyExcel framework, we can easily handle Excel files to achieve various complex Excel operation requirements.