Link to home
Start Free TrialLog in
Avatar of Veljean
Veljean

asked on

Read Excel documents with C#

Hi Everybody

I want to read a xsl document from my C# application, i need a string concatenated for each row in the selected sheet,  can give me tips to do this please?
Avatar of iUsername
iUsername

add a new reference to the assembly:
Microsoft.Office.Interop.Excel

Now you have a new namespace: Microsoft.Office.Interop.Excel (for short I will call it "Excel")

Create a new application object
Excel.Application appExcel = new Excel.ApplicationClass();

Now a new excel is running in the background.
You can open new worksheets with:
Excel.Worksheets ws = appExcel.Worksheet;
ws .open().

You can find all the documentations here:
http://msdn.microsoft.com/de-de/library/microsoft.office.interop.excel%28en-us%29.aspx


VERY IMPORTANT:
These classes are COM classes, therefore you *MUST* release them before quitting (using quit() method) the excel application.
If you don't release all the object, the excel.exe will simply won't quit.

To do that you need to use:
Marshal.ReleaseComObject()
(http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.releasecomobject.aspx)

As an example,
lets say that you're using appExcel and ws that I wrote above, than you will have to:
appExcel.quit();
Marshal.ReleaseComObject(appExcel);
Marshal.ReleaseComObject(ws);

Good Luck!
Avatar of Veljean

ASKER

There is not  another way to do this without open Excel?
ASKER CERTIFIED SOLUTION
Avatar of iUsername
iUsername

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Veljean

ASKER

ok thanks... i will check it out ;)
SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial