简体   繁体   English

如何按列(不包括第一行)对数据进行排序

[英]How to sort data by column excluding the first row

I need to sort a worksheet by the 11th column. 我需要按第11列对工作表进行排序。 This code works but includes the column header so that it moves down the worksheet. 该代码有效,但包含列标题,以便其在工作表中向下移动。

I have already tried the below code 我已经尝试了以下代码

worksheet.Cells[1, 1].EntireRow.Font.Bold = true;
var startCell = (Excel.Range)worksheet.Cells[1, 1];
var endCell = (Excel.Range)worksheet.Cells[row, 12];

Excel.Range range = worksheet.Range[startCell, endCell];
range.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders.Weight = Excel.XlBorderWeight.xlThin;

Excel.Range fullDataRange = worksheet.UsedRange;
fullDataRange.Sort(fullDataRange.Columns[11], 
Excel.XlSortOrder.xlAscending);

excelApp.ActiveWorkbook.SaveAs(txtOutputFolder.Text + "\\" + 
filestart + " Instructions" + DateTime.Now.ToString(" dd.MM") + ".xlsx");
            excelApp.ActiveWorkbook.Close();

您可以选择所需的范围:

 worksheet.Range("A1:H8")

If you check the Sort documentation you will see that you can specify if the sort range has a header. 如果查看“ 排序”文档,将会看到您可以指定排序范围是否具有标题。 Just add a named parameter to you sort command : 只需在您的排序命令中添加一个命名参数即可:

fullDataRange.Sort(fullDataRange.Columns[11], Excel.XlSortOrder.xlAscending, Header:xlYes);

and the first row will be ignored when sorting. 并且排序时第一行将被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM