Click here to Skip to main content
15,885,839 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
using linq query-:

C#
public IEnumerable<EmployeeModel> GetAllEmployeeInExcel()
   {
       try
       {
           using (var ContextDb = new FourMagazineEntities())
           {
               var employee = (from itm in ContextDb.Employees
                               where itm.Role == 3
                               select new EmployeeModel
                               {
                                   EmpID = itm.EmpID,
                                   Code = itm.Code,
                                   Name = itm.Name,
                                   Email = itm.Email,
                                   Role = itm.Role.Value,
                                   RoleName = (from c in ContextDb.Roles.Where(d => d.RoleID == itm.Role) select c.RoleName).FirstOrDefault(),
                                   Address = itm.Address,
                                   City = itm.City,
                                   Country = itm.Country,
                                   PhoneNo = itm.PhoneNo,
                                   MobileNo = itm.MobileNo,
                                   CreateDate = itm.CreatedDate.Value,

                                   //EmpImage = itm.EmpImage,

                                   VatNo = itm.VatNo,
                                   CompanyID = itm.CompanyID.Value,
                                   CompanyName = (from c in ContextDb.Employees.Where(d => d.EmpID == itm.CompanyID) select c.Name).FirstOrDefault(),
                                   Status = itm.Status.Value == true ? true : false,
                               }).OrderByDescending(x => x.EmpID).ToList();
               return employee;
           }
       }
       catch (Exception ex)
       {
           throw ex;
       }
   }


protected void btnExport_Click(object sender, EventArgs e)
        {
            ExportToExcel();
        }

        public void ExportToExcel()
        {
            var emp = objEmployee.GetAllEmployeeInExcel();
            if (emp != null)
            {
                string filename = "Employee.xls";
                System.IO.StringWriter tw = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                DataGrid dgGrid = new DataGrid();
                dgGrid.DataSource = emp;
                dgGrid.DataBind();

                //Get the HTML for the control.
                dgGrid.RenderControl(hw);
                //Write the HTML back to the browser.
                //Response.ContentType = application/vnd.ms-excel;
                Response.ContentType = "application/vnd.ms-excel";
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
                this.EnableViewState = false;
                Response.Write(tw.ToString());
                Response.End();
            }
        }
from above this code i already fetch all data in excel sheet but some column data are not required. so, i can't understand to fetch some column data.


SalesName	  Password	Role	Address    EmpImge    EmpStatus
raj			         1       sdf
abhi			         2       abc

I want to fetch from database SalesName Role Address other are not required.
Posted
Updated 18-Jan-14 3:10am
v2

1 solution

Just use below mentioned Custom Model with your GetAllEmployeeInExcel() method.

C#
using (var ContextDb = new FourMagazineEntities())
          {
              var employee = (from itm in ContextDb.Employees
                              where itm.Role == 3
                              select new EmployeeModel
                              {
                                  Name = itm.Name,
                                  Role = itm.Role.Value,
                                  Address = itm.Address,

                              }).OrderByDescending(x => x.EmpID).ToList();
              return employee;
          }
 
Share this answer
 
v3

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