3

I'm using ClosedXML elsewhere in my script where I'm iterating through every row like this and it works.

var workbook = new XLWorkbook(ObjectRepPath);
var rows     = workbook.Worksheet(1).RangeUsed().RowsUsed().Skip(1);

foreach (var row in rows)
{
    objPage    = row.Cell(1).GetString();
    objElement = row.Cell(2).GetString();

    if (objPage == page && objElement == element)
    {
        locType = row.Cell(3).GetString();
        locParm = row.Cell(4).GetString();
    }
}

After that I need to pull the data from the cells in a randomly selected row. Here's what I've got so far, which is not working...

var workbook = new XLWorkbook(extFile);
var ws       = workbook.Worksheets.Add("Cell Values");
var rnd      = new Random();
int rowNum   = rnd.Next(2, workbook.Worksheet(1).RangeUsed().RowsUsed().Count());
var dataRow  = ws.Row(rowNum);

string dangit = dataRow.Cell(1).GetString();
System.Diagnostics.Debug.WriteLine("Why is this dang thing not working... " + dangit);

Output: Why is this damn thing not working...

It just comes back empty. No error. Does anyone see something I don't?

ag93
  • 343
  • 4
  • 17
HeadlyvonNoggin
  • 313
  • 1
  • 3
  • 14
  • What is `datarow.Cell(1).Address`? And if you open the file in Excel, what is the value at that address? – Francois Botha Aug 23 '18 at 21:19
  • Address is random, so different each run. When I changed it to .Address and ran it it returned "C84". The value at C84 in the file is "AL". So it's a valid/populated field. – HeadlyvonNoggin Aug 23 '18 at 21:40
  • And what does `ws.Cell("C84").GetString()` return? Also an empty string? Does that cell contain a formula or a value? – Francois Botha Aug 24 '18 at 04:43
  • Yes, that also comes back with nothing. The actual C84 cell has the value of 'AL'. – HeadlyvonNoggin Aug 24 '18 at 13:19
  • Also, if it helps, ..... string testdata = cell.ToString(); .... returns "ClosedXML.Excel.XLCell" / string testdata = cell.Value.ToSting(); .... returns nothing – HeadlyvonNoggin Aug 24 '18 at 15:24

1 Answers1

3

Alright, I found the solution.

I changed the line ... var ws = workbook.Worksheets.Add("Cell Values");

to .... var ws = workbook.Worksheet(1);

and now this works .... Storage.StreetAddress = ws.Cell(xlRow, 1).GetString();

HeadlyvonNoggin
  • 313
  • 1
  • 3
  • 14