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?
Asked
Active
Viewed 3,929 times
1 Answers
2
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]);
-
Is there a reason why you wouldn't do a column range like `A:A`? – Captain Prinny Jan 03 '20 at 17:29
-
@CaptainPrinny you're always welcome to add an answer if you think there is a better way. I no longer use this library so I can't really speak to what I was thinking 3 years ago but the answer worked for me at the time. – M Y Jan 03 '20 at 18:22