Skip to main content

POJO

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

Export Only Specified Columns Based on Parameters​

Overview​

Dynamically select columns to export by setting a collection of column names, supporting ignoring columns or exporting only specific columns.

The collection holds POJO field names, not header titles. Both examples below use the DemoData class and the data() method from Simple Writing, whose fields are string, date and doubleData.

Code Examples​

Ignore Specified Columns​

Everything except the listed fields is written:

@Test
public void excludeColumnWrite() {
String fileName = "excludeColumnFieldWrite" + System.currentTimeMillis() + ".xlsx";

Set<String> excludeColumns = Collections.singleton("date");
FesodSheet.write(fileName, DemoData.class)
.excludeColumnFieldNames(excludeColumns)
.sheet()
.doWrite(data());
}

Result

The date field is gone, the other two remain:

AB
1String TitleNumber Title
2String00.56
3String10.56
4String20.56
⋮……
11String90.56

Export Only Specified Columns​

Only the listed fields are written:

@Test
public void includeColumnWrite() {
String fileName = "includeColumnFieldWrite" + System.currentTimeMillis() + ".xlsx";

Set<String> includeColumns = Collections.singleton("date");
FesodSheet.write(fileName, DemoData.class)
.includeColumnFieldNames(includeColumns)
.sheet()
.doWrite(data());
}

Result

Only the date field is kept:

A
1Date Title
22026-07-31 20:50:23
32026-07-31 20:50:23
42026-07-31 20:50:23
⋮…
112026-07-31 20:50:23

Specify Column Order for Writing​

Overview​

Specify column order.

POJO Class​

@Getter
@Setter
@EqualsAndHashCode
public class IndexData {
@ExcelProperty(value = "String Title", index = 0)
private String string;
@ExcelProperty(value = "Date Title", index = 1)
private Date date;
@ExcelProperty(value = "Number Title", index = 3)
private Double doubleData;
}

Code Example​

index attribute​

Using the index attribute of the @ExcelProperty annotation.

@Test
public void indexWrite() {
String fileName = "indexWrite" + System.currentTimeMillis() + ".xlsx";
FesodSheet.write(fileName, IndexData.class)
.sheet()
.doWrite(data());
}

Result

ABCD
1String TitleDate TitleNumber Title
2String02026-07-31 20:50:230.56
3String12026-07-31 20:50:230.56
4String22026-07-31 20:50:230.56
⋮………
11String92026-07-31 20:50:230.56
note

Column C is empty on purpose. index is an absolute, 0-based column position, not a sort key: the fields declare 0, 1 and 3, so nothing is written at position 2 and the gap is preserved in the output. To place the three columns side by side, number them 0, 1, 2.

includeColumnFieldNames​

The columns come out in the POJO's order, not the collection's. Listing doubleData before string still writes string first, because that is the order the fields are declared in:

@Test
public void includeColumnOrderWrite() {
String fileName = "includeColumnFieldWrite" + System.currentTimeMillis() + ".xlsx";

Set<String> includeColumns = new LinkedHashSet<>(Arrays.asList("doubleData", "string"));
FesodSheet.write(fileName, DemoData.class)
.includeColumnFieldNames(includeColumns)
.sheet()
.doWrite(data());
}

Result

AB
1String TitleNumber Title
2String00.56
⋮……

orderByIncludeColumn​

Add .orderByIncludeColumn(true) to follow the collection's order instead:

@Test
public void orderByIncludeColumnWrite() {
String fileName = "includeColumnFieldWrite" + System.currentTimeMillis() + ".xlsx";

Set<String> includeColumns = new LinkedHashSet<>(Arrays.asList("doubleData", "string"));
FesodSheet.write(fileName, DemoData.class)
.includeColumnFieldNames(includeColumns)
.orderByIncludeColumn(true)
.sheet()
.doWrite(data());
}

Result

AB
1Number TitleString Title
20.56String0
⋮……

The collection needs a stable iteration order for this - a LinkedHashSet or a List, not a HashSet.


Writing Without Creating Objects​

Overview​

Write data directly using List<List<String>> to define headers and data without creating entity classes.

Code Example​

@Test
public void noModelWrite() {
String fileName = "noModelWrite" + System.currentTimeMillis() + ".xlsx";

FesodSheet.write(fileName)
.head(head()) // Dynamic headers
.sheet("Write Without Object")
.doWrite(dataList());
}

private List<List<String>> head() {
return Arrays.asList(
Collections.singletonList("String Title"),
Collections.singletonList("Number Title"),
Collections.singletonList("Date Title"));
}

private List<List<Object>> dataList() {
List<List<Object>> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(Arrays.asList("String" + i, 0.56, new Date()));
}
return list;
}

Result​

ABC
1String TitleNumber TitleDate Title
2String00.562026-07-31 20:50:23
3String10.562026-07-31 20:50:23
4String20.562026-07-31 20:50:23
⋮………
11String90.562026-07-31 20:50:23