Search code examples
c#epplus

EPPlus copy a column's data into an empty column


I need to swap two columns in an Excel document using EPPlus. Or alternatively is there a way to copy a column into a freshly inserted column?


Solution

  • Found the answer in a comment on this answer to another question.

    > workSheet.Cells["A1:I1"].Copy(workSheet.Cells["A4:I4"]);
    

    but in order to copy say column 5 to column 2 you could do

    > workSheet.Cells[1,5,100,5].Copy(workSheet.Cells[1,2,100,2]);
    

    Instead of 100 you can plug in a maxvalue from one of your columns

    var max = worksheet.Column(index).ColumnMax
    

    Which allows you to have a less arbitrary

    > workSheet.Cells[1,5,max,5].Copy(workSheet.Cells[1,2,max,2]);