title: EPPlus: How to traverse through every content block (cell or merged range) of a worksheet?
body: <p>I'm trying to traverse through cells and merged cells inside a worksheet, and replace some template texts with dynamic values. However, I didn't manage to loop through all non-empty cells until now. Currently I tried this code, but it threw NullReferenceException when it tried to access the Text property of a cell which is merged.</p>
  
  <p>I'm using a template file, copying the template worksheet from there into my constructed workbook. I tried it with a single workbook without the copy, it gave the same result.</p>
  
  <p>I also tried to put the Where(cell => !cell.Merge) closure into the first foreach loop, but with the same result.</p>
  
  <pre><code>    using (var p = new ExcelPackage(new FileInfo(templateFile)))
      {
          var ws = _excel.Workbook.Worksheets.Add("Report", p.Workbook.Worksheets[sablonMunkafuzet]);
          foreach (ExcelRangeBase cell in ws.Cells)
          {
              if (string.IsNullOrEmpty(cell.Text)) continue;
              var s = cell.Text;
              if (s.StartsWith("^^"))
                   ProcessCell(cell, s.Substring(2));
          }
          foreach (string mc in ws.MergedCells)
          {
              var s = ws.Cells[mc].Text;
              if (s.StartsWith("^^"))
                   ProcessCell(ws.Cells[mc], s.Substring(2));
              }
          }
      }
  </code></pre>
  
  <p>EDIT: I would like to achieve that what I would do manually if I open that template in Excel. To find every "block" (I mean individual cells, or merged cell ranges) where a specifix text pattern appears, then process it, and change the value of that "block" to what I calculate.</p>