0

I have following web api code which returns data into excel file.

  1. I have requirement to export (open download popup) for this data using JavaScript/Angular JS/ Jquery.

  2. I want to add data into different excel sheets

Note: I do not want to create any copy of excel file at server, just wanted to show download popup to client.

Web Api Code Snippet as below - 1) Entity - Record

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ExcelExport.Models
{
    public class Record
    {
        public string FName { get; set; }
        public string LName { get; set; }
        public string Address { get; set; }   
    }
}

2) Controller - HomeController (URL - http://ServerName:PortNumber/Home/GetExcelData)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ExportToExcel.Models;
using System.Text;

namespace ExportToExcel.Controllers
{
    public class HomeController : Controller
    {
        [HttpPost]
        public ActionResult GetExcelData(int id = 0)
        {
            List<Record> obj = new List<Record>();
            obj = RecordInfo();
            StringBuilder str = new StringBuilder();
            str.Append("<table border=`" + "1px" + "`b>");
            str.Append("<tr>");
            str.Append("<td><b><font face=Arial Narrow size=3>FName</font></b></td>");
            str.Append("<td><b><font face=Arial Narrow size=3>LName</font></b></td>");
            str.Append("<td><b><font face=Arial Narrow size=3>Address</font></b></td>");
            str.Append("</tr>");
            foreach (Record val in obj)
            {
                str.Append("<tr>");
                str.Append("<td><font face=Arial Narrow size=" + "14px" + ">" + val.FName.ToString() + "</font></td>");
                str.Append("<td><font face=Arial Narrow size=" + "14px" + ">" + val.LName.ToString() + "</font></td>");
                str.Append("<td><font face=Arial Narrow size=" + "14px" + ">" + val.Address.ToString() + "</font></td>");
                str.Append("</tr>");
            }
            str.Append("</table>");
            HttpContext.Response.AddHeader("content-disposition", "attachment; filename=Information" + DateTime.Now.Year.ToString() + ".xls");
            this.Response.ContentType = "application/vnd.ms-excel";
            byte[] temp = System.Text.Encoding.UTF8.GetBytes(str.ToString());
            return File(temp, "application/vnd.ms-excel");
        }

        public List<Record> RecordInfo()
        {
            List<Record> recordobj = new List<Record>();
            recordobj.Add(new Record { FName = "Smith", LName = "Singh", Address = "Knpur" });
            recordobj.Add(new Record { FName = "John", LName = "Kumar", Address = "Lucknow" });
            recordobj.Add(new Record { FName = "Vikram", LName = "Kapoor", Address = "Delhi" });
            recordobj.Add(new Record { FName = "Tanya", LName = "Shrma", Address = "Banaras" });
            recordobj.Add(new Record { FName = "Malini", LName = "Ahuja", Address = "Gujrat" });
            recordobj.Add(new Record { FName = "Varun", LName = "Katiyar", Address = "Rajasthan" });
            recordobj.Add(new Record { FName = "Arun  ", LName = "Singh", Address = "Jaipur" });
            recordobj.Add(new Record { FName = "Ram", LName = "Kapoor", Address = "Panjab" });
            recordobj.Add(new Record { FName = "Vishakha", LName = "Singh", Address = "Banglor" });
            recordobj.Add(new Record { FName = "Tarun", LName = "Singh", Address = "Kannauj" });
            recordobj.Add(new Record { FName = "Mayank", LName = "Dubey", Address = "Farrukhabad" });
            return recordobj;
        }
    }
}
2
  • So what's the question? Commented May 4, 2014 at 17:19
  • If you want to create sheets and more advanced functions outside of CSVs, you need to add a reference to the Excel Object Library dll. Then you use new WorkSheet() etc. Commented May 5, 2014 at 1:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.