方法排序类范围失败c# - excel

时间:2011-04-28 05:25:48

标签: c# excel sorting

我正在尝试使用c#使用Microsoft.Office.Interop.Excel对列进行排序,并且我得到“类别范围失败的方法”错误。代码是:

        Excel.Workbook theWorkbook = objApp.Workbooks.Open("D:/rank.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing); 
        Excel.Sheets sheets = theWorkbook.Sheets;
        Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(2);

        Excel.Range oRng = worksheet.get_Range("E4", "E100");
        oRng.Activate();
        oRng.Cells.Select();

        oRng.Sort(Type.Missing, Excel.XlSortOrder.xlAscending,Type.Missing, Type.Missing,
        Excel.XlSortOrder.xlAscending, Type.Missing,Excel.XlSortOrder.xlAscending, Excel.XlYesNoGuess.xlNo,
        Type.Missing, Type.Missing, Excel.XlSortOrientation.xlSortColumns,Excel.XlSortMethod.xlPinYin, 
        Excel.XlSortDataOption.xlSortNormal,Excel.XlSortDataOption.xlSortNormal,
        Excel.XlSortDataOption.xlSortNormal);

        theWorkbook.Save();
        theWorkbook.Close();
        objApp.Quit();
        Marshal.ReleaseComObject(objApp);

我错过了什么吗?

电子表格没有受到保护,也没有密码可以打开它(我曾在一个简单的电子表格中尝试过一列30行,但也无法使用)

如果有人可以提供帮助,我将非常感激。

提前致谢。

此致

理学

2 个答案:

答案 0 :(得分:1)

oRng.Sort("This should be column name you going sort",
    Excel.XlSortOrder.xlAscending,
    Type.Missing,
    Type.Missing,
    Excel.XlSortOrder.xlAscending,
    Type.Missing,Excel.XlSortOrder.xlAscending,
    Excel.XlYesNoGuess.xlNo,
    Type.Missing,
    Type.Missing,
    Excel.XlSortOrientation.xlSortColumns,
    Excel.XlSortMethod.xlPinYin, 
    Excel.XlSortDataOption.xlSortNormal,
    Excel.XlSortDataOption.xlSortNormal,
    Excel.XlSortDataOption.xlSortNormal)

答案 1 :(得分:0)

尝试在Excel中编写宏,然后在C#中实现保存的VB代码。