Skip to main content

Simple Reading

This chapter introduces how to use this project to perform simple spreadsheet reading operations.

Data Listeners​

Overview​

Fesod provides a listener mechanism for processing each row of data while reading spreadsheet files.

Usage​

Data listeners need to be instantiated and support various usage patterns.

Instantiation​

Listeners cannot be managed by Spring and must be re-instantiated each time a spreadsheet file is read.

Lambda Expressions​

@Test
public void simpleRead() {
String fileName = "path/to/demo.xlsx";

FesodSheet.read(fileName, DemoData.class, new PageReadListener<>(dataList -> {
for (DemoData demoData : dataList) {
log.info("Read one record: {}", JSON.toJSONString(demoData));
}
})).sheet().doRead();
}

Anonymous Inner Classes​

@Test
public void simpleRead() {
String fileName = "path/to/demo.xlsx";

FesodSheet.read(fileName, DemoData.class, new ReadListener<DemoData>() {
@Override
public void invoke(DemoData data, AnalysisContext context) {
log.info("Read one record: {}", JSON.toJSONString(data));
}

@Override
public void doAfterAllAnalysed(AnalysisContext context) {
log.info("All data reading completed!");
}
}).sheet().doRead();
}

Data Listeners​

@Test
public void simpleRead() {
String fileName = "path/to/demo.xlsx";

FesodSheet.read(fileName, DemoData.class, new DemoDataListener())
.sheet()
.doRead();
}

POJO Classes and Listeners​

Overview​

Fesod provides a simple way to read spreadsheet files. Users only need to define a POJO class to represent the data structure, then read data through Fesod's listener mechanism.

POJO Class​

The DemoData POJO class corresponding to the spreadsheet structure:

@Getter
@Setter
@EqualsAndHashCode
public class DemoData {
private String string;
private Date date;
private Double doubleData;
}

Data Listener​

DemoDataListener is a custom listener used to process data read from spreadsheet.

@Slf4j
public class DemoDataListener implements ReadListener<DemoData> {

@Override
public void invoke(DemoData data, AnalysisContext context) {
log.info("Read one record: {}", JSON.toJSONString(data));
}

@Override
public void doAfterAllAnalysed(AnalysisContext context) {
log.info("All data reading completed!");
}
}

Code Example​

@Test
public void simpleRead() {
String fileName = "path/to/demo.xlsx";

FesodSheet.read(fileName, DemoData.class, new DemoDataListener())
.sheet()
.doRead();
}

Without POJO Classes and Listeners​

Overview​

Fesod supports reading spreadsheet files directly without defining POJO classes, using Map<Integer, String> to read data directly, where the key is the column index and the value is the cell data.

Data Listener​

@Slf4j
public class NoModelDataListener extends AnalysisEventListener<Map<Integer, String>> {

@Override
public void invoke(Map<Integer, String> data, AnalysisContext context) {
log.info("Read one record: {}", JSON.toJSONString(data));
}

@Override
public void doAfterAllAnalysed(AnalysisContext context) {
log.info("All data reading completed!");
}
}

Synchronous Reading​

Overview​

Use the doReadSync method to directly read spreadsheet data into a list in memory. This method is recommended for scenarios with small data volumes. The read data can be either a list of POJO objects or a list of Maps.

POJO Class​

The DemoData POJO class corresponding to the spreadsheet structure:

@Getter
@Setter
@EqualsAndHashCode
public class DemoData {
private String string;
private Date date;
private Double doubleData;
}

Code Examples​

Reading as POJO Object List​

@Test
public void synchronousReadToObjectList() {
String fileName = "path/to/demo.xlsx";

// POJO Object List
List<DemoData> list = FesodSheet.read(fileName)
.head(DemoData.class)
.sheet()
.doReadSync();

for (DemoData data : list) {
log.info("Read data: {}", JSON.toJSONString(data));
}
}

Reading as Map List​

When not using POJOs, each row can be read as a Map, where the key is the column index and the value is the cell content.

@Test
public void synchronousReadToMapList() {
String fileName = "path/to/demo.xlsx";

// Map List
List<Map<Integer, String>> list = FesodSheet.read(fileName)
.sheet()
.doReadSync();

for (Map<Integer, String> data : list) {
log.info("Read data: {}", JSON.toJSONString(data));
}
}

Reading Specific Columns (Column Filtering)​

By default, Fesod reads all columns from a sheet. If you only want to process specific columns, you can use .includeColumnIndexes(List<Integer>). This selects specific columns out of the entire sheet column set

Reading Specific Columns to Map List (Synchronous)​

When reading without a POJO template, the filtered columns are packed sequentially into the resulting Map<Integer, String>. The first targeted column is mapped to key 0, the second to key 1, and so on.

@Test
public void readSpecificColumnsToMapList() {
String fileName = "path/to/demo.xlsx";

// We only want to read Column A (Index 0) and Column C (Index 2)
List<Integer> targetColumns = Arrays.asList(0, 2);

List<Map<Integer, String>> list = FesodSheet.read(fileName)
.sheet()
.includeColumnIndexes(targetColumns)
.doReadSync();

for (Map<Integer, String> data : list) {
// Column Index 0 is mapped to key 0
String id = data.get(0);
// Column Index 2 is mapped sequentially to key 1
String age = data.get(1);

log.info("ID: {}, Age: {}", id, age);
}
}