Skip to content

Comments

fix: fix error in using relativeHeadRowIndex for writing tables only#810

Open
32154678925 wants to merge 3 commits intoapache:mainfrom
32154678925:fix/fix-relativeHeadRowIndex-invalid-#794
Open

fix: fix error in using relativeHeadRowIndex for writing tables only#810
32154678925 wants to merge 3 commits intoapache:mainfrom
32154678925:fix/fix-relativeHeadRowIndex-invalid-#794

Conversation

@32154678925
Copy link

@32154678925 32154678925 commented Jan 18, 2026

Purpose of the pull request

Fixes(#794)
When you do not write directly in the sheet, but only write data in the table and set the head, the offset value set with relativeHeadRowIndex() will make an error, and the blank line with the specified offset will be empty again between the head and the data. If the head is not set, the offset value set with relativeHeadRowIndex() will not take effect

What's changed?

Change the judgment of whether to add offset in the add method in the ExcelWriteAddExecutor class from writeSheetHolder to currentWriteHolder

Before:

WriteSheetHolder writeSheetHolder = writeContext.writeSheetHolder();
int newRowIndex = writeSheetHolder.getNewRowIndexAndStartDoWrite();
if (writeSheetHolder.isNew()
        && !writeSheetHolder.getExcelWriteHeadProperty().hasHead()) {
    newRowIndex += writeContext.currentWriteHolder().relativeHeadRowIndex();
}

After:

WriteSheetHolder writeSheetHolder = writeContext.writeSheetHolder();
WriteHolder currentWriteHolder = writeContext.currentWriteHolder();
int newRowIndex = writeSheetHolder.getNewRowIndexAndStartDoWrite();
if (currentWriteHolder.isNew()) {
    if(currentWriteHolder instanceof AbstractWriteHolder){
        AbstractWriteHolder writeHolder = (AbstractWriteHolder) currentWriteHolder;
        if(!writeHolder.getExcelWriteHeadProperty().hasHead()){
            newRowIndex += currentWriteHolder.relativeHeadRowIndex();
        }
    }
}

Checklist

  • I have read the Contributor Guide.
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request fixes a bug where relativeHeadRowIndex() was being applied twice when writing data to tables with headers, causing extra blank rows to appear between the header and data. The fix changes the condition from checking whether the sheet is new to checking whether the current write holder (which could be a table) is new and has no head.

Changes:

  • Modified logic in ExcelWriteAddExecutor.add() to check currentWriteHolder instead of writeSheetHolder for applying the row index offset
  • Added import for AbstractWriteHolder class
  • Added instanceof check before casting to AbstractWriteHolder (unnecessary based on codebase architecture)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +70 to +74
if (currentWriteHolder instanceof AbstractWriteHolder) {
AbstractWriteHolder writeHolder = (AbstractWriteHolder) currentWriteHolder;
if (!writeHolder.getExcelWriteHeadProperty().hasHead()) {
newRowIndex += currentWriteHolder.relativeHeadRowIndex();
}
Copy link

Copilot AI Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The instanceof check for AbstractWriteHolder is unnecessary. All implementations of WriteHolder (WriteSheetHolder, WriteTableHolder, WriteWorkbookHolder) extend AbstractWriteHolder. The rest of the codebase directly casts currentWriteHolder() to AbstractWriteHolder without instanceof checks (see WriteHandlerUtils.java lines 79, 124, 160, 168, 176, 184). For consistency with the existing codebase patterns, the cast should be done directly without the instanceof check.

Suggested change
if (currentWriteHolder instanceof AbstractWriteHolder) {
AbstractWriteHolder writeHolder = (AbstractWriteHolder) currentWriteHolder;
if (!writeHolder.getExcelWriteHeadProperty().hasHead()) {
newRowIndex += currentWriteHolder.relativeHeadRowIndex();
}
AbstractWriteHolder writeHolder = (AbstractWriteHolder) currentWriteHolder;
if (!writeHolder.getExcelWriteHeadProperty().hasHead()) {
newRowIndex += currentWriteHolder.relativeHeadRowIndex();

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants