473,378 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

problems with reading cells in excel with C#

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
Nov 22 '05 #1
2 5989
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();

Nov 22 '05 #2
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();

Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David Krmpotic | last post by:
hi!! I use the following code to operate excel document: Excel.Application excelApp = new Excel.ApplicationClass(); //excelApp.Visible = true; string workbookPath = "c:\\eltina.xls";...
6
by: Paul | last post by:
I was wondering if anyone has had an issue where using vba code to read an excel file and import the data into an access table some records are not imported from the excel file. It seems looking at...
1
by: Jan Olof Jonsson | last post by:
Hello. We hawe a problem with the Excel-program on a public webserver. Excel is installated to convert/produce HTML - documents to a website on the server when client users in a webinterface...
2
by: Praveen K | last post by:
I have a problem in communicating between the C# and the Excel Interop objects. The problem is something as described below. I use Microsoft Office-XP PIA dll’s as these dll’s were been...
5
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column...
13
by: nuti | last post by:
Hi all, I am fairly new to VB.I am trying to figure out as how to write a script so that i can read the data from an excel sheet to Access. can u guys please help me out? cheers, nuti
1
by: mgoold2002 | last post by:
I'm moving some code from Excel vbs into a .NET context and I'm very new to .NET. I'm trying to make this Excel vb code work in .NET. My questions are: how do I reference cells correctly using...
3
by: Bharathi | last post by:
Hi, I got strucked with reading date value from excel file using C#.NET. For Jan-2000 the value I am getting is 36526.0. For all other dates also I am getting some double value like this. ...
2
by: DvsStyle | last post by:
Firstly i should mention that Im relativey New to VB, and programming in general - Only starting in the last 12 months or so for school, So plz dont take anything for granted and act like i know...
1
by: ShafeeqRahman | last post by:
I am not getting the value of some excel cells while reading it from vb6. The code is as follows. The cells has some numeric values. Is it the problem of the format of the cells in excel. When I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.