1

I'm stuck in an easy task: convert an multidimensional Array (Object[,]) in a monodimensional string[].

Long story short: I'm reading an excel file using a com library which returns a dynamic type casted to Object[,] at runtime.

SortedSet<string> rows = new SortedSet<string>();
for (int i = 3; i <= 5; i++) 
{
    Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A"+i, "J"+i);
    Array cells = (Array) range.Cells.Value; // value return dynamic [,] -> Object[,] -> cast to Array[,]
    string[] rowCells = ConvertArrayToStringArray(cells); // 
    rows.Add(string.Join("\t", rowCells));
}
return string.Join("\r\n", rows);

Since Array[,] always have 1 row (I read the excel file row by row) I'd like to cast it to a simple string[] (or List) without using another loop in ConvertArrayToStringArray function.

I could read all cells in an action (not row by row like in code above), but I've no idea of how many rows are in excel file.

I appreciate any help

1 Answer 1

3
string[] rowCells = cells.Cast<string>().ToArray();
Sign up to request clarification or add additional context in comments.

3 Comments

This is the solution I was looking for! Thank you very much!
I'm ashamed, but I didn't know existance of ".Cast(<T>)".
@insilenzio, it's an extension method, part of Linq: msdn.microsoft.com/en-us/library/vstudio/bb341406.aspx

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.