C#Excel的interopsorting范围,使空白单元格走到最后,数据单元格上升

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

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

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

图像正确显示,但仅仅是因为从input源检索的数据是按正确的顺序input的。 如果数据没有按照正确的顺序input,那么从一开始,空白单元格可能不在G列的最下面。

这是我有,为每个Ddate范围(一天)进行sorting。

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'是电子表格中数据最后一行之后的行的索引号。

但是,如这里所示,它对行进行sorting,以便列G中的空白单元格到达所选范围的顶部。

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

我的第二个问题是,在完成这个sorting之后,如何从所选范围中只select列G中单元格不为空的行? 所以我可以再次分类

谢谢。

最后,我设法做到这一点。

“数据组”是一组在D(D)列中具有相同值的行,按天分组。 “types”是不是真正相关的,这是因为在我的DataSet.Table [0]我有一个列中有两个可能的值,并根据行中的值,该行将被写入工作簿中的第一个或第二个工作表。

“Incasari”是我用来sorting的列。 像这样,符号,数字,小写字母,大写字母,然后空格或空string来; 该栏只有符号,数字,小写字母和空格/空string。

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

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