C# 尝试使用c按两列对excel范围进行排序#

C# 尝试使用c按两列对excel范围进行排序#,c#,excel,sorting,C#,Excel,Sorting,我在excel中有一个范围,需要按两列进行排序。数据总是从A列到AA列,我需要首先按A列(这是一个日期列,从最早到最新)排序,然后按F列(数字列,从最小到最高)排序。行数将有所不同 这是我到目前为止得到的,请记住我对c#还比较陌生 在JobDataSheet.Sort.Apply()中行excel抛出“排序引用无效。请确保它位于要排序的数据内,并且第一个排序依据框不在同一个框中或为空。”这对我来说是有效的: private void SortExcel() { //Set up

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

这是我到目前为止得到的,请记住我对c#还比较陌生


JobDataSheet.Sort.Apply()中行excel抛出
“排序引用无效。请确保它位于要排序的数据内,并且第一个排序依据框不在同一个框中或为空。”

这对我来说是有效的:

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

这不是你的真实代码吗?基于此,您似乎正在对空白工作表进行排序?我忘了复制我声明工作表的部分,我编辑了我的帖子以包含此部分。我声明了一个tmpSheet变量,并在循环中使用该变量检查三个特定工作表,然后删除(如果存在)。接下来,我检查tmpSheet=“Job Cost”这张表是否是代码需要执行的表,然后让我的JobDataSheet变量=我的tmpSheet。@Cornelius很高兴看到这里!通过单击答案旁边的复选标记,您可以随意接受此答案作为您问题的答案!我在末尾添加了以下内容
oWB.Save();oWB.Close()
使用sort保存更新后的工作表并关闭Excel工作表,但当我返回打开它时,会出现以下错误:
Excel在“file.xlsx”中发现不可读的内容。是否要恢复此工作簿的内容?如果您信任此工作簿的来源,请单击“是”。
如果我单击“是”,它将打开并显示带有排序的Excel文件。知道为什么以及如何预防吗?谢谢+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);
    }