Use the Light Excel Reader framework in the Java class library for screening and sorting Excel data
Use the Light Excel Reader framework in the Java class library for screening and sorting Excel data
Overview:
Excel is a widely used electronic table software for data analysis, database management and report generation.In Java development, we often need to read and process Excel data.Light Excel Reader is a lightweight Java class library that provides a simple and efficient method for screening and sorting Excel data.This article will introduce how to use the Light Excel Reader framework in Java to achieve the screening and sorting of Excel data.
Step 1: Import Light Excel Reader framework
First, we need to import the Light Excel Reader framework in the Java project.You can download and import related JAR bags from the official website.
Step 2: Read Excel data
Using Light Excel Reader framework, we can read Excel data through the following code:
// Import related classes
import com.github.liaochong.myexcel.core.DefaultExcelReader;
import com.github.liaochong.myexcel.core.ExcelReader;
// Read Excel data
public void readExcelData(String filePath) {
// Create ExcelReader objects
ExcelReader excelReader = new DefaultExcelReader();
// Read the excel file
List<List<Object>> rows = excelReader.read(filePath).getAll();
// Traversing Excel data
for (List<Object> row : rows) {
// Process each line of data
for (Object cell : row) {
// Process each cell data
System.out.println(cell.toString());
}
}
}
Step 3: Screen Excel data
Using Light Excel Reader framework, we can use the filtering conditions to screen Excel data.The following is a sample code that is used to screen data that meets conditions:
// Import related classes
import com.github.liaochong.myexcel.core.unique.SaxAnalyser;
// Screen Excel data
public List<List<Object>> filterExcelData(String filePath, String column, String value) {
// Create saxanalyser object
SaxAnalyser saxAnalyser = SaxAnalyser.createSaxAnalyser();
// Read the excel file
ExcelAnalyseData excelAnalyseData = saxAnalyser.read(filePath);
// Screening data
SheetanalySedata SheetanalySedata = ExcelaanalySedata.getSheetanalySedatalist (). Get (0); // Read the first worksheet
List<RowAnalyseData> rowAnalyseDataList = sheetAnalyseData.getRowAnalyseDataList();
List<List<Object>> filteredData = new ArrayList<>();
for (RowAnalyseData rowAnalyseData : rowAnalyseDataList) {
// Get the specified column index
int columnIndex = rowAnalyseData.getColumnIndex(column);
// Get the value of the specified column
Object cellValue = rowAnalyseData.getCellVal(columnIndex);
// Whether the judgment value matches
if (cellValue != null && cellValue.toString().equals(value)) {
// Add row data that meets conditions
filteredData.add(rowAnalyseData.getCells());
}
}
return filteredData;
}
Step 4: Sorting Excel data
Using Light Excel Reader framework, we can use sorting rules to sort Excel data.The following is an example code that is used to sort the data according to the specified column:
// Import related classes
import com.github.liaochong.myexcel.core.ExcelBuilder;
import com.github.liaochong.myexcel.core.RowExcelBuilder;
// Sorting Excel data
public void sortExcelData(String filePath, String column) {
// Create an excelbuilder object
ExcelBuilder excelBuilder = RowExcelBuilder.fromDefault();
// Read the excel file
List<List<Object>> rows = excelBuilder.read(filePath).getAll();
// Get the specified column index
int columnIndex = 0;
for (Object cell : rows.get(0)) {
// Find the specified column
if (cell.toString().equals(column)) {
columnIndex = rows.get(0).indexOf(cell);
break;
}
}
// Sort according to the value of the specified column
Collections.sort(rows.subList(1, rows.size()), (row1, row2) -> {
String cell1Value = row1.get(columnIndex).toString();
String cell2Value = row2.get(columnIndex).toString();
return cell1Value.compareTo(cell2Value);
});
// Excel data after output sorting
excelBuilder.append(rows).write("sorted_data.xlsx");
}
Summarize:
Light Excel Reader is an easy -to -use Java class library for reading and processing Excel data.This article introduces how to use the Light Excel Reader framework in Java to achieve the screening and sorting of Excel data.By using this framework, a large amount of Excel data can be processed effectively to improve development efficiency.I hope this article will help you understand and use Light Excel Reader.