在 C# 中對 Excel 範圍進行排序
using IronXL;

WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Select a range
var range = workSheet["A1:D20"];

// Select a column(B)
var column = workSheet.GetColumn(1);

// Sort the range in ascending order (A to Z)
range.SortAscending();

// Sort the range by column(C) in ascending order
range.SortByColumn("C", SortOrder.Ascending);

// Sort the column(B) in descending order (Z to A)
column.SortDescending();

workBook.SaveAs("sortExcelRange.xlsx");
NuGet 下載
PM > Install-Package IronXL.Excel
在 C# 中對 Excel 範圍進行排序

在 C# 中對 Excel 範圍進行排序

在 Microsoft Excel 中,按字母順序或數值順序排列資料,使得人工分析資料成為可能。 IronXL使得在C#和VB.NET中排序Excel Columns、Rows和Ranges極其簡單。

在選定的範圍或列上使用 SortAscending 或 SortDescending 方法以適當的順序進行排序。 然而,SortByColumn方法需要兩個參數:第一個輸入是要排序的列,第二個輸入是排序順序。

學習如何使用 C# 在 Excel 中使用命名區域
在 GitHub 查看

準備好開始了嗎?

NuGet 下載次數 2,393,101版本:2026.11剛剛發布