In this tutorial, I am going to show you how to Display CSV Data in ASP.NET MVC in a tabular format. The data from the CSV file will be parsed and Display in the Tabular format using Jquery.

Introduction:

Here I am going to develop a simple example in ASP.NET MVC that will show CSV(Comma Separated Value) data in the Grid format using Jquery.

Display CSV Data in ASP.NET MVC:

Step 1- Create an ASP.NET MVC project DisplayCSVDataInGrid.

Display CSV Data

Choose and Empty Solution and Proceed. An Empty application will be created.

Step 2- Right Click on the Model folder and add a class called Employee. Write the following code to Employee.cs file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DisplayCSVDataInGrid.Models
{
public class Employee
{
public string EmployeeID { get; set; }
public string EmployeeName { get; set; }
public string Designation { get; set; }
public string Skill { get; set; }

}
}

Step 3- Now right click on the Controller folder and Create a Controller called HomeController. HomeController with the following code will be created.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DisplayCSVDataInGrid.Models;
namespace DisplayCSVDataInGrid.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/

public ActionResult Index()
{
return View();
}



}
}

Step 4- Add the following JSON method in HomeController.

public JsonResult GetCSVData()
{
string path = Server.MapPath("~/Content/Employee.csv");
string csvData = System.IO.File.ReadAllText(path);
List<Employee> lstEmployee = new List<Employee>();
//Execute a loop over the rows.
string[] sCSVData = csvData.Split('\n');
for (int iCount = 1; iCount < sCSVData.Length-1; iCount++)
{

lstEmployee.Add(new Employee
{
EmployeeID = sCSVData[iCount].Split(',')[0],
EmployeeName = sCSVData[iCount].Split(',')[1],
Designation = sCSVData[iCount].Split(',')[2],
Skill = sCSVData[iCount].Split(',')[3]
});
}
 
return Json(lstEmployee, JsonRequestBehavior.AllowGet);

}

Step 5-  Now right Click on Index Action method and add a View called Index.cshtml as shown in below image.

Display CSV Data

Step 6- Write the following code to Index.cshtml file.

@{
ViewBag.Title = "Index";
}
<html>
<head>
<title>

Display CSV Data in Grid
</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="~/Css/BootstrapTableCss.css" rel="stylesheet" />
<script type="text/javascript">
GetCSVData();
function GetCSVData() {
$.ajax({
url: '/Home/GetCSVData/',
dataType: "json",
type: "GET",
contentType: 'application/json; charset=utf-8',

success: function (data) {
for (var iCount = 0; iCount < data.length; iCount++)
{
$("#tData").append("<tr>"+
" <td data-title='Employee ID'>"+
data[iCount].EmployeeID+
"</td>" +
" <td data-title='Employee Name'>" +
data[iCount].EmployeeName +
"</td>" +
" <td data-title='Designation'>" +
data[iCount].Designation +
"</td>" +
" <td data-title='Skill'>" +
data[iCount].Skill +
"</td>" +
" </tr>")
}
}

});
}
</script>
</head>
<body>
<h2>
Employee Details 
</h2>
<hr />
<div class="container">
<div class="row">

<div id="no-more-tables">
<table class="col-md-12 table-bordered table-striped table-condensed cf">
<thead class="cf">
<tr>
<th>
Employee ID
</th>
<th>
Employee Name
</th>
<th>
Designation
</th>
<th>
Skill
</th>



</tr>

</thead>
<tbody id="tData">
</tbody></table></div>
</div>
</div>
</body>
</html>

Step 7- Create a Content folder in root location and the Employee.csv file. You can download Employee.csv file from here.

Output:

Display CSV Data

View More:

Conclusion:

I hope this post is useful for you. I would like to have your feedback, comments and suggestions.

Thank You.

