DEV Community

Jeremy K.
Jeremy K.

Posted on

Reorder Excel Columns with C#

When processing data or generating reports in .NET applications, reordering Excel columns is a common requirement—whether to match downstream system schemas, improve data readability, or standardize layouts across multiple files. While drag-and-drop works fine for one-off tasks with small files, batch processing and automated workflows demand a code-based solution.

This article walks through how to rearrange Excel column positions using a free, lightweight .NET library—without requiring Microsoft Office to be installed.

You can install the free library via NuGet Package Manager by searching for Free Spire.XLS.

After installation, add the following using directive to your code file:

using Spire.Xls;
Enter fullscreen mode Exit fullscreen mode

Core Strategy

Free Spire.XLS doesn’t expose a dedicated Reorder() API. Instead, we implement column reordering with a simple three-step approach:

  1. Create a temporary worksheet and clone the full data from the target worksheet.
  2. Copy columns from the temporary sheet back to the original sheet, following the new desired order.
  3. Delete the temporary worksheet.

This "backup, overwrite, clean up" pattern ensures that all data, formatting, and column widths are fully preserved.


Complete Code Example

using System.Linq;
using Spire.Xls;

namespace ReorderExcelColumns
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1. Load the source Excel file
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"C:\sample.xlsx");

            // 2. Reference the first worksheet
            Worksheet worksheet = workbook.Worksheets[0];

            // 3. Define the target column order (0‑based indices)
            // Example: Move the original 4th column (index 3) to the front,
            // shifting columns 1–3 to the right.
            int[] newColumnOrder = new int[] { 3, 0, 1, 2, 4, 5 };

            // 4. Create a temporary sheet and clone all data from the original
            Worksheet tempSheet = workbook.Worksheets.Add("temp");
            tempSheet.CopyFrom(worksheet);

            // 5. Reorder columns by copying from the temporary sheet
            for (int i = 0; i < newColumnOrder.Length; i++)
            {
                tempSheet.Columns[newColumnOrder[i]].Copy(
                    worksheet.Columns[i],
                    true,   // Copy cell values
                    true    // Copy formatting
                );

                // 6. Preserve column width for consistency
                worksheet.Columns[i].ColumnWidth =
                    tempSheet.Columns[newColumnOrder[i]].ColumnWidth;
            }

            // 7. Remove the temporary sheet
            workbook.Worksheets.Remove(tempSheet);

            // 8. Save the reordered workbook
            workbook.SaveToFile("Reordered.xlsx", FileFormat.Version2016);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Key API Reference

API Description
Workbook.LoadFromFile() Loads an existing Excel workbook
Workbook.Worksheets.Add() Creates a new blank worksheet
Worksheet.CopyFrom() Copies all data and formatting from one sheet to another
Worksheet.Columns[index].Copy() Copies a specific column (values + optional formatting) to a target column
Worksheet.Columns[index].ColumnWidth Gets or sets the width of a column
Workbook.SaveToFile() Saves the workbook to disk

Typical Use Cases

  • Batch restructuring – Apply the same column order to hundreds of Excel files in a single run.
  • UX-focused presentation – Promote key identifiers (like order IDs or dates) to the leftmost positions for better readability.
  • Automated reporting pipelines – Standardize column sequences during ETL or report generation flows.
  • Data consolidation – Align Excel imports from disparate sources into a uniform column structure before merging.

Top comments (0)