Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's an implementation of <code>IEnumerable</code> that should do what you want, compiled and unit tested.</p> <pre><code> ///&lt;summary&gt;returns an empty cell when a blank cell is encountered ///&lt;/summary&gt; public IEnumerator&lt;Cell&gt; GetEnumerator() { int currentCount = 0; // row is a class level variable representing the current // DocumentFormat.OpenXml.Spreadsheet.Row foreach (DocumentFormat.OpenXml.Spreadsheet.Cell cell in row.Descendants&lt;DocumentFormat.OpenXml.Spreadsheet.Cell&gt;()) { string columnName = GetColumnName(cell.CellReference); int currentColumnIndex = ConvertColumnNameToNumber(columnName); for ( ; currentCount &lt; currentColumnIndex; currentCount++) { yield return new DocumentFormat.OpenXml.Spreadsheet.Cell(); } yield return cell; currentCount++; } } </code></pre> <p>Here are the functions it relies on:</p> <pre><code> /// &lt;summary&gt; /// Given a cell name, parses the specified cell to get the column name. /// &lt;/summary&gt; /// &lt;param name="cellReference"&gt;Address of the cell (ie. B2)&lt;/param&gt; /// &lt;returns&gt;Column Name (ie. B)&lt;/returns&gt; public static string GetColumnName(string cellReference) { // Match the column name portion of the cell name. Regex regex = new Regex("[A-Za-z]+"); Match match = regex.Match(cellReference); return match.Value; } /// &lt;summary&gt; /// Given just the column name (no row index), /// it will return the zero based column index. /// &lt;/summary&gt; /// &lt;param name="columnName"&gt;Column Name (ie. A or AB)&lt;/param&gt; /// &lt;returns&gt;Zero based index if the conversion was successful&lt;/returns&gt; /// &lt;exception cref="ArgumentException"&gt;thrown if the given string /// contains characters other than uppercase letters&lt;/exception&gt; public static int ConvertColumnNameToNumber(string columnName) { Regex alpha = new Regex("^[A-Z]+$"); if (!alpha.IsMatch(columnName)) throw new ArgumentException(); char[] colLetters = columnName.ToCharArray(); Array.Reverse(colLetters); int convertedValue = 0; for (int i = 0; i &lt; colLetters.Length; i++) { char letter = colLetters[i]; int current = i == 0 ? letter - 65 : letter - 64; // ASCII 'A' = 65 convertedValue += current * (int)Math.Pow(26, i); } return convertedValue; } </code></pre> <p>Throw it in a class and give it a try.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload