Skip to main content

POJO

This chapter introduces how to read data by configuring POJO classes.

Reading by Column Name or Column Index​

Overview​

You can read spreadsheet data by specifying column names or column indexes. This makes interaction with dynamically generated spreadsheet files more flexible.

Example Code​

POJO Class​

@Getter
@Setter
@EqualsAndHashCode
public class IndexOrNameData {
@ExcelProperty(index = 2)
private Double doubleData;

@ExcelProperty("String Title")
private String string;

@ExcelProperty("Date Title")
private Date date;
}

Code Example​

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

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

Note when using includeColumnIndexes:
If includeColumnIndexes is configured on the reader (e.g., selecting original columns [0, 2, 4]), the parsed columns are remapped to contiguous indices (0, 1, 2). Ensure @ExcelProperty(index = ...) matches the remapped index rather than the original file column index.


Cell Objects​

Overview​

Use the CellData type to receive cell data to support formulas and various cell formats.

POJO Class​

@Getter
@Setter
@EqualsAndHashCode
public class CellDataReadDemoData {
private CellData<String> string;
private CellData<Date> date;
private CellData<Double> doubleData;
private CellData<String> formulaValue;
}

Code Example​

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

FesodSheet.read(fileName, CellDataReadDemoData.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.

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!");
}
}