cant read a string value from an excel cell?

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]);

}
}
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top