Question posted 2009 · +10 upvotes
How would I read from an Excel sheet and load the marked selection (Area) into an multidimensional array? A column in Excel could itself be a multi dimensional array since it would contain more than just one value.
The idea (not sure how good or bad this is) is right now is to do a for loop through all the Excel.Area (selected fields) and add the content of that field to the multi dimensional array. Since the multi dimensional array is of type object[,] and therefore non-generic there is no convenient add() method to it. All of it needs to be done manually.
Any idea if this approach is ok or if it could be done more efficientlty?
Many Thanks,
Accepted answer +13 upvotes
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.
Excel VBA objects referenced (5)
Application— Using events with the Application objectApplication— Working with Other ApplicationsMSExcel.Range— Refer to Cells by Using a Range ObjectMSExcel.Range— Delete Duplicate Entries in a RangeMSExcel.Workbook— Workbooks and Worksheets
Top excel Q&A (6)
- Shortcut to Apply a Formula to an Entire Column in Excel +335 (2011)
- How should I escape commas and speech marks in CSV files so they work in Excel? +136 (2012)
- Convert xlsx to csv in linux command line +96 (2012)
- How to create a link inside a cell using EPPlus +50 (2011)
- IF statement: how to leave cell blank if condition is false ("" does not work) +44 (2013)
- T-SQL: Export to new Excel file +44 (2012)
excel solutions on this site
.