I’ve tried the code above and it deletes everything in my excel, I had to change around some of the stuff from my end:
Cells cellsmerge = workbook.Worksheets[0].Cells;
//From your template file I assume the data table starts from the third row
int rowHead = 2;
//because in the template file there are data of OUTPUT,
// so I set static value here for the end row. For your case maybe it
// should be cells.MaxDataRow or others
int rowEnd = worksheet.Cells.MaxDataRow;
for (int i = rowEnd - 1; i > rowHead; i--)
{
//I use ID as the criteria to check whether it is the row where
// the next row needs to be merged into
Cell cell = cellsmerge.CheckCell(i, 0);
if (cell == null || cell.Type == CellValueType.IsNull)
{
continue;
}
IEnumerator en = cellsmerge.Rows[i + 1].GetEnumerator();
while (en.MoveNext())
{
Cell cell1 = (Cell)en.Current;
if (cell1.Type != CellValueType.IsNull)
{
cells[i, cell1.Column].Copy(cell1);
}
}
cellsmerge.DeleteRow(i + 1);
}
I’m thinking maybe the Cells cellsmerge = workbook.Worksheets[0].Cells; would also need to be changed,
Also here’s what I have already declared in the code prior to adding the above code:
Workbook workbook = Excel.BuildWorkbook(new MemoryStream(batchContent));
workbook.Worksheets.ActiveSheetIndex = workbook.Worksheets[sheetName].Index;
Worksheet worksheet = workbook.Worksheets[sheetName];
Cells cells = worksheet.Cells;
If this changes anything.
I also run some column deletes right before I’m merging rows, I assume that shouldn’t make too much of a difference but in case it does feel free to let me know:
worksheet.Cells.DeleteColumn(27);
worksheet.Cells.DeleteColumn(26);
worksheet.Cells.DeleteColumn(23);
worksheet.Cells.DeleteColumn(22);
worksheet.Cells.DeleteColumn(21);
worksheet.Cells.DeleteColumn(20);
worksheet.Cells.DeleteColumn(19);
worksheet.Cells.DeleteColumn(18);
worksheet.Cells.DeleteColumn(15);
worksheet.Cells.DeleteColumn(14);
worksheet.Cells.DeleteColumn(3);
worksheet.Cells.DeleteColumn(2);
worksheet.Cells.DeleteColumn(1);
worksheet.Cells.DeleteColumn(0);
Keep in mind the excel I provided is not the excel I’m actually using, it’s a mock up of something similar I need done