Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Initially, I could not get the values from a spreadsheet that only has 2 columns and 2 rows, but I can get them now, but the range function is continuous. I have a textbox in my form to which the cursor stops blinking. I take this as a sign that something is still going on with my range function for my excel worksheet.

What I have tried:

This was my code prior to adding the stringIsNull methods
C#
ews = (Worksheet)ewb.Worksheets["Sheet1"];
int count = ews.Rows.Count;
string[] hashes = new string[count];

for (int row = 2; row <= count; row++)
{
    Range range = ews.Rows[row];

    foreach (Range r in range.Cells)
    {   
        if (r.Value2 != null)
        {
            // Break to check values
            MessageBox.Show(r.Value2);
        }
        else
            break;                    
    }            
}


This is what I added to get all the values from both rows under each column
C#
if (!string.IsNullOrWhiteSpace(r.Value2) || !string.IsNullOrEmpty(r.Value2))
{
    MessageBox.Show(r.Value2);
}
Posted
Updated 23-May-21 16:18pm

1 solution

Not sure if I understand your question correctly, but if you wonder why the function loops through all rows, it's because you have two nested loops and you break only from the first then the value is null.

If you only want to go through the range that has actually been used, try using Worksheet.UsedRange property (Excel) | Microsoft Docs[^] . Note that this will still return cells with no value if they are within range. For example if you have data on rows one and three the range also contains row two.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900