0

I have spent hours searching all over the internet but it seems no one has a current answer. I have a simple table set up in a PARTIAL view and want to export the data in the html table not from the database. I do not have the option of using any plugins either. I am open to doing it through JavaScript/Jquery but I am fairly new to MVC and I have exhausted other solutions on the internet.

Address Book Summary (Partial View):

<table class="table table-bordered table-responsive table-striped" id="tblAddressBook">
    <thead>
        <tr>
            <th> @Html.DisplayNameFor(model => model.Name)</th>
            <th> @Html.DisplayNameFor(model => model.Address)</th>
         <tr>
     </thead>
     <tbody>
     @foreach (var item in Model)
        {
        <td class="text-right">@Html.DisplayFor(modelItem => item.Name)</td>
        <td class="text-right">@Html.DisplayFor(modelItem => item.Address)</td>
     }
     </tbody>
     <tfoot>
        <td class="text-right>Model.Sum(model => model.TotalPeople))</td>
        <td class="text-right>Model.Sum(model => model.Addresses))</td>
     </tfoot>

Address Book

<button type="button" class="btn btn-primary">
    Export
</button>
<div id="summaryAddressBook">
            @Html.Partial("AddressBook/_Summary", Model.Peoples)
</div>

Address Book Controller

public ActionResult Export() {
   Response.ContentType = "application/x-msexcel"; 
   Response.AddHeader("Content-Disposition", "attachment;filename=ExcelFile.xls");
   return View();
}
cxwilson
  • 107
  • 1
  • 5
  • 15
  • If you can't use any plugins, you have pretty much no other choice than using the microsoft open xml sdk (because using interop on a server is something you should avoid at all cost). This will be the hard way with a lot of code which takes long to write and make bug-free. Or you can use a library like EPPlus and be done with it in a few hours – Alexander Derck Mar 23 '16 at 20:17
  • @AlexanderDerck I'm okay with spending the hours but I know there has to be some way to do it other than to use the Microsoft open xml sdk. I've seen implementations in webforms but nothing for MVC that is really helpful – cxwilson Mar 23 '16 at 20:21

2 Answers2

0

Personally, here is how I would get it done. I would use JQuery to gather up the data from the table through this:

var tableData = {}
$('#tblAddressBook > tbody  > tr').each(function() {...add each row's columns to tableData...});

Then make an AJAX call to an MVC Controller method. See Here: Making a Simple Ajax call to controller in asp.net mvc

With your data now in the controller method, you can go ahead and create your excel file with the data. I would probably go with .csv file, rather than .xls if you can, considering you most likely need a third party library to export to .xls.

Edit: Alternatively, you can skip the jquery and pass the table's model straight to the controller, see here: Pass Table Value from View to Controller MVC

Community
  • 1
  • 1
Steve
  • 571
  • 8
  • 16
0

If "Address Book Summary"(PartialView) contains only the table then change the code :-

public ActionResult Export() {
    Response.ContentType = "application/x-msexcel"; 
    Response.AddHeader("Content-Disposition", "attachment;filename=ExcelFile.xls");
    return View();
}

to :-

public ActionResult Export()
{
    Response.ContentType = "application/x-msexcel"; 
    Response.AddHeader("Content-Disposition", "attachment; filename=ExcelFile.xls");
    return PartialView("Address_Book_Partial_View_FileName_Here_Without_Extension");
}