problems with reading cells in excel with C#

D

David Krmpotic

hi!!

I use the following code to operate excel document:


Excel.Application excelApp = new Excel.ApplicationClass();
//excelApp.Visible = true;
string workbookPath = "c:\\eltina.xls";
Excel.Workbook excelWorkbook =
excelApp.Workbooks.Open("eltina.xls",0,false,5,"","",false,Excel.XlPlatform.
xlWindows,"\t",false,false,0,false);

//Workbooks.Open(workbookPath, 0, false, 5, "", "", false,
Excel.XlPlatform.xlWindows, "",true, false, 0, true, false, false);
//this is from http://www.codeproject.com/csharp/csharp_excel.asp, but it
doesn't compile in my VS.NET 2003 ?? it says "there is no overload method
which accepts 15 parameters

Excel.Sheets excelSheets = excelWorkbook.Worksheets;
string currentSheet = "List1";
Excel.Worksheet excelWorksheet =
(Excel.Worksheet)excelSheets.get_Item(currentSheet);
Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "A1");


I get this error:
-------------------------------------------------------
An unhandled exception of type 'System.Runtime.InteropServices.COMException'
occurred in BorisExcel.exe

Additional information: 'eltina.xls' could not be found. Check the spelling
of the file name, and verify that the file location is correct.

If you are trying to open the file from your list of most recently used
files on the File menu, make sure that the file has not been renamed, moved,
or deleted.
-------------------------------------------------------

and it happens at line with: excelApp.Workbooks.Open(..

But file *does* exist! Please help.. I've already spent too much time in
opening one simple excel document trying to read cells content from
worksheet.

Thank you sooo much !


David
 
D

David Krmpotic

SOLVED!

I realized that I wasn't really using the variable workbookPath , so the
path was wrong.. But I don't know if it's only this.. I think that it was
also that I didn't release excel object and old instance of excel was
holding the file or something.. It doesn't really matter.. now it WORKS!!!
so here is ful working example if someone needs it!!

Excel.Application excelApp = new Excel.ApplicationClass();
//excelApp.Visible = true;
string workbookPath = "c:\\eltina.xls";
Excel.Workbook excelWorkbook =
excelApp.Workbooks.Open(workbookPath,0,false,5,"","",false,Excel.XlPlatform.
xlWindows,"\t",false,false,0,false);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
string currentSheet = "List1";
Excel.Worksheet excelWorksheet =
(Excel.Worksheet)excelSheets.get_Item(currentSheet);
Excel.Range excelCell =
(Excel.Range)excelWorksheet.get_Range("C4", "C4");
MessageBox.Show((string)excelCell.Value);
Marshal.ReleaseComObject(excelApp);
excelApp = null;
GC.Collect();
 

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