JanKallman / EPPlus

Create advanced Excel spreadsheets using .NET
3.76k stars 1.18k forks source link

InsertRow copies wrong styles #288

Open ZTHawk opened 6 years ago

ZTHawk commented 6 years ago

When using ExcelWorksheet -> InsertRow with a referenced row from which the styles should be copied than styles are wrong if referenced row (copyStylesFromRow) is higher or equal to the target insert position (rowFrom).

Load a sheet that has two rows (eg: row 1 + 2) with styles. Now insert a row between them and copy styles from row 2.

int targetRow = 2;
int numRowsToInsert = 1;
int refRow = 2;
sh.InsertRow(targetRow, numRowsToInsert, refRow);

The code above will insert a row but the styles are not copied from "refRow" but from itself. Using the following code will result in correct styles:

int targetRow = 2;
int numRowsToInsert = 1;
int refRow = 2;
sh.InsertRow(targetRow, numRowsToInsert, refRow + numRowsToInsert);

The issues lies in ExcelWorksheet.cs -> public void InsertRow(int rowFrom, int rows, int copyStylesFromRow) line 1925

var cseS = new CellsStoreEnumerator<ExcelCoreValue>(_values, copyStylesFromRow, 0, copyStylesFromRow, ExcelPackage.MaxColumns); //Fixes issue 15068 , 15090

It should be

if ( copyStylesFromRow >= rowFrom )
    copyStylesFromRow += rows;
var cseS = new CellsStoreEnumerator<ExcelCoreValue>(_values, copyStylesFromRow, 0, copyStylesFromRow, ExcelPackage.MaxColumns); //Fixes issue 15068 , 15090
lionpeloux commented 5 years ago

Same here. Workaround refRow will be moved to refRow + numRowsToInsert by the insert. So you need to point to refRow + numRowsToInsert to copy the style

See here https://stackoverflow.com/a/34299694/7060811