Use Light Excel Reader framework to analyze the basic operation of Excel file
Use Light Excel Reader framework to analyze the basic operation of Excel file
Light Excel Reader is a Java library that can be used to analyze and read Excel files.It provides a simple and flexible way to extract data in the Excel file and can process various common Excel data types.The following will introduce how to use the Light Excel Reader framework for basic operations of Excel files.
First of all, we need to add Light Excel Reader to rely on the Java project.Add the following code to the pom.xml file:
<dependency>
<groupId>com.github.liaochong</groupId>
<artifactId>excel</artifactId>
<version>3.0.3</version>
</dependency>
Next, we need to create a class used to analyze the Excel file.First of all, the need for introduction:
import com.github.liaochong.myexcel.core.DefaultExcelBuilder;
import com.github.liaochong.myexcel.core.ListBuilder;
import com.github.liaochong.myexcel.core.WorkbookType;
import com.github.liaochong.myexcel.utils.FileExportUtil;
Then, we can use the following code to parse the Excel file:
String excelFilePath = "/path/to/excel/file.xlsx";
List<List<Object>> dataList = DefaultExcelBuilder.of(ListBuilder.of(Object.class).sheetIndex(0).build())
.excelType(WorkbookType.XLSX)
.build()
.read(excelFilePath);
for (List<Object> row : dataList) {
for (Object cellData : row) {
System.out.print(cellData + "\t");
}
System.out.println();
}
In the above code, we first specified the path of the Excel file to be parsed, and then used the `defaultExcelbuilder` to create an Excel parser.When creating the `defaultExcelbuilder`, we used the` ListBuilder` to specify the data type to be read to `Object.class`, and specify the index of the sheet page to be read through the` sheetindex` method (start from 0).Then, we can set the type of Excel file by calling the `ExcellType` method, and calling the` Build` method to create a parser.Finally, we read the excel file with the `Read` method and return the resolution results into a list containing multiple` list <object> `.
Finally, we can traverse the analysis results and print the data of each cell.
In addition to reading data, Light Excel Reader also provides other functions, such as exporting Excel files, setting unit grid styles, and so on.You can further understand and use the framework according to the specific needs of Light Excel Reader.
In summary, using Light Excel Reader framework to analyze the Excel file is a simple and powerful way.By using Light Excel Reader, you can easily read and process the data in the Excel file to help you implement the basic operations of various excel files in the Java project.