43 Comments

  • Hello! I’ve been following your site for a long time now and finally got the courage to go ahead and give you a shout out from Humble Texas! Just wanted to tell you keep up the fantastic job!|

  • My spouse and I absolutely love your blog and find nearly all of your post’s to be just what I’m looking for. can you offer guest writers to write content for yourself? I wouldn’t mind producing a post or elaborating on some of the subjects you write concerning here. Again, awesome weblog!|

  • I keep listening to the news update talk about receiving free online grant applications so I have been looking around for the finest site to get one. Could you advise me please, where could i find some?

  • Hi! Someone in my Facebook group shared this site with us so I came to look it over. I’m definitely loving the information. I’m bookmarking and will be tweeting this to my followers! Exceptional blog and terrific style and design.

  • What i don’t understood is actually how you’re not actually much more smartly-preferred than you may be now. You are very intelligent. You already know therefore considerably in the case of this subject, made me for my part consider it from a lot of various angles. Its like men and women are not fascinated until it is something to do with Lady gaga! Your individual stuffs great. At all times maintain it up!

  • Does your website have a contact page? I’m having a tough time locating it but, I’d like to shoot you an e-mail. I’ve got some suggestions for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it develop over time.

  • obviously like your web-site but you need to check the spelling on quite a few of your posts. A number of them are rife with spelling problems and I find it very troublesome to tell the truth nevertheless I’ll certainly come back again.

  • Hi, Neat post. There’s a problem with your website in web explorer, could test this? IE nonetheless is the marketplace chief and a large component to folks will leave out your excellent writing due to this problem.|

  • Hiya! I know this is kinda off topic however , I’d figured I’d ask. Would you be interested in trading links or maybe guest writing a blog article or vice-versa? My website addresses a lot of the same subjects as yours and I believe we could greatly benefit from each other. If you happen to be interested feel free to shoot me an e-mail. I look forward to hearing from you! Great blog by the way!

  • Just desire to say your article is as astounding. The clarity to your put up is simply nice and i can assume you’re a professional on this subject. Well together with your permission let me to grasp your RSS feed to keep updated with coming near near post. Thank you a million and please keep up the rewarding work.

  • I’ve come across that now, more and more people are increasingly being attracted to cameras and the issue of taking pictures. However, really being a photographer, you need to first commit so much time deciding the model of video camera to buy plus moving store to store just so you could buy the lowest priced camera of the trademark you have decided to choose. But it does not end just there. You also have to take into consideration whether you should purchase a digital video camera extended warranty. Thx for the good suggestions I gained from your site.

  • Spot on with this write-up, I truly feel this website needs a great deal more attention. I’ll probably be returning to read through more, thanks for the info!|

  • Spot on with this write-up, I absolutely feel this amazing site needs far more attention. I’ll probably be returning to see more, thanks for the info!|

  • Thank you for sharing superb informations. Your web site is very cool. I am impressed by the details that you’ve on this site. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for extra articles. You, my pal, ROCK! I found simply the information I already searched all over the place and just could not come across. What a perfect site.

  • Thank you for the sensible critique. Me & my neighbor were just preparing to do a little research on this. We got a grab a book from our area library but I think I learned more from this post. I’m very glad to see such excellent info being shared freely out there.

  • Hi! I know this is kinda off topic nevertheless I’d figured I’d ask. Would you be interested in trading links or maybe guest writing a blog article or vice-versa? My blog discusses a lot of the same topics as yours and I think we could greatly benefit from each other. If you might be interested feel free to send me an e-mail. I look forward to hearing from you! Awesome blog by the way!|

  • F*ckin’ remarkable things here. I am very glad to see your post. Thanks a lot and i am looking forward to contact you. Will you please drop me a e-mail?

  • Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your blog? My blog is in the very same niche as yours and my visitors would really benefit from a lot of the information you present here. Please let me know if this alright with you. Many thanks!

  • There are some interesting deadlines on this article however I don’t know if I see all of them middle to heart. There’s some validity however I’ll take maintain opinion till I look into it further. Good article , thanks and we want extra! Added to FeedBurner as well

  • Hey there! I just wanted to ask if you ever have any trouble with hackers? My last blog (wordpress) was hacked and I ended up losing months of hard work due to no data backup. Do you have any solutions to protect against hackers?

  • In the grand pattern of things you secure a B- for hard work. Where you actually lost us was in the facts. As they say, details make or break the argument.. And that couldn’t be more correct in this article. Having said that, allow me say to you precisely what did work. The article (parts of it) is rather engaging and this is probably why I am taking an effort to comment. I do not make it a regular habit of doing that. Second, despite the fact that I can notice a leaps in reason you come up with, I am definitely not confident of how you seem to unite your points which in turn help to make the conclusion. For the moment I shall subscribe to your position however hope in the foreseeable future you actually connect the dots better.