Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Read Excel documents with C#

Posted on 2009-07-07
6
Medium Priority
?
582 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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 1000 total points
ID: 24799323
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

610 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