What is the best way to read an XLS file?

12

I would like to know the best way to read an XLS file in C #.

Can I treat it the same way I treat a table in the database?

    
asked by anonymous 12.05.2014 / 19:44

2 answers

14

What's the best way I can not say it, because what's best for one case can be worse for another. I can give you options.

In addition to the method already mentioned by Reiksiel, there are some other options:

There is the EPPlus library that is one of those used for this function.

You can use the Excel Data Reader library that is an open source project that does the hard work for you. I've never used it but there are a lot of people who use it.

There's also excellibrary . I have no further information.

There is also the Free .NET Excel API which [is a "covert" version "of commercial software. Just to test it out and see if it's worth buying.

Using LINQ is very important to you, you have Linq to Excel . I do not know anyone using it.

There are a few ways to treat spreadsheets like XML. A example in SO .

Some of these solutions only work with XLSX. I do not know if your case is strictly for XLS.

As you have not given parameters, I can say that there are several commercial components of several well-known companies in the market. A simple survey takes you to them.

I will not mention the more "nuts" solutions:)

    
12.05.2014 / 21:29
10

There is the option to use OleDB and read the Excel file as a table in a database ...

An example .....

string con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp\test.xls;Extended Properties='Excel 8.0;HDR=Yes;'"
using(OleDbConnection connection = new OleDbConnection(con))
{
    connection.Open();
    OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection) 
    using(OleDbDataReader dr = command.ExecuteReader())
    {
         while(dr.Read())
         {
             var row1Col0 = dr[0];
             Console.WriteLine(row1Col0);
         }
    }
}

This example uses Microsoft.Jet.OleDb.4.0 provider to open and read an Excel file. However, if the file is of type xlsx (from Excel 2007 and later versions), then you need to download the Microsoft Data Access components and install it on the machine. The provider will be Microsoft.ACE.OLEDB.12.0; . It is not necessary to have Office on the machine.

Source: Here

    
12.05.2014 / 20:52