Im trying to iterate through worksheets from a work book and basically place each of the cell values into a list box. Here is the code i have so far but i either get null errors or Unable to cast object of type 'System.String' to type 'System.Object[,]'.
using
namespace
{
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Alias = Microsoft.Office.Interop.Excel;
using System.Collections;EXpublic partial class Form1 : Form
{
{
InitializeComponent();
}
{
Alias.Scansheets(workbook);
private void Scansheets(Workbook wookBookIn)
int numsheets = wookBookIn.Sheets.Count;
for (int sheetNum = 1; sheetNum < (numsheets); sheetNum++)
Worksheet sheet = (Worksheet)wookBookIn.Sheets[sheetNum];
Range excelRange = sheet.UsedRange;
object[,] valArray = (object[,])excelRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);
foreach (object value in valArray)
if (value != null)
else
{listBox1.items.add(value);
Hope you can help. it's driving me crazy
Thanks
Anthony

VulpesPosted Feb 6, 2012, 6:50 AM
Anthony ClarkePosted Feb 8, 2012, 4:09 AM
Thanks for your help but the 2nd question was part of my original question. It was only that Vulpes answer completely answered my question so i marked as correct.
But still i thank you for your response and I am grateful.
Regards
Anthony
Sam HobbsPosted Feb 6, 2012, 2:13 PM
Anthony ClarkePosted Feb 6, 2012, 7:39 AM
Yeah your right, i used for (int sheetNum = 1; sheetNum < (numsheets + 1); sheetNum++)
Works Perfectly :) Didnt think about testing to see whether it was the object that was NULL lol
Anthony ClarkePosted Feb 6, 2012, 1:48 AM
Any Ideas?
Sam HobbsPosted Feb 5, 2012, 5:15 PM