0

I discovered that using OpenXml my columns don't always line up. For instance look at this example

This is the header row column 37 = "Exchange Only" column 38 = "Qty On Hand" column 39 = "Cost Method" column 40 = "Cost Method Amount"

Now, in the next row - the one that has actual data in both 37 and 38 are blank but it omits column 38. so my data looks like this.

column 37 = "" (should be blank) column 38 = "Set Amount" (Should be blank cause it should line up with 'Qty On Hand') column 39 = 0

Notice that it dropped out the real column / cell 38 and now my parser no longer lines up. both 37 and 38 are blank but it doesn't lose 37.

Here is some code to show how I am getting to the string array - it's really just a modified example from MSDN.

public InventoryItemLoadProxy CreateInventoryItemFromSpreadsheetRow(Row row, SharedStringTable sharedStrings)
     {
         var invItem = new InventoryItemLoadProxy();
         var theCells = row.Descendants<Cell>();

         var textValues =
             from cell in row.Descendants<Cell>()
             select(cell.CellValue == null ? string.Empty : ((cell.DataType != null
                             && cell.DataType.HasValue
                              && cell.DataType == CellValues.SharedString) ? sharedStrings.ChildElements[int.Parse(cell.CellValue.InnerText)].InnerText : cell.CellValue.InnerText));

          if(textValues.Any(x => x != string.Empty))
          {
             var textArray = textValues.ToArray();


             invItem.PartNumber = textArray[0].ToStrippedPartNumber();
             invItem.DisplayPartNumber = textArray[0];
             //More parsing...
         }
    }

You can see I am saying if it's null just make it string.empty (yeah - that crazy linq statement will get refactored at some point).

4

1 回答 1

0

事实证明,其他人已经回答了这个问题。请参阅阅读 Excel Open XML 忽略空白单元格。我使用了更多的“蛮力和无知”方法,尽管一旦我有机会对来自其他答案的代码进行单元测试,我可能会进行重构。不过,这可以满足我目前的需求。

private int GetIndexFromCellRef(Cell cell)
        {
            var colname = cell.CellReference.Value;
            colname = Regex.Replace(colname, "[0-9]", "");
            var indexList = new Dictionary<string, int>
                {
                    {"A", 0},
                    {"B", 1},
                    {"C", 2},
                    {"D", 3},
                    {"E", 4},
                    {"F", 5},
                    {"G", 6},
                    {"H", 7},
                    {"I", 8},
                    {"J", 9},
                    {"K", 10},
                    {"L", 11},
                    {"M", 12},
                    {"N", 13},
                    {"O", 14},
                    {"P", 15},
                    {"Q", 16},
                    {"R", 17},
                    {"S", 18},
                    {"T", 19},
                    {"U", 20},
                    {"V", 21},
                    {"W", 22},
                    {"X", 23},
                    {"Y", 24},
                    {"Z", 25},
                    {"AA", 26},
                    {"AB", 27},
                    {"AC", 28},
                    {"AD", 29},
                    {"AE", 30},
                    {"AF", 31},
                    {"AG", 32},
                    {"AH", 33},
                    {"AI", 34},
                    {"AJ", 35},
                    {"AK", 36},
                    {"AL", 37},
                    {"AM", 38},
                    {"AN", 39},
                    {"AO", 40},
                    {"AP", 41},
                    {"AQ", 42},
                    {"AR", 43},
                    {"AS", 44},
                    {"AT", 45},
                    {"AU", 46},
                    {"AV", 47},
                    {"AW", 48},
                    {"AX", 49},
                    {"AY", 50},
                    {"AZ", 51},
                    {"BA", 52},
                    {"BB", 53},
                    {"BC", 54},
                    {"BD", 55},
                    {"BE", 56},
                    {"BF", 57},
                    {"BG", 58},
                    {"BH", 59},
                    {"BI", 60},
                    {"BJ", 61},
                    {"BK", 62},
                    {"BL", 63},
                    {"BM", 64},
                    {"BN", 65},
                    {"BO", 66},
                    {"BP", 67},
                    {"BQ", 68}
                };

            if (indexList.ContainsKey(colname))
                return indexList.First(x => x.Key.Equals(colname)).Value;
            return -1;
        }
于 2012-05-02T18:34:49.580 回答