How to Read Excel Data Using LINQ to ExcelProvider in .NET

No.of Views1781
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  27 Dec 2010 08:12:02
Tag : LINQ , General
In this article I will show to how to read Excel work book data using LINQ to Excel provider in LINQ. The LINQ has many powerful ways to extend and access different type source. Let’s see how to implement this with Visual studio 2008 and .NET 3.5 Framework.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article I will show to how to read Excel work book data using LINQ to Excel provider in LINQ. The LINQ has many powerful ways to extend and access different type of data source. Let’s see how to implement this with Visual studio 2008 and .NET 3.5 Framework.

Implementation

Step 1

Let's create a simple excel sheet with few rows as looks like below,

Image Loading

Step 2

And then create a new console application using Visual studio 2008; project name is “LINQToExcel”. 

Image Loading

Step 3

Now add LINQToExcelProvider.cs in your project. You can download here Or I have included sample project end of this article you can download it.

Note:Thank SolidCoding

Step 4

If you are added LINQTOExcelProvider.cs, finally project structure looks like below, 

Image Loading

Step 5

Let's write code read the from source file and print screen.

C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinqToExcel;

namespace LINQToExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            ExcelProvider provider = ExcelProvider.Create(@".......\LINQ\How to read Excel data usign LINQ Provider\Source.xls", "Sheet1");
            Console.WriteLine("{0}\t{1}\t{2}\t{3}", "ID", "Name", "Address1", "Address2");
            foreach (ExcelRow row in (from x in provider select x))
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}", row.GetString(0),row.GetString(1),row.GetString(2),row.GetString(3));
            }

            Console.WriteLine("Done");
            Console.ReadLine();
        }
    }
}

Code Explanation

1.    The first line is creating object Provider using static Method for ExcelProvider.Create(). The Create method has to supply two arguments first one is source file path and second argument is sheet name.

ExcelProvider provider = ExcelProvider.Create(@".....\LINQ\How to read Excel data usign LINQ Provider\Source.xls", "Sheet1");

2.    Then I have print excel sheet data header in console using formatting

Console.WriteLine("{0}\t{1}\t{2}\t{3}", "ID", "Name", "Address1", "Address2");

3.    And then in the next line select data from the provider using LINQ and iterate each ExcelRow and print values from array in console

foreach (ExcelRow row in (from x in provider select x))
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}", row.GetString(0),row.GetString(1),row.GetString(2),row.GetString(3));
            }

4.    Finally print done for indicates successfully finished.

 

Step 6

Now run application and see output will be like followings, 

Image Loading

Download Sample Project

Download source files -28 kb

Conclusion

Through this article you have learned how to read Excel work book data using LINQ to Excel Provider. This is made easy to access or read excel sheet data easily in .NET. Hopes help and thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
    Deferred Execution executes the query only when Loop starts. What I mean here is that, we iterate through the query variable to get the result.
    Published Date : 15/Jan/2011
    In this article we will focus LINQ Remote and Local exaction such as This query executes on the server, Remote execution of query is default in LINQ, In Remote execution advantage of Databases index can be taken, Remote execution allows us to take the advantage of Database server engine, Remote execution allows us to only select particular rows from the table. This is very useful when we do have large amount of data in the server.
    Published Date : 07/Jan/2011
    In this article, we will see how to work with LINQ against IIS.
    Published Date : 10/Aug/2010
    how to create a XML tree using Functional Construction method of LINQ to XML.
    Published Date : 13/Apr/2010
    This article demonstrates how to use LINQ on DataTable, XML Data using LINQ to XML and SQL server data base using LINQ to SQL Classes.
    Published Date : 28/Jun/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top