我一直試圖根據第一列(即日期)對一個范圍進行排序。但當我運行我的代碼時,似乎什么也沒有發生。檔案中沒有任何變化。以下是我的代碼。
Excel.Application xlApp = new Excel.Application()。
Excel.Workbook xlWorkBook;
Excel.作業表 xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
String file = @"C:Book1.xlsx"。
xlWorkBook = xlApp.Workbooks.Open(file)。
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1) 。
Excel.Range rng = xlWorkSheet.get_Range("B16"/span>, "I38"/span>)。
rng.Sort(rng.Columns[1, Type.Missing], Excel.XlSortOrder.xlDescending,
Type.Missing, Type.Missing, 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)。)
xlWorkBook.Close(true, misValue, misValue)。
uj5u.com熱心網友回復:
因為你給rngsort變數設定了null值。
uj5u.com熱心網友回復:
如果有人在尋找或解決方法的話。
Excel.Application xlApp = new Excel.Application()。
# setting constants, workbook, sheet, etc.
Excel.作業簿 xlWorkBook;
Excel.作業表 xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
String file = @"C:Book1.xlsx"。
xlWorkBook = xlApp.Workbooks.Open(file)。
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1)。
# 從感興趣的列(本例中為B列)的使用范圍中確定最后一行。
Excel.Range UsedRange = xlWorkSheet.UsedRange.Columns["B:B"/span>]。
int lastRow = UsedRange.Row UsedRange.Rows.Count - 1;
# 獲得感興趣的范圍...B15:I<最后一行>
Excel.Range rng = xlWorkSheet.get_Range("B15", "I" lastRow)。
# 最后,根據原始標準對感興趣的范圍的行進行排序。
rng.Sort(rng.Rows, Excel.XlSortOrder.xlAscending,
Type.Missing, Type.Missing, Excel.XlSortOrder.xlAscending,
型別.缺失, 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)。)
rng = null。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/310859.html
標籤:
