closedxml 按特定数字排序

closedxml sorting by specific number

我对 C# 中的 ClosedXML 有疑问。我想对数据进行排序,但我希望系统读取列中包含 1、2、3、4 之类的数字,然后按该数字排序。

如果我使用 ws.Sort(1) 标题也会排序。

这是我的代码:

protected void ImportExcel(object sender, EventArgs e)
{
    //Save the uploaded Excel file.
    string filePath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.SaveAs(filePath);

//Open the Excel file using ClosedXML.
using (XLWorkbook workBook = new XLWorkbook(filePath))
{
    //Read the first Sheet from Excel file.
    IXLWorksheet workSheet = workBook.Worksheet(1);

    //Create a new DataTable.
    DataTable dt = new DataTable();
    workSheet.sort(1);
    //Loop through the Worksheet rows.
    bool firstRow = true;
    foreach (IXLRow row in workSheet.Rows())
    {
        //Use the first row to add columns to DataTable.
        if (firstRow)
        {
            foreach (IXLCell cell in row.Cells())
            {
                dt.Columns.Add(cell.Value.ToString());
            }
            firstRow = false;
        }
        else
        {
            //Add rows to DataTable.
            dt.Rows.Add();
            int i = 0;
            foreach (IXLCell cell in row.Cells())
            {
                dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
                i++;
            }
        }

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
}

有没有用数字对特定列进行排序的方法?

Select要排序的单元格区域,但排除headers并调用IXLRange.Sort()方法,而不是IXLWorksheet.Sort()方法。

示例:

var range = wb.Worksheet("Sheet1").Range("A2:C6");
range.Sort(1);