Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is my code

Careerpage.cs

C#
//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System;
using System.Collections.Generic;

namespace pp.Models
{



    public class CareerPage
    {
        public int Id { get; set; }
        public string FileName { get; set; }
        public string contentType { get; set; }
        public byte[] Data { get; set; }
        public string name { get; set; }
        public string comment { get; set; }
        public string company { get; set; }
        public string email { get; set; }
        public string Message { get; set; }
    }
}


FileController.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using pp.Models;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Helpers;
namespace pp.Controllers
{
    public class FileController : Controller
    {
        //
        // GET: /File/

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

        public ActionResult List()
     {
         var users = new List<careerpage>();

         using (imicronEntities dc = new imicronEntities())
        {
          users = dc.CareerPages.ToList();
   }

            return View(users);
    }

        public void GetExcel()
         {
                  List<careerpage> allCust = new List<careerpage>();
                  using (imicronEntities dc = new imicronEntities())
             {
                         allCust = dc.CareerPages.ToList();
         }
               
                      WebGrid grid = new WebGrid(source: allCust, canPage: false, canSort: false);
 
                      string gridData = grid.GetHtml(
                      columns: grid.Columns(
                       grid.Column("Id", "Id"),
                       grid.Column("name", "name"),
                       grid.Column("Message", "Message"),
                       grid.Column("email", "email"),
                       grid.Column("comment", "comment")
                   )
                ).ToString();
 
                  Response.ClearContent();
                  Response.AddHeader("content-disposition", "attachment; filename=CustomerInfo.xls");
                  Response.ContentType = "application/excel";
                  Response.Write(gridData);
                  Response.End();
 }


        
        
    }
}


List.cstml

HTML
@model List<pp.models.careerpage>

@{
    ViewBag.Title = "List";
    
    var grid = new WebGrid(source:Model,canPage:true, rowsPerPage:10);
    grid.Pager(WebGridPagerModes.All);
}

<h2>List</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

<div id="content">
    @grid.GetHtml(
                   // tableStyle: "webgrid-table",
                    //headerStyle: "webgrid-header",
                    //footerStyle: "webgrid-footer",
                    //alternatingRowStyle: "webgrid-alternating-row",
                    //rowStyle: "webgrid-row-style",
                    columns: grid.Columns(
                    grid.Column(header: "Serial No", format: @<text><div>@(item.WebGrid.Rows.IndexOf(item) + 1)</div></text>),
                    grid.Column(columnName:"name",header:"name"),
                    grid.Column(columnName:"comment", header:"comment"),
                    grid.Column(columnName:"Message",header:"message"),
                    grid.Column(header:"email", format:@<text><a href="mailto:@item.email">@item.email</a></text>)
    ))
   

    <div>
        Export Data : @Html.ActionLink("Excel", "GetExcel", "File")
    </div>
    
</div>
Posted
Updated 31-Aug-15 19:14pm
v2
Comments
RMSinha 1-Sep-15 1:39am    
If I have 15 record in my database and its displaying first 10 records in the server and another page its displaying more 5 records...
i want to export that 10 records only, how to do that??
RMSinha 1-Sep-15 1:42am    
my code exporting all 15 records at a time which is not my requirement...
i want only 10 records that are displaying in the first page when i will do exporting,how to export only 10 records only sir/mam??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900