简体   繁体   中英

the method sort of class range failed c# - excel

I´m trying to sort a column using Microsoft.Office.Interop.Excel using c# and I'm getting a "the method sort of class range failed" error. The code are:

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

Am I missing something?

The spreadsheet are not protected and doesn't have a password to open it (I've tried in a simple spreadsheet with one column and 30 rows and doesn't work either)

If anyone can help, I'll be very appreciated.

Thanks in advance.

Regards,

Neo

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)

Try to write a macros in Excel and then implement saved VB code in C#.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM