Read Data From Excel File (xls, xlsx, csv) In ASP.NET MVC (C#)

Reading Text or CSV File using Asp.Net C# and Store in Database or Display in Grid


Introduction : here in this progaram we are describe how to read data from exel(.csv or xlsx,xls file) using the Asp.net with c# code

Step 1 :  first create asp.net application

Step 2: create exel file (.csv) and give name "TestFile.csv"

Step 3 : After these step done need to do code in page load of code behind file as following

  protected void Page_Load(object sender, EventArgs e)
        {
            DataTable csvData = new DataTable();
            try
            {
                string csv_file_path = "C:\\Users\\Home\\Desktop\\TestFile.csv";
                using (TextFieldParser csvReader = new TextFieldParser( csv_file_path))
                {
                    csvReader.SetDelimiters(new string[] { "," });
                    csvReader.HasFieldsEnclosedInQuotes = true;
                    //read column names  
                    string[] colFields = csvReader.ReadFields();
                    foreach (string column in colFields)
                    {
                        DataColumn datecolumn = new DataColumn(column);
                        datecolumn.AllowDBNull = true;
                        csvData.Columns.Add(datecolumn);
                    }
                    while (!csvReader.EndOfData)
                    {
                        string[] fieldData = csvReader.ReadFields();
                        csvData.Rows.Add(fieldData);
                    }
                }
                 DataTable dt2 = new DataTable();
                 dt2 = csvData.AsEnumerable()
                 .GroupBy(x => x.Field<string>("Email"))
                 .Select(y => y.First())
                 .CopyToDataTable();

                 string final = string.Empty;
                foreach (DataRow row in dt2.AsEnumerable())
                {
                    var vv = csvData.Select("Email='"+ row["Email"]+"'").AsEnumerable();
                    string st = string.Empty;
                    st = "<table>";
                    st += "<tr> <th>Name</th>  <th>Order</th> <th> Product</th>  </tr>";
                    foreach (DataRow row2 in vv.AsEnumerable())
                    {
                        st += "<tr> <td>"+ row2["Name"] + "</td>  <td> " + row2["Order"] + "</td> <td> " + row2["Product"] + "</td>  </tr>";
                    }
                    st += "</table>";
                    final += st;
                }
                Label1.Text = final;

            }
            catch (Exception ex)
            {

            }
        }
if you read file from desktop then set file path like this :

string csv_file_path ="C:\\Users\\Home\\Desktop\\TestFile.csv";

(1) In this program we are first reading all data from exel file and store in datatable csvData

(2) and then after used second datatable dt2  to store data group by Email

(3) and then after using email we are separate order data in html table

.Csv File data (TestFile.csv):

Exel data


OutPut Data :

Read data from exel file

Hi, friend if you like this program or article than give comment in gelow comment box or if you have any query reguarding this program feel free to comment

Previous
Next Post »