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:
| A | B | |
| 1 | String Title | Number Title |
| 2 | String0 | 0.56 |
| 3 | String1 | 0.56 |
| 4 | String2 | 0.56 |
| ⋮ | … | … |
| 11 | String9 | 0.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 | |
| 1 | Date Title |
| 2 | 2026-07-31 20:50:23 |
| 3 | 2026-07-31 20:50:23 |
| 4 | 2026-07-31 20:50:23 |
| ⋮ | … |
| 11 | 2026-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
| A | B | C | D | |
| 1 | String Title | Date Title | Number Title | |
| 2 | String0 | 2026-07-31 20:50:23 | 0.56 | |
| 3 | String1 | 2026-07-31 20:50:23 | 0.56 | |
| 4 | String2 | 2026-07-31 20:50:23 | 0.56 | |
| ⋮ | … | … | … | |
| 11 | String9 | 2026-07-31 20:50:23 | 0.56 |
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
| A | B | |
| 1 | String Title | Number Title |
| 2 | String0 | 0.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
| A | B | |
| 1 | Number Title | String Title |
| 2 | 0.56 | String0 |
| ⋮ | … | … |
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
| A | B | C | |
| 1 | String Title | Number Title | Date Title |
| 2 | String0 | 0.56 | 2026-07-31 20:50:23 |
| 3 | String1 | 0.56 | 2026-07-31 20:50:23 |
| 4 | String2 | 0.56 | 2026-07-31 20:50:23 |
| ⋮ | … | … | … |
| 11 | String9 | 0.56 | 2026-07-31 20:50:23 |