How to read excel file using c#

Today I am going to show you how read excel file using c# and create folders based on excel file. Sometimes we may want to create many folders/sub folders based on business requirement and copy files into respective directory. So I am going to show how to do this using c# .net language. For this project i am using

  • Microsoft Visual Studio 2013
  • .net framework 4.5
  • ExcelDataReader library ( You can find it here)
  • Sample excel file which contain sample data set

Here is my project structure.

Project Structure

Sample output

Parent folders
Child folders

I have created sample console application to perform this task. I haven’t add exception handling for this code and feel free to optimize code and use it.

using Excel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FolderProject
    /// <summary>
    /// Author : Damith Wanninayake
    /// Excel 4.5 library url:
    /// </summary>
    class Program
        static void Main(string[] args)
            FileStream stream = File.Open(ConfigurationManager.AppSettings["sourceFile"], FileMode.Open, FileAccess.Read);
            string basePath = ConfigurationManager.AppSettings["basePath"];

            // Reading from a OpenXml Excel file (2007 format; *.xlsx)
            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

            // DataSet - Create column names from first row
            excelReader.IsFirstRowAsColumnNames = true;
            DataSet result = excelReader.AsDataSet();

            // Getting distinct values
            var distinctValues = result.Tables[0].AsEnumerable()
                        .Select(row => new
                            Mem = row.Field<double>("Mem#"),

You can find the completed project which contains excel file in here. Please note that i am not the author of the excel file.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.