title: Excel spreadsheet row cells out of order when read with OPENXML
body: <p>I have a requirement to read an excel document and place the data contained in the cells in the database. However, I problem I notice is that when I got to read the data out of the rows, they do not come out in the order they appear in the form. How do I work this out please?</p>
  
  <pre><code>public void getrowdata(){
  
   IEnumerable&lt;Row&gt; dataRows = from row in s.worksheetpart.Worksheet.Descendants&lt;Row&gt;()
                                                  where row.RowIndex &gt; 6
                                                  select row;
  
  // extract the data in the row in order 
                  foreach (Row row in dataRows)
                  { 
                      var cellValues = from cell in row.Descendants&lt;Cell&gt;()
                                       select ((cell.CellValue != null &amp;&amp; cell.DataType != null &amp;&amp; cell.DataType.HasValue)
                                           &amp;&amp; (sharedString.HasChildren &amp;&amp; int.Parse(cell.CellValue.InnerText) &lt; sharedString.ChildElements.Count)
                                           ? sharedString.ChildElements[int.Parse(cell.CellValue.InnerText)].InnerText
                                           : ((cell.CellValue != null &amp;&amp; cell.CellValue.InnerText != null) ? cell.CellValue.InnerText : String.Empty));
  
  
  //--cellValues.toArray() and then access each cell via index in array
  
  
  }
  
    public void ReadDSheetsToBuffer()
          {
              try
              { 
                  //Open the Excel workbook.
                  using (SpreadsheetDocument document = SpreadsheetDocument.Open(file.FullName, true))
                  {
                      //References to the workbook and Shared String Table.
                      workBook = document.WorkbookPart.Workbook;
                      workSheets = workBook.Descendants&lt;Sheet&gt;();
                      sharedStrings = document.WorkbookPart.SharedStringTablePart.SharedStringTable; 
                      ExtractSheetstoMemory2(document);
  
                  }
              }
              catch (Exception ex)
              {
                  throw ex.GetBaseException();
              }
          }
  </code></pre>
  
  <p>Sample File found at <a href="http://www.filedropper.com/dcttestdata" rel="nofollow noreferrer">Sample Excel File I read with the code</a></p>
  
  <p>And below is the type of way I access the values stored up in the cells in the row. . . </p>
  
  <pre><code> if (values[228] != null)
                  itemdetail.Custom1 = rowvalues[228].Trim();
              if (values[229] != null)
                  itemdetail.Custom2 = rowvalues[229].Trim();
              if (values[230] != null)
                  itemdetail.Custom3 = rowvalues[230].Trim();
              if (values[231] != null)
                  itemdetail.Custom4 = rowvalues[231].Trim();
              if (values[232] != null)
                  itemdetail.Custom5 = rowvalues[232].Trim();
              if (values[233] != null)
                  itemdetail.Custom6 = rowvalues[233].Trim();
  </code></pre>
  
  <p>My Attempt at using cell reference to access the cell innertext</p>
  
  <pre><code>foreach (Row row in dataRows)
  {
      if (row.RowIndex &gt; 6)
      {
          String theCell = row.Descendants&lt;Cell&gt;().Where(c =&gt; c.CellReference == ExcelColumnFromNumber(1) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
          String theCell2 = row.Descendants&lt;Cell&gt;().Where(c =&gt; c.CellReference == ExcelColumnFromNumber(2) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
          String theCell3 = row.Descendants&lt;Cell&gt;().Where(c =&gt; c.CellReference == ExcelColumnFromNumber(3) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
          String theCell4 = row.Descendants&lt;Cell&gt;().Where(c =&gt; c.CellReference == ExcelColumnFromNumber(4) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
          String theCell5 = row.Descendants&lt;Cell&gt;().Where(c =&gt; c.CellReference == ExcelColumnFromNumber(5) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
          String theCell6 = row.Descendants&lt;Cell&gt;().Where(c =&gt; c.CellReference == ExcelColumnFromNumber(6) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
          String theCell7 = row.Descendants&lt;Cell&gt;().Where(c =&gt; c.CellReference == ExcelColumnFromNumber(7) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
          String theCell8 = row.Descendants&lt;Cell&gt;().Where(c =&gt; c.CellReference == ExcelColumnFromNumber(8) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
          String theCell9 = row.Descendants&lt;Cell&gt;().Where(c =&gt; c.CellReference == ExcelColumnFromNumber(9) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
          String theCell10 = row.Descendants&lt;Cell&gt;().Where(c =&gt; c.CellReference == ExcelColumnFromNumber(10) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
          String theCell11 = row.Descendants&lt;Cell&gt;().Where(c =&gt; c.CellReference == ExcelColumnFromNumber(11) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
          String theCell112 = row.Descendants&lt;Cell&gt;().Where(c =&gt; c.CellReference == ExcelColumnFromNumber(12) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
          String theCell13 = row.Descendants&lt;Cell&gt;().Where(c =&gt; c.CellReference == ExcelColumnFromNumber(13) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
          String theCell14 = row.Descendants&lt;Cell&gt;().Where(c =&gt; c.CellReference == ExcelColumnFromNumber(14) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
          String theCell15 = row.Descendants&lt;Cell&gt;().Where(c =&gt; c.CellReference == ExcelColumnFromNumber(15) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
      }
  }
  </code></pre>