在 C# 中對 Excel 範圍進行排序
from ironxl import *

# Load the Excel workbook
workbook = WorkBook.Load("sample.xlsx")
worksheet = workbook.DefaultWorkSheet

# Select a range
selected_range = worksheet["A1:D20"]

# Select a column(B)
column = worksheet.GetColumn(1)

# Sort the range in ascending order (A to Z)
selected_range.SortAscending()

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

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

# Save changes with the sorted range and column
workbook.SaveAs("sortExcelRange.xlsx")
pip install ironxl
在 C# 中對 Excel 範圍進行排序

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

將資料按字母順序或數字順序排列,使分析實用且高效。 IronXL for Python 使排序 Excel 欄、列和範圍變得簡單,只需少量程式碼。

在任何選定的範圍或欄上使用 SortAscending 或 SortDescending 方法,以應用所需順序的排序。 SortByColumn 方法需要兩個參數:第一個是用來排序的欄,第二個是排序順序。

學習在 Excel 中使用 Python 命名範圍

準備好開始了嗎?

版本:2026.11剛剛發布