Export the Excel File in ASP.Net Web API

Introduction

In this article, I will show you how to export an Excel file in the Web API. Here we create an Excel file and then attach this file.

The following is the procedure for creating the application.

Step 1

Create a Web API application.

  • Start Visual Studio 2012.
  • From the Start window select "New Project".
  • From the new project window select "Installed" -> "Visual C#" -> "Web".
  • Select "ASP.NET MVC4 Web Application" and click the "OK" button.

    exp.jpg
  • From the "MVC4 Project" window select "Web API".

    exp1.jpg
  • Click on the "OK" button.

Step 2

Add the Model Class "Record.cs" in the model folder.

  • In the "Solution Explorer".
  • Right-click on the "Model folder".
  • Select "Add" -> "class".
  • From the Add item window select "Installed" -> "Visual C#".

    exp2.jpg
  • Select "Class" and then click on "Add" button.

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace ExportFile.Models  
  6. {  
  7.     public class Record  
  8.     {  
  9.         public string FName { getset; }  
  10.         public string LName { getset; }  
  11.         public string Address { getset; }     
  12.     }  
  13. }   

Step 3

In the "HomeController" write some code to export the Excel file. This Excel file exists:

  • In the "Solution Explorer".
  • Expand the "Controller" folder.
  • Select "HomeController".

    exp3.jpg

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using ExportFile.Models;  
  7. using System.Text;  
  8. namespace ExportFile.Controllers  
  9. {  
  10.     public class HomeController : Controller  
  11.     {  
  12.         public ActionResult Index()  
  13.         {  
  14.             return View();  
  15.         }  
  16.         [HttpPost]  
  17.         public ActionResult Index(int id = 0)  
  18.         {  
  19.             List<Record> obj = new List<Record>();  
  20.             obj = RecordInfo();  
  21.             StringBuilder str = new StringBuilder();  
  22.             str.Append("<table border=`" + "1px" + "`b>");           
  23.             str.Append("<tr>");  
  24.             str.Append("<td><b><font face=Arial Narrow size=3>FName</font></b></td>");  
  25.             str.Append("<td><b><font face=Arial Narrow size=3>LName</font></b></td>");  
  26.             str.Append("<td><b><font face=Arial Narrow size=3>Address</font></b></td>");  
  27.             str.Append("</tr>");  
  28.             foreach (Record val in obj)  
  29.             {  
  30.                 str.Append("<tr>");  
  31.                 str.Append("<td><font face=Arial Narrow size=" + "14px" + ">" + val.FName.ToString() +"</font></td>");  
  32.                 str.Append("<td><font face=Arial Narrow size=" + "14px" + ">" + val.LName.ToString() + "</font></td>");  
  33.                 str.Append("<td><font face=Arial Narrow size=" + "14px" + ">" + val.Address.ToString() + "</font></td>");  
  34.                 str.Append("</tr>");  
  35.             }  
  36.             str.Append("</table>");  
  37.             HttpContext.Response.AddHeader("content-disposition""attachment; filename=Information" + DateTime.Now.Year.ToString() + ".xls");  
  38.             this.Response.ContentType = "application/vnd.ms-excel";  
  39.             byte[] temp = System.Text.Encoding.UTF8.GetBytes(str.ToString());  
  40.             return File(temp, "application/vnd.ms-excel");  
  41.         }  
  42.         public List<Record> RecordInfo()  
  43.         {  
  44.             List<Record> recordobj = new List<Record>();  
  45.             recordobj.Add(new Record { FName = "Smith", LName = "Singh", Address = "Knpur" });  
  46.             recordobj.Add(new Record { FName = "John", LName = "Kumar", Address = "Lucknow" });  
  47.             recordobj.Add(new Record { FName = "Vikram", LName = "Kapoor", Address = "Delhi" });  
  48.             recordobj.Add(new Record { FName = "Tanya", LName = "Shrma", Address = "Banaras" });  
  49.             recordobj.Add(new Record { FName = "Malini", LName = "Ahuja", Address = "Gujrat" });  
  50.             recordobj.Add(new Record { FName = "Varun", LName = "Katiyar", Address = "Rajasthan" });  
  51.             recordobj.Add(new Record { FName = "Arun  ", LName = "Singh", Address = "Jaipur" });  
  52.             recordobj.Add(new Record { FName = "Ram", LName = "Kapoor", Address = "Panjab" });  
  53.             recordobj.Add(new Record { FName = "Vishakha", LName = "Singh", Address = "Banglor" });  
  54.             recordobj.Add(new Record { FName = "Tarun", LName = "Singh", Address = "Kannauj" });  
  55.             recordobj.Add(new Record { FName = "Mayank", LName = "Dubey", Address = "Farrukhabad" });  
  56.             return recordobj;  
  57.         }  
  58.     }  
  59. } 

In the code above we use the "str.Append" that is used for adding the string.

  1. HttpContext.Response.AddHeader("content-disposition""attachment; filename=Information" + DateTime.Now.Year.ToString() + ".xls"); 
This line of code define that attaching the Excel file. In it provide the filename, Date and time and the extension of the file.

The "Response.ContentType="application/vnd.ms-excel"" tells to the browser that this is actually an Excel file.

Step 4

Now write some HTML code in the "index.cshtml" file. This file exists:

  • In the "Solution Explorer".

  • Expand "Views" folder.

  • Select "Home" -> "index.cshtml".

    exp4.jpg

Add the following code:

  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4. @using(Html.BeginForm("Index","Home"))  
  5. {  
  6. <h2>Export the Excel file in web API Application</h2><br />  
  7. <input type="submit" value="Export" title="Export An Excel File" />  
  8. } 

Step 5

Execute the application by press"F5" and the output looks like this:

exp5.jpg

Click on the "Export" button. Then select the "Open With" radio button and click the "OK" button.

exp6.jpg

Now open the "Informatiom.xls" file.

exp7.jpg