title: Open XML get cells by Column index C#
body: <p>I have found the index of the columns that I want to put into a string to be added to a data grid. I am not sure how to use the index value in Open XML to find the cells that I want.</p>
  
  <pre><code>        bool headerIndexComplete = false;
          int a1141Index = -1;
          int NacIndex = -1;
  
          string fileName = "FilePathHere";
  
          using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
          {
              using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fs, false))
              {
                  WorkbookPart workbookPart = doc.WorkbookPart;
                  SharedStringTablePart sstpart = workbookPart.GetPartsOfType&lt;SharedStringTablePart&gt;().First();
                  SharedStringTable sst = sstpart.SharedStringTable;
  
                  WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
                  Worksheet sheet = worksheetPart.Worksheet;
  
                  //var cells = sheet.Descendants&lt;Cell&gt;();
                  var rows = sheet.Descendants&lt;Row&gt;();
  
                  foreach (Row row in rows)
                  {
                      int ColumnIndex = 0;
  
                      foreach (Cell Cell in row.Elements&lt;Cell&gt;())
                      {
                          int ssid = int.Parse(Cell.CellValue.Text);
                          string CellText = sst.ChildElements[ssid].InnerText;
  
                          if (!headerIndexComplete)
                          {
                              if (CellText == "1141")
                                  a1141Index = ColumnIndex;
  
                              if (CellText == "CHARGE GROUP (NAC'S)")
                                  NacIndex = ColumnIndex;
  
                              ColumnIndex++;
                          }                         
                      }         
                  }
              }
          }
      }
  </code></pre>