473,383 Members | 1,739 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,383 software developers and data experts.

Working with Excel

HI all,

I'm new to C#.
I want to open an excel document and work with it in C#.
I currently have Excel2000, I think it is version9.0. But Clients might have
office XP, XP2003 on
Could someone be so jind as to show me some code or point me in the right
direction.

Thanks
Robert
Jun 27 '06 #1
1 12605
Hi Robert, here you have a bit:

Microsoft.Office.Interop.Excel.Application app = new
Microsoft.Office.Interop.Excel.Application();

Microsoft.Office.Interop.Excel.Workbook book = null; //to access to book
Microsoft.Office.Interop.Excel.Worksheet sheet = null; //to access to sheet
Microsoft.Office.Interop.Excel.Range range = null; //to access to a range of
data "A1:B2"
book = app.Workbooks.Open("c:\\test.xls", Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);

sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.Wor ksheets.get_Item[1];

range = sheet.get_Range("A1","B5");

Only take care about release resources from excel. Use ReleaseComObject
foreach com object that you use.

System.Runtime.InteropServices.Marshal.ReleaseComO bject(range);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(book);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(app);

Hope this helps.
Regards.
Josema
"Robert Bravery" wrote:
HI all,

I'm new to C#.
I want to open an excel document and work with it in C#.
I currently have Excel2000, I think it is version9.0. But Clients might have
office XP, XP2003 on
Could someone be so jind as to show me some code or point me in the right
direction.

Thanks
Robert

Jun 27 '06 #2

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

Similar topics

3
by: Otie | last post by:
I found the following under the GetObject help notes and in the example for GetObject: "This example uses the GetObject function to get a reference to a specific Microsoft Excel worksheet...
6
by: Brent | last post by:
Having used every combonation of excel 9/10/11 and office 10/11 possible as references i still can not create an excel object! Any ideas? Just using this simple bit below i get "Excel.appication...
2
by: saeed rezaei | last post by:
hi i am saeed rezaei srezaii2000@yahoo.com who is knowh how i can changing the excel cells backcolor. now i can working whit excel but still i cant change the backcolor of cells.
3
by: Michael Kellogg | last post by:
My application works with Excel to put together spreadsheets for reporting purposes. It runs just fine on my box, but when I put it on another one, it bombs. Here is the relevant code: ...
3
by: jeremy.bird | last post by:
I am looking for a possible solution or bump in the right direction when working with excel on the web. Currently I am using and developing in visual studio 2005. Here is the situation I have a lot...
6
by: AleydisGP | last post by:
I have a .plt file (which is a tab delimited ASCII file) and I want to format it to get a .dbf with data in rows and columns, detele some rows/columns and substitute decimal '.' with ','. All this...
0
by: shantanu | last post by:
I am trying to convert a macro code to c# that will copy the values of a column and paste to anather through paste special. Everything is working fine but the transpose meathod to paste the column...
0
by: mustaqueahmad | last post by:
Hi All, I am getting export to excel problem when I am giving IPAddress in URL, but it is working correct when am giving localhost and page name. eg : when I am writing My machine IP...
2
by: deepadevid | last post by:
Hi, when I tried to copy a worksheet programmatically in Excel 2007 the chartObjects are not getting copied. I am getting empty charts inside my new worksheet. This is working in Excel 2003. ...
4
by: stephen | last post by:
Hi all, I am working with Excel. I read an excel document using ExcelReader and lets say it has 10 columns. I have to read each record and based on a specified column peform some activites and...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.