【问题标题】:c# Sorting Rows in Excel using Microsoft Interopc# 使用 Microsoft Interop 在 Excel 中对行进行排序
【发布时间】:2021-11-06 09:33:18
【问题描述】:

我一直在尝试根据第一列(即日期)对范围进行排序。但是当我运行我的代码时,似乎什么也没发生。文件没有变化。以下是我的代码。

Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlWorkBook;
        Excel.Worksheet 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", "I38");

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

【问题讨论】:

    标签: c# excel sorting office-interop


    【解决方案1】:

    因为您将空值设置为 rngsort 变量。

    【讨论】:

    • rngSort = null 排序后我正在清除 rngSort 变量。在此之前进行排序。
    【解决方案2】:

    如果有人正在寻找或解决方案。

    Excel.Application xlApp = new Excel.Application();
            # setup constants, workbook, sheet, etc.
            Excel.Workbook xlWorkBook;
            Excel.Worksheet 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);
    
            # identify the last row from the used range of the column of interest (column B in this case)
            Excel.Range UsedRange = xlWorkSheet.UsedRange.Columns["B:B"];
            int lastRow = UsedRange.Row + UsedRange.Rows.Count - 1;
            
            # get range of interest...B15:I<last row>
            Excel.Range rng = xlWorkSheet.get_Range("B15", "I" + lastRow);
    
            # finally, sort the rows of the range of interest according to the original criteria
            rng.Sort(rng.Rows, Excel.XlSortOrder.xlAscending,
                            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);
            rng = null;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      • 2012-10-19
      • 1970-01-01
      相关资源
      最近更新 更多