Skip to main content

Extra Information

This chapter introduces how to write extra information such as comments, hyperlinks, formulas, etc.

Comments​

Overview​

Add comments to specific cells through interceptors, suitable for annotations or special reminders.

Code Example​

Custom interceptor

@Slf4j
public class CommentWriteHandler implements RowWriteHandler {

@Override
public void afterRowDispose(RowWriteHandlerContext context) {
if (BooleanUtils.isTrue(context.getHead())) {
Sheet sheet = context.getWriteSheetHolder().getSheet();
Drawing<?> drawingPatriarch = sheet.createDrawingPatriarch();
// Create comment in first row, second column
Comment comment = drawingPatriarch.createCellComment(
new XSSFClientAnchor(0, 0, 0, 0, (short) 1, 0, (short) 2, 1));
comment.setString(new XSSFRichTextString("Comments"));
sheet.getRow(0).getCell(1).setCellComment(comment);
}
}
}

Usage

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

FesodSheet.write(fileName, DemoData.class)
.inMemory(Boolean.TRUE) // Comments must enable in-memory mode
.registerWriteHandler(new CommentWriteHandler())
.sheet()
.doWrite(data());
}

Result​

The comment is attached to B1 and is only shown when that cell is hovered.

ABC
1String TitleDate TitleCommentsNumber Title
2String02026-07-31 20:50:230.56
3String12026-07-31 20:50:230.56
⋮………
11String92026-07-31 20:50:230.56

Write extra hyperlink information

POJO Class​

@Getter
@Setter
@EqualsAndHashCode
public class WriteCellDemoData {
private WriteCellData<String> hyperlink;
}

Code Example​

@Test
public void writeHyperlinkDataWrite() {
String fileName = "writeCellDataWrite" + System.currentTimeMillis() + ".xlsx";
WriteCellDemoData data = new WriteCellDemoData();
// Set hyperlink
WriteCellData cellData = new WriteCellData<>("Click to visit");
HyperlinkData hyperlinkData = new HyperlinkData();
hyperlinkData.setAddress("https://example.com");
hyperlinkData.setHyperlinkType(HyperlinkData.HyperlinkType.URL);
cellData.setHyperlinkData(hyperlinkData);
data.setHyperlink(cellData);

FesodSheet.write(fileName, WriteCellDemoData.class)
.sheet()
.doWrite(Collections.singletonList(data));
}

Result​

A
1hyperlink
2Click to visit

Formulas​

Write extra formula information

POJO Class​

@Getter
@Setter
@EqualsAndHashCode
public class WriteCellDemoData {
private Integer num1;
private Integer num2;
private WriteCellData<String> formulaData;
}

Code Example​

@Test
public void writeFormulaDataWrite() {
String fileName = "writeCellDataWrite" + System.currentTimeMillis() + ".xlsx";
WriteCellDemoData data = new WriteCellDemoData();
data.setNum1(10);
data.setNum2(20);
// Set formula
WriteCellData<String> cellData = new WriteCellData<>();
FormulaData formulaData = new FormulaData();
formulaData.setFormulaValue("SUM(A2:B2)");
// Or
// formulaData.setFormulaValue("=SUM(A2:B2)");
cellData.setFormulaData(formulaData);
data.setFormulaData(cellData);

FesodSheet.write(fileName, WriteCellDemoData.class)
.sheet()
.doWrite(Collections.singletonList(data));
}

Result​

ABC
1num1num2formulaData
2102030

Template-based Writing​

Overview​

Supports using existing template files and filling data into templates, suitable for standardized output.

Code Example​

@Test
public void templateWrite() {
String templateFileName = "path/to/template.xlsx";
String fileName = "templateWrite" + System.currentTimeMillis() + ".xlsx";

FesodSheet.write(fileName, DemoData.class)
.withTemplate(templateFileName)
.sheet()
.doWrite(data());
}

Custom Interceptors​

Overview​

Implement custom logic (such as adding dropdowns) through interceptor operations.

Code Example​

Setting dropdowns

public class DropdownWriteHandler implements SheetWriteHandler {
@Override
public void afterSheetCreate(SheetWriteHandlerContext context) {
DataValidationHelper helper = context.getWriteSheetHolder().getSheet().getDataValidationHelper();
CellRangeAddressList range = new CellRangeAddressList(1, 10, 0, 0); // Dropdown area
DataValidationConstraint constraint = helper.createExplicitListConstraint(new String[] {"Option1", "Option2"});
DataValidation validation = helper.createValidation(constraint, range);
context.getWriteSheetHolder().getSheet().addValidationData(validation);
}
}

Usage

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

FesodSheet.write(fileName, DemoData.class)
.registerWriteHandler(new DropdownWriteHandler())
.sheet("Dropdown Example")
.doWrite(data());
}

Result​

The validation covers A2:A11, so every cell in that range offers the list. Selecting one shows the dropdown button and its options - drawn open here on A2.

ABC
1String TitleDate TitleNumber Title
2String0▾Option1Option22026-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