2013-10-22 69 views
2

我在Excel中有一个范围,我需要按两列进行排序。数据总是从A列到AA列,我需要先按列A(这是一个日期列,最早到最新),然后按列F(数字列,从最小到最高)对它进行排序。行数会有所不同。尝试使用c对两列进行排序excel范围

这是我到目前为止,请记住我相对较新的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."

+1

这是不是你的实际代码?基于此,它看起来像你正在排序一个空白表? – user2140261

+0

我忘了复制宣布我的工作表的部分,我编辑了我的帖子以包含此部分。我声明了一个tmpSheet变量,并在循环中使用它来检查三个特定工作表,然后删除它们(如果存在)。接下来,我检查tmpSheet =“工作成本”,这是工作表需要执行的代码,然后让我的JobDataSheet变量=我的tmpSheet。 – Cornelius

回答

7

这对我来说是什么在起作用:

private void SortExcel() 
{ 
    //Set up 
    Excel.Application oXL; 
    Excel._Workbook oWB; 
    Excel._Worksheet oSheet; 
    Excel.Range oRng; 
    Excel.Range oLastAACell; 
    Excel.Range oFirstACell; 

    //Start Excel and get Application object. 
    oXL = new Excel.Application(); 
    oXL.Visible = true; 

    //Get a new workbook.; 
    oWB = (Excel._Workbook)(oXL.Workbooks.Open(@"C:\Book.Xlsx")); 

    //Get Sheet Object 
    oSheet = (Excel.Worksheet)oWB.Worksheets["Sheet1"]; 

    //Get complete last Row in Sheet (Not last used just last)  
    int intRows = oSheet.Rows.Count; 

    //Get the last cell in Column AA 
    oLastAACell = (Excel.Range)oSheet.Cells[intRows, 27]; 

    //Move courser up to the last cell in AA that is not blank 
    oLastAACell = oLastAACell.End[Excel.XlDirection.xlUp]; 

    //Get First Cell of Data (A2) 
    oFirstACell = (Excel.Range)oSheet.Cells[2, 1]; 

    //Get Entire Range of Data 
    oRng = (Excel.Range)oSheet.Range[oFirstACell, oLastAACell]; 

    //Sort the range based on First Columns And 6th (in this case A and F) 
    oRng.Sort(oRng.Columns[1, Type.Missing],Excel.XlSortOrder.xlAscending, // the first sort key Column 1 for Range 
       oRng.Columns[6, Type.Missing],Type.Missing, Excel.XlSortOrder.xlAscending,// second sort key Column 6 of the range 
       Type.Missing, Excel.XlSortOrder.xlAscending, // third sort key nothing, but it wants one 
       Excel.XlYesNoGuess.xlGuess, Type.Missing, Type.Missing, 
       Excel.XlSortOrientation.xlSortColumns, Excel.XlSortMethod.xlPinYin, 
       Excel.XlSortDataOption.xlSortNormal, 
       Excel.XlSortDataOption.xlSortNormal, 
       Excel.XlSortDataOption.xlSortNormal); 
    } 
+0

非常感谢,完美的工作! – Cornelius

+0

@Cornelius很高兴到这里!通过点击答案旁边的复选标记,随时接受答案作为答案。 – user2140261

+0

我在末尾加了'oWB.Save(); oWB.Close();'以保存更新后的表格并关闭Excel表格,但是当我回去打开它时,出现以下错误:' Excel在'file.xlsx'中发现了不可读的内容。你想恢复这个工作簿的内容吗?如果您信任此工作簿的来源,请单击“是”。如果我单击“是”,它会打开并显示带有排序的Excel文件。任何想法为什么以及如何预防它?谢谢。 +1 btw的代码工作。 – Si8

相关问题