How To Read Data From .CSV File In ASP.NET MVC

In This Article, I’m Going To Tell You How To Read Data From CSV File In MVC

So Let’s begin now.
Step 1
Create a new MVC Web Project.
Step 2
Install-Package Lumen CSV Reader Package From Nuget Package
Step 3
Create A New “Controller\HomeController.cs” File And Add The Following Method Inside.
Step 4
Add Namespace “using LumenWorks.Framework.IO.Csv;”
using LumenWorks.Framework.IO.Csv;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ReadCSVFileMvc.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Upload(HttpPostedFileBase upload)
        {
            if (ModelState.IsValid)
            {

                if (upload != null && upload.ContentLength > 0)
                {

                    if (upload.FileName.EndsWith(".csv"))
                    {
                        Stream stream = upload.InputStream;
                        DataTable csvTable = new DataTable();
                        using (CsvReader csvReader =
                            new CsvReader(new StreamReader(stream), true))
                        {
                            csvTable.Load(csvReader);
                        }
                        return View(csvTable);
                    }
                    else
                    {
                        ModelState.AddModelError("File", "This file format is not supported");
                        return View();
                    }
                }
                else
                {
                    ModelState.AddModelError("File", "Please Upload Your file");
                }
            }
            return View();
        }
    }
}

Step 5

Add View For An Upload Action And Put Following Code:

@model System.Data.DataTable
@using System.Data;

<h2>Upload File</h2>
<style>
table, th, td {
  border: 1px solid black;
}
</style>
@using (Html.BeginForm("Upload", "Home", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()

    <div class="form-group">
        <input type="file" id="dataFile" name="upload" />
    </div>

    <div class="form-group">
        <input type="submit" value="Upload" class="btn btn-default" />
    </div>

    if (Model != null)
    {
        <table>
            <thead>
                <tr>
                    @foreach (DataColumn col in Model.Columns)
                    {
                        <th>@col.ColumnName</th>
                    }
                </tr>
            </thead>
            <tbody>
                @foreach (DataRow row in Model.Rows)
                {
                    <tr>
                        @foreach (DataColumn col in Model.Columns)
                        {
                            <td>@row[col.ColumnName]</td>
                        }
                    </tr>
                }
            </tbody>
        </table>
    }
}

Output:-

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories