【问题标题】:Trying to sort excel range by two columns using c#尝试使用c#按两列对excel范围进行排序
【发布时间】:2013-11-01 00:08:51
【问题描述】:

我在 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."

【问题讨论】:

  • 这不是您的实际代码吗?基于此,您似乎正在对空白工作表进行排序?
  • 我忘记复制我声明工作表的部分,我编辑了我的帖子以包含此部分。我声明了一个 tmpSheet 变量,并在循环中使用它来检查三个特定的工作表,然后我如果存在则删除。接下来,我检查 tmpSheet = "Job Cost" 是否是代码需要执行的工作表,然后我将 JobDataSheet 变量设置为我的 tmpSheet。

标签: c# excel sorting


【解决方案1】:

这对我有用:

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);
    }

【讨论】:

  • @Cornelius 很高兴来到这里!点击答案旁边的复选标记,随时接受此答案作为您问题的答案!
  • 我在末尾添加了以下内容 oWB.Save();oWB.Close(); 以使用排序保存更新的工作表并关闭 Excel 工作表,但是当我返回打开它时,我收到以下错误:Excel found unreadable content in 'file.xlsx'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes. 如果我单击是,它会打开并显示带有排序的 Excel 文件。知道为什么以及如何预防吗?谢谢。 +1 顺便说一句,代码可以正常工作。
  • 如果我按是并打开并将其另存为其他文件,则新文件会正常打开。我想知道为什么第一个文件有问题。任何帮助,将不胜感激。谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-12-27
  • 1970-01-01
  • 2015-06-03
  • 1970-01-01
  • 2010-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多