views:

1572

answers:

4

Hello everyone,

I have experience in C# but limited experience in using C# to read content from Excel. My task is very simple, just read each column of each row of an Excel document and retrieve their values.

Any good tutorials or samples for a beginner? I am using VSTS 2008 + C# + .Net 3.5.

I am working with Excel 2007.

thanks in advance, George

+1  A: 

Check out this link.

Based on your description it is enough, but if you need to create an Add-in I would just look at VSTO. just google/bing it, rather easy :)

ArielBH
Looks like no read sample, all of them are how to write? :-)
George2
+2  A: 

If you just need to read an excel document I'll recommend this blog post by David Hayden. I used it as a primer when I moved from excel automation to ADO.NET

Kasper
Thanks! This is just what I need. A further question, about using C# to deal with Excel, do you have any other good resources, like forum or books to recommend?
George2
Hi George2,No, not really. But try to have a look at some of the projects on codeplex, that is how I dig into a new subject, by reading other peoples code :-)
Kasper
+2  A: 

Format of Excel 2007 files isn't straigtforward. Getting a text value of a cell using Open XML Format SDK 2.0 requires a lot of actions. If you're not going to use third party libraries, which don't know about, you have to get deeply into this SDK. There are tutorials, but I dont't know easy solution even for your simple task.

Dmitry Tashkinov
Are there any easy to use wrapper or libraries to manipulate Excel?
George2
What the others in this topic suggest must be easier, but I suspect their solutions require Excel installed on the machine running the program and also not sure if they will work with Excel 2007. I you don't find easy solution, I can make copy and paste of my C# program code working with Excel 2007 files.
Dmitry Tashkinov
+1  A: 

SpreadsheetGear for .NET will do it. Here is a simple example in a C# console application:

using System;
using SpreadsheetGear;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load Book.xlsx.
            IWorkbook workbook = Factory.GetWorkbook(@"c:\Book.xlsx");
            // Write the address and formatted text value of each
            // cell to the console.
            foreach (IRange cell in workbook.Worksheets[0].UsedRange)
                Console.WriteLine("{0}='{1}'", cell.Address, cell.Text);
        }
    }
}

You can download a free trial here and try it yourself.

Disclaimer: I own SpreadsheetGear LLC

Joe Erickson