簡體   English   中英

C# Excel 范圍排序

[英]C# Excel Range Sort

我想對一個范圍進行排序。 第一行(Excel 工作簿中的第 3 行)包含需要按升序從左到右排序的列標題:

Excel.Range tempRange = ws.get_Range("F3", "H8");

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

這當前會生成錯誤“范圍 class 的排序方法失敗”。

我在排序方法開始時嘗試了各種參數,但這會生成“排序引用無效。 確保它在您要排序的數據中,並且第一個排序依據框不是相同或空白的錯誤消息。

我哪里錯了?

等效的 VBA 工作正常:

 With ActiveWorkbook.Worksheets("Sheet1").Sort
    .SetRange Range("F3:H8")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlLeftToRight
    .SortMethod = xlPinYin
    .Apply
End With

非常感謝

我將 Range 作為第一個參數並將 Orientation 設置為 Excel.XlSortOrientation.xlSortRows。

                tempRange.Sort(tempRange,
                    Excel.XlSortOrder.xlAscending,
                    Type.Missing, Type.Missing,
                    Excel.XlSortOrder.xlAscending,
                    Type.Missing,
                    Excel.XlSortOrder.xlAscending,
                    Excel.XlYesNoGuess.xlYes, 
                    Type.Missing,
                    Type.Missing,
                    Excel.XlSortOrientation.xlSortRows,
                    Excel.XlSortMethod.xlPinYin,
                    Excel.XlSortDataOption.xlSortNormal,
                    Excel.XlSortDataOption.xlSortNormal,
                    Excel.XlSortDataOption.xlSortNormal);

有用的鏈接:

http://social.msdn.microsoft.com/Forums/en-US/exceldev/thread/a699d754-98d5-4241-87da-8761c520ba72/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM