?
Solved

Read Excel documents with C#

Posted on 2009-07-07
6
Medium Priority
?
581 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

762 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