cant read a string value from an excel cell?

  • Thread starter Thread starter aotemp
  • Start date Start date
A

aotemp

Hi,

Im having a reaaally hard time with something... Im trying to read a
cell of data into a String variable. It seems like such a simple task
too... Get the excel spreadsheet, get the workbook, get the sheet, get
the cell, get the cells data and store it into a string.

But it wont work! Im really frustrated. the best I got was
"System.___ComObject" coming out of the string.

Any ideas C# gurus?

Only I cannot seem to get it right! Please help!


this.openFileDialog1.FileName = "*.xls";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
InitializeComponent();
ExcelObj = new Excel.Application();

Excel.Workbook xwSpreadsheet = ExcelObj.Workbooks.Open("C:\\Documents
and Settings\\MTSUser\\My Documents\\Visual Studio Projects\
\WindowsApplication1\\bin\\Debug\\Countries.xls",0,false,
5,"","",true,Excel.XlPlatform.xlWindows,"",true,false,
0,false,true,false);

Excel.Sheets sheets = xwSpreadsheet.Worksheets;
Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
for (int i = 1; i <= 10; i++)
{
////////NONE OF THESE ARE WORKING :(
//String y = (worksheet.Cells[1,1]);
//Excel.Range range = worksheet.get_Range("A"+i.ToString(), "I" +
i.ToString());
//System.Array myvalues =
(System.Array)range.Cells.get_Value(range);
//string[] strArray = //ConvertToStringArray(myvalues);

//MessageBox.Show(strArray[1]);

}
}
 
Well, you have to get the actual Range object that corresponds to the
cell (you should be able to call the Range property on the Worksheet to give
you the appropriate object, which you might have to cast to a Range object).
Once you have that, you can get the value using the Value property.
 
Back
Top