Solved

Read Excel documents with C#

Posted on 2009-07-07
6
578 Views
Last Modified: 2012-05-07
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?
0
Comment
Question by:Veljean
  • 2
  • 2
6 Comments
 
LVL 5

Expert Comment

by:iUsername
ID: 24796478
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!
0
 
LVL 1

Author Comment

by:Veljean
ID: 24797277
There is not  another way to do this without open Excel?
0
 
LVL 5

Accepted Solution

by:
iUsername earned 250 total points
ID: 24797978
You can do it with IFilter, but it doesn't ALWAYS work very good...

Using IFilter you can extract text from files that you have IFilter implemented for them.
When you install office, MS also install an implementation of IFilter for office files.

You can get a small demo & source in C# that extracts the text in that article from code project:
http://www.codeproject.com/KB/cs/IFilter.aspx

As I wrote, it doesn't always work very good in excel.
Just one example, Duplicated cells are being saved once with pointers to these cells, if they are one after the other (I think this pattern is called "lightweight"), so using IFilter you will get this text only once, like it is in the file.

Anyway, that is the only way I know how to do it without opening excel.
0
 
LVL 1

Author Comment

by:Veljean
ID: 24798353
ok thanks... i will check it out ;)
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 250 total points
ID: 24799323
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question