How to Move a sheet using EPPlus?
Asked Answered
S

3

7

What is the command needed in EPPlus , to move a worksheet location in a workbook?

I couldn't find any Move command for EPPlus only Interop.

Subplot answered 22/6, 2016 at 14:23 Comment(0)
A
17

There are four methods for moving a worksheet. They are

excelPackage.Workbook.Worksheets.MoveAfter()
excelPackage.Workbook.Worksheets.MoveBefore()
excelPackage.Workbook.Worksheets.MoveToStart()
excelPackage.Workbook.Worksheets.MoveToEnd()
Apograph answered 24/6, 2016 at 19:34 Comment(0)
D
1

For anybody finding this in 2021, @chandler is totally correct, but let me add this:

//
// Summary:
//     Moves the source worksheet to the start of the worksheets collection
//
// Parameters:
//   sourceName:
//     The name of the source worksheet
public void MoveToStart(string sourceName);

myExcelPackage.Workbook.Worksheets.MoveToStart(myExcelWorkSheetObject.Name);

The worksheetName input parameter tripped me up.

Dynamite answered 6/4, 2021 at 7:29 Comment(0)
T
0

I have done this job. sharing answer if someone gets help from my answer.

FileInfo newFile = new FileInfo("d:\\TestOrder.xlsx");
ExcelPackage pck = new ExcelPackage(newFile);

List<string> _sheet = new List<string>();
_sheet.Add("Summary");
_sheet.Add("Vertical");
_sheet.Add("DashBoard");
_sheet.Add("Delta");

int counter = 1;
foreach (var wsname in pck.Workbook.Worksheets)
{
    string worksheetname = wsname.Name;

    int index = _sheet.FindIndex(str => str.Contains(worksheetname));
    pck.Workbook.Worksheets.MoveAfter(counter, index + 1);
    counter++;
}

pck.Save();
Tai answered 29/9, 2021 at 17:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.