1

I wanted to add Windows Forms controls to an excel document I created.

To do so, I created a new project using the Office 2010 Excel 2010 Workbook template in VS 2010 (C#). All I am trying to do is copy data from multiple cells and write them to a text file.

Whenever I put all of the cells I want under a single Name Space, and then try to read the "Value2", I just get "System.Object[,]".

All I have done so far is added a radioButton to Sheet 1 and created an event handler. Past that I am not sure what to do.

1 Answer 1

3

The Value2 property of a range comes back as a 2-d array of values. To access the values just loop through the array:

object[,] values = range.Values2;
for(int i = 0; i < values.Length(0); i++)
    for(int j = 0; j < values.Length(1); j++)
    {
        object cellValue = values[i,j];
        // do something with the value
    }
Sign up to request clarification or add additional context in comments.

2 Comments

@D Stanley When I use this code, I get syntax errors stating "cannot implicitly convert type 'object' to 'object[,]'". Also I'm dealing with just numbers. I'm copying hex-decimal values from the cells so I'm not sure if cycling through will just work.
You'll need to cast to a double[,] and may be able to cast the cell values to doubles. The code I gave is a rough guideline - you'l need to adapt it to meet your specific needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.