UrbanPro
true

Learn .NET MVC from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Export To Excel using StringBuilder in ASP.net MVC C#

M
Mohammad Shafi
18/10/2016 0 1
 public ActionResult Export()  
   {  
     StringBuilder sb = new StringBuilder();  
     //static file name, can be changes as per requirement  
     string sFileName = "filename.xls";  
     //Bind data list from edmx  
     var Data = objEntities.citymaster.Take(10).ToList();  
     if (Data != null && Data.Any())  
     {  
       sb.Append("<table style='1px solid black; font-size:12px;'>");  
       sb.Append("<tr>");  
       sb.Append("<td style='width:120px;'><b>City Name</b></td>");  
       sb.Append("<td style='width:300px;'><b>City Code</b></td>");  
       sb.Append("</tr>");  
       foreach (var result in Data)  
       {  
         sb.Append("<tr>");  
         sb.Append("<td>" + result.sCityName + "</td>");  
         sb.Append("<td>" + result.sCityCode + "</td>");  
         sb.Append("</tr>");  
       }  
     }  
     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");  
   }  
0 Dislike
Follow 1

Please Enter a comment

Submit

N

N Chaitanya reddy | 29/04/2020

Hi.. unable to open file in excel file, it is opening when tried to open a file in notepad which contains html code in string builder. Please help..

0 0

Other Lessons for You

A very brief introduction to .NET framework
.NET development has started in the year 2000 , a product of Microsoft. From the begining of .NET development till now, it has got many RAD facilities. Why .NET? developed for distributed computing...

Benefits of Angular JS over traditional JavaScript and jquery
1. Easy designing web pages 2. Code reusability 3. Client validations more optimized and faster . 4. MVC architecture finds suitable interface to accept server responses Back in more customized manner .

ValidateAntiForgeryToken used in ajax request in MVC
Today we learn how to use antiforgery token validation Key in ajax reqeust (MVC) first we create a Action with attributes antiforgery key.. Now we add @Html.AntiForgeryToken() in view and then create...

Pro Developer Program Syllabus
What you will learn in Pro-Developer Program: i. Introduction to Web Technologies. ii. Introduction to .Net - Features of .Net, CTS, CLS, CLR and MSIL. iii. C# & .Net Basics - Data Types,...

Benefits of AsP.net webapi over traditional asp.net 2 and 3.5
Tradional asp.net had slower server side response feedback to the server with the MVC concept on webapi there were faster responses from the server side for client requests. Optimization of code was main...
X

Looking for .NET MVC Classes?

The best tutors for .NET MVC Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn .NET MVC with the Best Tutors

The best Tutors for .NET MVC Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more