C#EPPlus – sorting插入的数据创build行空的查询

我有一个包含有关产品类别的信息的Excel文件:

FatherId | CategoryId | CategoryLevel | CategoryName 

我需要为每个类别构build一个层次结构,如下所示:

  A.Id | |__A.FatherId = B.Id | |__B.FatherId = C.Id... 

我可以循环多次通过excel文件并build立层次结构。 但是我发现真的很无效。 我决定把每个级别(最多9级)分成不同的excel文件。 我通过在正确的位置插入每个新的类别id来创build一个有序的文件。 我的代码的问题是,有时我“创build”一些空行,我不明白为什么。

以下是处理这个问题的代码:

 int integerPosition = 2; while (!string.IsNullOrEmpty(worksheet.Cells["A" + integerPosition].Text)) { string parentId = worksheet.Cells["A" + integerPosition].Text.Trim(); string id = worksheet.Cells["B" + integerPosition].Text.Trim(); string type = worksheet.Cells["C" + integerPosition].Text.Trim(); // string subType = worksheet.Cells["D" + integerPosition].Text.Trim(); string name = worksheet.Cells["E" + integerPosition].Text.Trim(); string imageId = worksheet.Cells["F" + integerPosition].Text.Trim(); if (string.IsNullOrEmpty(id) || string.IsNullOrWhiteSpace(id)) { throw new Exception($"Id at position B{integerPosition} is empty"); } CategoryLevel level = ParseCategoryLevelFromString(type); // If you take a look it might seems that ids are not correctly ordered. // But since some ids are strings and others are numbers we treat both as strings. // So, for example, id1: 10000023, id2: 5432 -> id1 < id2, because id1[0] < id2[0].. long indexToInsert = FindInsertionIndexDichotomically( id, position => { string requestedId = tempWorksheets[level].Cells["B" + (position + 1)].Text; if (string.IsNullOrEmpty(requestedId) || string.IsNullOrWhiteSpace(requestedId)) { Console.WriteLine($"[{integerPosition}] RequestedId at position B{position + 1} is empty"); } return requestedId; }, lastUsedPositions[level] ); // If the cell at index indexToInsert is empty there's no need to add any row if (tempWorksheets[level].Cells["B" + (indexToInsert + 1)].Value != null) { tempWorksheets[level].InsertRow((int)indexToInsert + 1, 1); Utilities.LogInfo($"[{level}] Inserted row at position: {indexToInsert + 1}", LogFilePath); } tempWorksheets[level].SetValue("A" + (indexToInsert + 1), parentId); tempWorksheets[level].SetValue("B" + (indexToInsert + 1), id); tempWorksheets[level].SetValue("C" + (indexToInsert + 1), name); tempWorksheets[level].SetValue("D" + (indexToInsert + 1), imageId); Utilities.LogInfo($"[{level}] Written category ({id}) at row: {indexToInsert + 1}", LogFilePath); // Update the number of element inserted lastUsedPositions[level]++; integerPosition++; } 

这里是FindInsertionIndexDichotomically的代码:

 private static long FindInsertionIndexDichotomically<T>(T target, Func<long, T> getElementAtIndex, long count, Func<T, T, int> customComparison = null) where T : IComparable { Func<T, T, int> compare = customComparison ?? ((arg1, arg2) => arg1.CompareTo(arg2)); #region Boundary condition // The collection is empty if (count == 0) { return 0; } // The element to insert is smaller or equal then the first element if (compare(target, getElementAtIndex(0)) <= 0) { return 0; } // The element to insert is grater or equal then the last element if (compare(target, getElementAtIndex(count - 1)) >= 0) { return count; } #endregion // ReSharper disable once ConvertToLocalFunction Func<long, long, long> innerBinarySearch = null; // Do not collapse this line with a direct assignment, // otherwise _inner_bynarySearch cannot call itself innerBinarySearch = (left, right) => { // If the collection is odd we will eventually have left == right // If the collection is even we won't have left == right, but right will be smaller then left if (right <= left) { return compare(target, getElementAtIndex(left)) <= 0 ? left : left + 1; } long relativeCenter = left + (right - left) / 2; int comparisonResult = compare(target, getElementAtIndex(relativeCenter)); // The element is already present at relativeCenter if (comparisonResult == 0) { return relativeCenter; } // The element is to be inserted between left and relativeCenter - 1 (relativeCenter - 1, // we already checked the centered positon if (comparisonResult < 0) { return innerBinarySearch(left, relativeCenter - 1); } // The element is to be inserted between relativeCenter - 1 and right (relativeCenter + 1, // we already checked the centered positon return innerBinarySearch(relativeCenter + 1, right); }; return innerBinarySearch(0, count - 1); } 

在这个操作后,我得到一个sorting的Excel文件,但有时我得到了一些空行(我检查,他们没有内容。源文件没有差距,我不明白为什么。