Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

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);
    }
}