如何在Excel中设置单元格背景图案和颜色

How to Sort Cell Range

This article was translated from English: Does it need improvement?
Translated
View the article in English

Sorting data in alphabetical or value order is essential for data analysis in Microsoft Excel. With IronXL, sorting Excel columns, rows, and ranges becomes straightforward in C# and VB.NET.

Quickstart: Sort a Range by a Column in One Line

Grab your Excel file and in just one line using IronXL’s fluent API, you can sort a chosen cell range by any column. Perfect for getting started quickly, this shows how simple and fast sorting data with IronXL can be.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    workSheet["A1:D10"].SortByColumn("B", IronXL.SortOrder.Ascending);
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer


Sort Columns Independently Example

Use the SortAscending or SortDescending method on the selected range or column to apply sorting in the desired order.

When applying sorting to a range with multiple columns, the SortAscending or SortDescending method will independently iterate and apply the sorting to each column.

These methods push any empty cells to either the top or bottom of the range. To remove these empty cells, you can use the Trim method after sorting. This will eliminate the empty cells, ensuring a clean and organized dataset.

:path=/static-assets/excel/content-code-examples/how-to/sort-cells-range.cs
using IronXL;

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

// Select a column(A)
var column = workSheet.GetColumn(0);

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

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

workBook.SaveAs("sortExcelRange.xlsx");
Imports IronXL

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

' Select a column(A)
Private column = workSheet.GetColumn(0)

' Sort column(A) in ascending order (A to Z)
column.SortAscending()

' Sort column(A) in descending order (Z to A)
column.SortDescending()

workBook.SaveAs("sortExcelRange.xlsx")
$vbLabelText   $csharpLabel
Sort Ascending and Descending

Sort By Column Example

The SortByColumn method allows you to sort a range based on a specified column. This method requires two parameters: the first parameter is the column you want to sort by, and the second parameter is the range to which you want to apply the sorting.

:path=/static-assets/excel/content-code-examples/how-to/sort-cells-sort-by-column.cs
using IronXL;

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

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

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

workBook.SaveAs("sortRange.xlsx");
Imports IronXL

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

' Select a range
Private range = workSheet("A1:D10")

' Sort the range by column(B) in ascending order
range.SortByColumn("B", SortOrder.Ascending)

workBook.SaveAs("sortRange.xlsx")
$vbLabelText   $csharpLabel
Sort by Specific Column

It is not possible to sort by multiple columns yet, for example, sorting by column A and then by column B.

常见问题解答

如何在Excel中使用C#排序单元格范围?

要在Excel中使用C#排序单元格范围,可以使用IronXL。首先,从NuGet下载库,加载Excel电子表格,然后选择要排序的范围或列。根据所需的排序顺序应用SortAscendingSortDescending方法。

我可以使用C#独立地对Excel列进行排序吗?

是的,使用IronXL,可以在C#中独立地对Excel列进行排序。在所选的范围或列上使用SortAscendingSortDescending方法,这将独立地将每列进行排序。

如何以编程方式对Excel行进行降序排序?

要在Excel中以编程方式对行进行降序排序,可以使用IronXL,加载您的Excel文件,并在选定的范围或列上使用SortDescending方法。这将使您的数据按降序排列。

在使用C#排序Excel范围后,是否可以移除空单元格?

是的,在使用IronXL排序Excel范围后,可以通过使用Trim方法移除空单元格。这将通过消除空单元格来清理您的数据集。

如何在Excel中使用C#基于特定列对范围进行排序?

要在Excel使用C#基于特定列对一个范围进行排序,使用IronXL提供的SortByColumn方法。您需要指定列索引和范围以应用排序。

我可以使用C#库同时对多个列进行排序吗?

不,目前IronXL不支持同时对多个列进行排序。您可以使用SortByColumn方法一次按一列进行排序。

如何以编程方式保存排序后的Excel数据?

在使用IronXL对Excel数据排序后,可以通过使用SaveAs方法将排序后的数据保存到新的Excel文件中,以确保您的更改被保存。

在C#中对Excel数据进行排序时,空单元格会发生什么变化?

在使用C#的IronXL进行Excel数据排序时,空单元格将被推到范围的顶部或底部。然后可以使用Trim方法移除这些空单元格。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布