Skip to content

Fix HSSFSheet.ShiftRows: PhysicalNumberOfRows not decreasing when shifting rows up#1707

Merged
tonyqus merged 3 commits intomasterfrom
copilot/fix-last-rownum-decrease
Mar 1, 2026
Merged

Fix HSSFSheet.ShiftRows: PhysicalNumberOfRows not decreasing when shifting rows up#1707
tonyqus merged 3 commits intomasterfrom
copilot/fix-last-rownum-decrease

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 1, 2026

HSSFSheet.ShiftRows with negative n correctly decremented LastRowNum but left vacated row objects in the internal rows dictionary, so PhysicalNumberOfRows stayed stale. XSSF handled this correctly; HSSF did not.

sheet.ShiftRows(3, sheet.LastRowNum, -1);
// Before: LastRowNum=3, PhysicalNumberOfRows=5  ← bug
// After:  LastRowNum=3, PhysicalNumberOfRows=4  ← correct

Changes

  • HSSFSheet.ShiftRows — After the shift loop, call RemoveRow() on each vacated position in the range [Math.Max(startRow, endRow + n + 1), endRow]. These rows had their cells moved out but persisted as empty objects in the rows dictionary, inflating PhysicalNumberOfRows.

  • BaseTestSheetShiftRows — Added TestShiftRowsUpPhysicalRowCount asserting both LastRowNum and PhysicalNumberOfRows decrease after an upward shift. Runs for HSSF and XSSF.

  • TestBugs.Test50416LastRowNumber — Updated assertions that previously expected vacated rows to survive as empty objects (GetRow(n).LastCellNum == -1). With the fix, those positions are null; updated to IsNull and removed the now-unreachable RemoveRow call.

Original prompt

This section details on the original issue you should resolve

<issue_title>When moving a row up in HSSFWorkbook, the LastRowNum decreases, but the physical rows do not decrease.</issue_title>
<issue_description>### NPOI Version
2.7.1

File Type

  • XLS

Upload the Excel File

input.xls

Reproduce Steps

string inputFile = "input.xls";
IWorkbook workbook;

using (FileStream file = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    workbook = new HSSFWorkbook(file);
}


ISheet sheet = workbook.GetSheetAt(0);

Console.WriteLine(sheet.LastRowNum);
Console.WriteLine(sheet.PhysicalNumberOfRows);

sheet.ShiftRows(3, sheet.LastRowNum, -1);

Console.WriteLine(sheet.LastRowNum);
Console.WriteLine(sheet.PhysicalNumberOfRows);

string outputFile = "output.xls";
using (FileStream file = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
{
    workbook.Write(file);
}

Issue Description

As shown in the code above, after I moved all rows below the third row up by one, the sheet.LastRowNum decreased by 1, but the sheet.PhysicalNumberOfRows remained unchanged.
Unlike XSSFWorkbook, where both sheet.LastRowNum and sheet.PhysicalNumberOfRows decrease by 1.

image
</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…icalNumberOfRows is correct

Co-authored-by: tonyqus <772561+tonyqus@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix last row number decrease issue in HSSFWorkbook Fix HSSFSheet.ShiftRows: PhysicalNumberOfRows not decreasing when shifting rows up Mar 1, 2026
@tonyqus tonyqus added this to the NPOI 2.8.0 milestone Mar 1, 2026
…ted rows after ShiftRows

Co-authored-by: tonyqus <772561+tonyqus@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When moving a row up in HSSFWorkbook, the LastRowNum decreases, but the physical rows do not decrease.

2 participants