Thursday 7 September 2017

Export Grid (Html Table) data from database to CSV (Text) file in ASP.Net MVC

Export Grid (Html Table) data from database to CSV (Text) file in ASP.Net MVC
Description:

In this example we explain that how to Export Grid data from database to CSV file in asp.net mvc.or how to bind Grid from database and export grid data to CSV file in asp.net MVC.or populate or bind Grid or Web grid from database and export this Grid data to CSV text file in asp.net MVC.

We have already explain to you that how to export Grid view data to CSV using Asp.Net but here we go through same process using MVC with Razor View.

Controller:

public class EmployeeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            NorthwindEntities entities = new NorthwindEntities();
            return View(from customer in entities.Employees.Take(10)
                        select customer);
        }

        [HttpPost]
        public FileResult Export()
        {
            string[] columnNames = new string[] { "EmployeeID", "ContactName", "City", "Country" };

            NorthwindEntities entities = new NorthwindEntities();

            var Employees = from customer in entities.Employees.Take(10)
                            select customer;

            //Build the CSV file data as a Comma separated string.
            string csv = string.Empty;

            foreach (string columnName in columnNames)
            {
                //Add the Header row for CSV file.
                csv += columnName + ',';
            }

            //Add new line.
            csv += "\r\n";

            foreach (var employee in Employees)
            {
                //Add the Data rows.
                csv += employee.EmployeeID.Replace(",", ";") + ',';
                csv += employee.ContactName.Replace(",", ";") + ',';
                csv += employee.City.Replace(",", ";") + ',';
                csv += employee.Country.Replace(",", ";") + ',';

                //Add new line.
                csv += "\r\n";
            }

            //Download the CSV file.
            byte[] bytes = Encoding.ASCII.GetBytes(csv);
            return File(bytes, "application/text", "Grid.csv");
        }

    }

 View:

@model IEnumerable<Export_Excel_MVC.Employee>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width"/>

    <title> Export Grid (Html Table) data from database to CSV (Text) file in ASP.Net MVC

</title>
</head>
<body>
    <h4>Customers</h4>
    <hr/>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>EmployeeID</th>
            <th>Contact Name</th>
            <th>City</th>
            <th>Country</th>
        </tr>
        @foreach (Employee employee in Model)
        {
            <tr>
                <td>@employee.EmployeeID</td>
                <td>@employee.ContactName</td>
                <td>@employee.City</td>
                <td>@employee.Country</td>
            </tr>
        }
    </table>
    <br/>
    <br/>
    @using (Html.BeginForm("Export", "Employee", FormMethod.Post))
    {
        <input type="submit" value="Export"/>
    }
</body>
</html>



This entry was posted in :

0 comments:

Post a Comment