4

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.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
George2
  • 44,761
  • 110
  • 317
  • 455
  • 1
    First of all specify your Excel version. In Office 2007 file formats completely changed and so there are different facilities of working with files for Office 97-2003 and Office 2007. – Dmitry Tashkinov Jun 21 '09 at 09:28
  • I need to work with Excel 2007. Thanks. Any good samples for me to learn how to read? – George2 Jun 21 '09 at 09:31

3 Answers3

2

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
  • 1,976
  • 19
  • 16
  • Are there any easy to use wrapper or libraries to manipulate Excel? – George2 Jun 21 '09 at 10:28
  • 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 Jun 21 '09 at 15:32
1

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
  • 1,991
  • 3
  • 22
  • 38
1

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
  • 7,077
  • 1
  • 31
  • 31