C# Excel 互操作排序范围,因此空白单元格排在最后,包含数据的单元格排在最前面

标签 c# excel sorting excel-interop

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

http://www.tiikoni.com/tis/view/?id=af4cf69

现在我需要按 G 列对选定范围(如图所示)进行排序,以便空白单元格位于选定范围的底部。

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

这就是我要对每个 D 日期范围(一天)进行排序的方法。

    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 列中的空白单元格到达所选范围的顶部。

http://www.tiikoni.com/tis/view/?id=ea48320

我的第二个问题是,在进行此排序后,如何从所选范围中仅选择 G 列中的单元格不为空的行? - 这样我就可以再次对它们进行排序。

谢谢。

最佳答案

最后我成功地做到了这一点。

“数据组”是一组在 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
});

这可能不是解决这个排序问题的最佳方法,但我找不到更好的方法。

关于C# Excel 互操作排序范围,因此空白单元格排在最后,包含数据的单元格排在最前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12041673/

相关文章:

c# - 如何检测现在是否有视频正在播放?

c# - 从 .NET/C# 中的站点下载图像

python - 我需要安装 Excel 才能使用 Openpyxl "interactively"吗?它可以与 .ODS 格式交互吗?

asp.net-mvc - Excel 不想打开通过 ASP 输出下载的 XLSX 文件

python - 如何根据Python中的列标题值选择excel列

c++ - 合并排序 C++ 帮助 为合并的数字动态分配临时数组

c - Quicksort 取决于选择 Pivot

c# - 在循环重复期间保持准确的条件

c# - 模拟高尔夫比赛策略

java - 创建一个 int 类型的随机数组。 java