Sunday, 6 January 2019

Export Excel

Export Excel in ASP.Net MVC  using C# With out third party integration

--------------------------------------------------------------------------------------------------------------
Most of the time  we use third party integration to make PDF or EXCEL file to show or download.
Today we will see how we can avoid third party integration and export the excel in c#.

So here we will use StringBuilder class to make a table  and then export that string object as Excel
file.



 When user click on Export excel button It will download the excel file.

Here you can get the code on Onclick action.















Here you can get the code .

Code :

HTML:

<a href="/Area/Controller/TrackExport" class="btn btn-default pull-right"><i class="fa fa-file-excel-o"></i> Export Excel </a>


C# :

        public ActionResult ExortExcel(int? id)
        {
            StringBuilder sb = new StringBuilder();

            string sFileName = "Data.xls";

            //Bind data list from edmx
            var Data = objDBEntity.SP_Name(id).ToList();

            if (Data != null && Data.Any())
            {
                sb.Append("<table border='1' style='4px solid black; font-size:12px;'>");
                sb.Append("<tr style='width:150px;background-color:#fc9003;'>");
                sb.Append("<td align='center'><b>Sr. N o</b></td>");
                sb.Append("<td align='center' ><b>Batch  Name</b></td>");
                sb.Append("<td align='center'><b>Batch  Short Name</b></td>");
                sb.Append("<td align='center' >  <b>District</b></td>");
                sb.Append("<td align='center'><b>Division</b></td>");
                sb.Append("<td align='center' ><b>Training Type</b></td>");
                sb.Append("<td align='center'><b>Institute Name</b></td>");
                sb.Append("<td align='center' '><b>Start Date</b></td>");
                sb.Append("<td align='center'><b>End Date</b></td>");
                sb.Append("<td align='center' ><b>Actual End Date</b></td>");
                sb.Append("<td align='center' ><b>Approximate Completion Date</b></td>");
                sb.Append("</tr>");

                int inc = 0;
                foreach (var result in Data)
                {
                    sb.Append("<tr >");
                    sb.Append("<td align='center'>" + (++inc) + "</td>");
                    sb.Append("<td align='center'>" + result.BATCHNAME + "</td>");
                    sb.Append("<td align='center'>" + result.BATCHSHORTNAME + "</td>");
                    sb.Append("<td align='center'>" + result.DistrictName + "</td>");
                    sb.Append("<td align='center'>" + result.Division_Name + "</td>");
                    sb.Append("<td align='center'>" + result.TrainingType + "</td>");
                    sb.Append("<td align='center'>" + result.InstituteName + "</td>");
                    sb.Append("<td align='center'>" + result.STARTDATE + "</td>");
                    sb.Append("<td align='center'>" + result.ENDDATE + "</td>");
                    sb.Append("<td align='center'>" + result.ACTUALENDDATE + "</td>");
                    sb.Append("<td align='center'>" + result.APPROXCOMPLETIONDATE + "</td>");
                    sb.Append("</tr>");
                }
                sb.Append("</table>");
            }
            HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" +                            sFileName);
            this.Response.ContentType = "application/vnd.ms-excel";
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
            return File(buffer, "application/vnd.ms-excel");

        }


  • If  any one have any question then ask me in comment section.

No comments:

Post a Comment

Custom Pagination

  SQL Server Pagination with MVC and JQuery ----------------------------------------------------------------- The question is, why ...