C# C Excel互操作排序范围,使空白单元格最后一个,带数据的单元格上升

C# C Excel互操作排序范围,使空白单元格最后一个,带数据的单元格上升,c#,excel,sorting,excel-interop,C#,Excel,Sorting,Excel Interop,我有一个Excel 97-2003.xls电子表格,使用C-4.0和Excel.Interop从.dbf转换而来。数据根据D列按日期排序 现在我需要按G列对图像中显示的选定区域进行排序,以便空白单元格位于选定区域的底部 图像正确地显示了它,但这仅仅是因为从输入源检索到的数据是按正确的顺序输入的。如果数据输入顺序不正确,那么从一开始,空白单元格可能就不在G列的底部 这就是我所拥有的,每天对每个日期范围进行排序 Range incasariSortRange; Range sRang

我有一个Excel 97-2003.xls电子表格,使用C-4.0和Excel.Interop从.dbf转换而来。数据根据D列按日期排序

现在我需要按G列对图像中显示的选定区域进行排序,以便空白单元格位于选定区域的底部

图像正确地显示了它,但这仅仅是因为从输入源检索到的数据是按正确的顺序输入的。如果数据输入顺序不正确,那么从一开始,空白单元格可能就不在G列的底部

这就是我所拥有的,每天对每个日期范围进行排序

    Range incasariSortRange;
    Range sRange;
    int startDateRowIndex = 6; // index of row where a D date starts
    int endDateRowIndex = 6; // index of row where the same D date ends

    public void selectGroupRange()
    {
        for (int r = startDateRowIndex; r < rowIndex; r++)
        {
            if (worksheet.Cells[endDateRowIndex, 4].Value == worksheet.Cells[r, 4].Value)
            {
                endDateRowIndex = r;
            }
            else
            {
                incasariSortRange = worksheet.get_Range("B" + startDateRowIndex, "H" + endDateRowIndex);
                sRange = incasariSortRange.get_Range("G" + startDateRowIndex, "G" + endDateRowIndex);

                // Sort the first 'D' date range's row by wether the cells in column 'G' 
                //of that range have any values (to be the first ones) or not (to be the last ones).
                incasariSortRange.Sort(sRange, XlSortOrder.xlAscending,
                    Type.Missing, Type.Missing, XlSortOrder.xlAscending,
                    Type.Missing, XlSortOrder.xlAscending, XlYesNoGuess.xlNo, Type.Missing,
                    Type.Missing, XlSortOrientation.xlSortColumns, XlSortMethod.xlPinYin, XlSortDataOption.xlSortNormal,
                    XlSortDataOption.xlSortNormal, XlSortDataOption.xlSortNormal);

                // Set the start and end (date) row indexes to the same so the incasariSortRange will be one row only.
                startDateRowIndex = r; // set the start to a new date
                endDateRowIndex = r; // set the end to the same new date
            }
        }
    }
“rowIndex”是电子表格中数据位于最后一行之后的行的索引号

但如图所示,它对行进行排序,以便G列中的空白单元格到达所选范围的顶部

我的第二个问题是,在进行排序之后,如何从所选范围中仅选择列G中的单元格不为空的行-这样我就可以再分类了


谢谢。

最后我还是这样做了

数据组是一组在columnD DATE中具有相同值的行,按天分组。类型实际上并不相关,因为在我的DataSet.Table[0]中有一列,其中有两个可能的值,根据行中的值,该行将写入工作簿的第一个或第二个工作表中

Incasari是我用来排序的列。像这样,符号、数字、小写字母、大写字母,然后出现空格或空字符串;该列只有符号、数字、小写字母和空格/空字符串

var sortedDataGroup = datagroup.OrderBy(row =>
{
    var wrapper = new DataRowWrapper(row, type);

    if (wrapper.Incasari != null)
        return wrapper.ContCor.ToLower();
    else
        return "A"; // Capital letters are after lower case letters
});
这可能不是解决排序问题的最佳方法,但我找不到更好的方法