Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Excel in C# to transfer Cell Values to a 2D string array
    primarykey
    data
    text
    <p>I need someone to create a method that will get me all the cell values in an excel spreadsheet into a 2D array.</p> <p>I'm making it using ribbons in C# to work with Excel but i just can't get it to work.</p> <pre><code> private string[,] GetSpreadsheetData () { try { Excel.Application exApp = Globals.TSExcelAddIn.Application as Excel.Application; Excel.Worksheet ExWorksheet = exApp.ActiveSheet as Excel.Worksheet; Excel.Range xlRange = ExWorksheet.get_Range("A1","F188000"); object[,] values = (object[,])xlRange.Value2; string[,] tsReqs = new string[xlRange.Rows.Count, 7]; char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); for (int i = 0; i &lt; tsReqs.GetLength(0); i++) { for (int z = 0; z &lt; tsReqs.GetLength(1); z++) { if(values[i+1,z+1] != null) tsReqs[i, z] = values[i + 1, z + 1].ToString(); } } return tsReqs; } catch { MessageBox.Show ("Excel has encountered an error. \nSaving work and exitting"); return null; } } </code></pre> <p>Also if anyone has a more efficient way of doing this I would greatly appreciate it.</p> <pre><code> Excel.Range xlRange = ExWorksheet.get_Range("A1","F188000"); </code></pre> <p>Reads all the way till F188000 cell from A1, I just want it to keep reading until it reaches a row with absolutely no data.</p> <pre><code> - Caught: "Index was outside the bounds of the array." (System.IndexOutOfRangeException) Exception Message = "Index was outside the bounds of the array.", Exception Type = "System.IndexOutOfRangeException" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload