Solved

Read Excel documents with C#

Posted on 2009-07-07
6
575 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 69

Assisted Solution

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
getting id from database 5 24
TT Text To Column Based On Criteria 3 18
VBA in SharePoint 3 17
Macro to delete column 3 8
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
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 Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now