Reading from Excel (Range into multidimensional Array) C#
You can read the value of Range as array:
using (MSExcel.Application app = MSExcel.Application.CreateApplication())
{
MSExcel.Workbook book1 = app.Workbooks.Open( this.txtOpen_FilePath.Text);
MSExcel.Worksheet sheet = (MSExcel.Worksheet)book1.Worksheets[1];
MSExcel.Range range = sheet.GetRange("A1", "F13");
object value = range.Value; //the value is boxed two-dimensional array
}
This code snippet is from .NET wrapper for MS Office. But same princip is in VSTO or VBA in MS Excel.
C# Excel Read to Multidimensional Array
This is because you are trying to cast values from an array that could contain strings, doubles, dates, etc to a single type. Instead, change the return type of your function from T[,] to a non-generic object [,] (and all the other bits in the function too, obviously). Then it should work.
start filling a two dimensional array from [0,0] in C#
Simply change the line:
myArray[cCnt, rCnt] = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
to:
myArray[cCnt - 1, rCnt - 1] = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
Related Topics
Launching Process in C# Without Distracting Console Window
Benefits of Using Async and Await Keywords
Count Number of Mondays in a Given Date Range
How to Check If Ioexception Is Not-Enough-Disk-Space-Exception Type
Raise an Event of a Class from a Different Class in C#
String.Equals() Not Working as Intended
Best Way to Dynamically Set an Appender File Path
Wpf Equivalent to Textrenderer
Get Just the Domain Name from a Url
How to Write a Transaction to Cover Moving a File and Inserting Record in Database
Write PDF Stream to Response Stream
Linq Query Group by and Selecting First Items
Date Difference in Years Using C#