Use the Excel Export API to Reorder Worksheets within a Document

The following example demonstrates how to use the IXlDocument.SetSheetPosition method to change a worksheet’s position within a workbook.

IXlExporter exporter = XlExport.CreateExporter(XlDocumentFormat.Xlsx);
using (FileStream stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite)) {
    using (IXlDocument document = exporter.CreateDocument(stream)) {
        // Create the first worksheet.
        using (IXlSheet sheet = document.CreateSheet()) {
            sheet.Name = "Sheet1"
            // ...
        }
        // Create the second worksheet.
        using (IXlSheet sheet = document.CreateSheet()) {
            sheet.Name = "Sheet2"
            // ...
        }
        // Create the third worksheet.
        using (IXlSheet sheet = document.CreateSheet()) {
            sheet.Name = "Sheet3"
            // ...
        }
        // Move the last worksheet to the first position within the document.
        document.SetSheetPosition("Sheet3", 0);
    }
}