我在excel中有一个范围,我需要按两列排序。数据的范围总是从 A 列到 AA 列,我需要首先按 A 列(这是一个日期列,所以从最旧到最新)对其进行排序,然后按 F 列(数字列,从最小到最高)进行排序。行数会有所不同。
这是我到目前为止所得到的,请记住我对 c# 比较陌生。
Excel.Worksheet JobDataSheet = new Excel.Worksheet();
foreach (Excel.Worksheet tmpSheet in Globals.ThisAddIn.Application.ActiveWorkbook.Sheets)
{
if (tmpSheet.Name == "Job Labour" || tmpSheet.Name == "Job Materials" || tmpSheet.Name == "Job Cost Report")
{
tmpSheet.Delete();
}
if (tmpSheet.Name == "Job Cost")
JobDataSheet = tmpSheet;
}
int MyCount = JobDataSheet.UsedRange.Rows.Count;
//Sort Collection by Date & Ref Line
Excel.Range tempRange = JobDataSheet.get_Range("A2:A" + MyCount);
Excel.Range tempRange2 = JobDataSheet.get_Range("F2:F" + MyCount);
JobDataSheet.Sort.SortFields.Clear();
JobDataSheet.Sort.SortFields.Add(tempRange // First Key
,Excel.XlSortOn.xlSortOnValues
,Excel.XlSortOrder.xlAscending
,Type.Missing
,Excel.XlSortDataOption.xlSortNormal);
JobDataSheet.Sort.SortFields.Add(tempRange2 // Second Key
, Excel.XlSortOn.xlSortOnValues
, Excel.XlSortOrder.xlAscending
, Type.Missing
, Excel.XlSortDataOption.xlSortNormal);
JobDataSheet.Sort.SetRange(JobDataSheet.get_Range("A1:AA" + MyCount));
JobDataSheet.Sort.Header = Excel.XlYesNoGuess.xlYes;
JobDataSheet.Sort.MatchCase = false;
JobDataSheet.Sort.Orientation = Excel.XlSortOrientation.xlSortRows;
JobDataSheet.Sort.SortMethod = Excel.XlSortMethod.xlPinYin;
JobDataSheet.Sort.Apply();
在行JobDataSheet.Sort.Apply();
excel 抛出"The sort reference is not valid. Make sure that it's within the data you want to sort, and the first Sort By box isn't in the same or blank."