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.
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.
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()
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.
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();
© 2022 - 2024 — McMap. All rights reserved.