0

I am working on one web application which is developing in ASP.Net vNext 5 MVC with angularjs. I want to export javascript array data in format of .xls, csv and pdf. My data structure (List of Employees) like this,

[{
"id": 1,
"name": "Employee 1",
"joiningDate": "January 2005",
"roleList": [{
    "id": 1,
    "name": "Role 1",
    "relatedDepartment": [{
        "id": 1,
        "departmentName": "Department 1",
        "status": "Working"
    }, {
        "id": 2,
        "departmentName": "Department 2",
        "status": "Paused"
    }]
}, {
    "id": 2,
    "name": "Role 2",
    "relatedDepartment": [{
        "id": 3,
        "departmentName": "Department 3",
        "status": "Working"
    }, {
        "id": 4,
        "departmentName": "Department 4",
        "status": "Working"
    }]
}]
}, {
    "id": 2,
    "name": "Employee 2",
    "joiningDate": "May 2006",
    "roleList": [{
        "id": 3,
        "name": "Role 3",
        "relatedDepartment": [{
            "id": 1,
            "departmentName": "Department 1",
            "status": "Working"
        }, {
            "id": 3,
            "departmentName": "Department 3",
            "status": "Working"
        }]
    }, {
        "id": 4,
        "name": "Role 4",
        "relatedDepartment": [{
            "id": 3,
            "departmentName": "Department 3",
            "status": "Paused"
        }, {
            "id": 4,
            "departmentName": "Department 4",
            "status": "Working"
        }]
    }]
}]

I want to export to excel in format like, Excel format

Is it possible to export to excel in ASP.Net 5 mvc with angularjs? If not please let me know any other possible solutions.

Thanks in advance.

swatkins
  • 13,530
  • 4
  • 46
  • 78
bdp
  • 3
  • 3
  • How are you displaying this data to the user? Is the user supposed to somehow select to export the data to Excel? – Rani Radcliff Jul 15 '16 at 16:38
  • Data will display like this shown in this image [link](http://i.stack.imgur.com/8xCbj.png). In my web page there is one button, on clicking of that have to export data to excel. – bdp Jul 15 '16 at 16:48
  • If you post your HTML I can show you how to do this. – Rani Radcliff Jul 15 '16 at 17:14
  • [Sample of my page](http://jsfiddle.net/bdp21/034oy53j/). – bdp Jul 15 '16 at 20:15

1 Answers1

0

First, in Visual Studio, go to NuGet Package manager and add FileSaver.js to your project. Change your HTML to add a div with an id like below:

    <div id="myTable">
<table class="table table-bordered">
    <thead>
      <tr>
        <th rowspan="2">#</th>
        <th colspan="2">Employee Info</th>
        <th rowspan="2">Roles</th>
        <th colspan="2">Related Department</th>
      </tr>
      <tr>
        <th>Name</th>
        <th>Joining Date</th>
        <th>Name</th>
        <th>Status</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat-start="emp in empList" ng-show="false" ng-init="parentIndex= $index"></tr>
      <tr ng-repeat-start="role in emp.roleList" ng-if="false"></tr>
      <tr ng-repeat="dept in role.relatedDepartment" ng-init="kFirst = $first; rSpan=countRowSpan(emp.roleList)">
        <td ng-if="(kFirst && $parent.$first)" rowspan="{{rSpan}}">{{(parentIndex + 1)}}</td>
        <td ng-if="(kFirst && $parent.$first)" rowspan="{{rSpan}}">
          {{emp.name}}
        </td>
        <td ng-if="(kFirst && $parent.$first)" rowspan="{{rSpan}}">
          {{emp.joiningDate}}
        </td>
        <td ng-if="kFirst" rowspan="{{role.relatedDepartment.length}}">{{role.name}}</td>
        <td>{{dept.departmentName}}</td>
        <td>{{dept.status}}</td>
      </tr>
      <tr ng-repeat-end ng-if="false"></tr>
      <tr ng-repeat-end ng-if="false"></tr>
    </tbody>
  </table>
</div>
   <button type="button" ng-click="export()">Export</button>

Then in your Angular Controller:

 $scope.export = function(){
var blob = new Blob([document.getElementById('myTable').innerHTML], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
                });

                saveAs(blob, "MyTable.xls");
}

For csv see FileSaver.js.

For pdf see StackOverflow

Community
  • 1
  • 1
Rani Radcliff
  • 4,856
  • 5
  • 33
  • 60
  • Let me know if you have any problems with the above solution. – Rani Radcliff Jul 15 '16 at 21:47
  • Thanks for your reply. I tried your solution and its working for excel but I also have to export html table to csv and pdf. I tried for pdf but when I open pdf file its message me that "Failed to load". $scope.export = function() { var blob = new Blob([document.getElementById('myTable').innerHTML], { type: "application/pdf" }); saveAs(blob, "MyTablePDF.pdf"); } – bdp Jul 18 '16 at 14:01
  • Please mark the above solution as the answer to this question so anyone else trying to export to Excel knows that it works. – Rani Radcliff Jul 18 '16 at 15:45
  • Not getting proper output with another structure of table. [jsFiddle Link](https://jsfiddle.net/bdp21/wbksqgz1/). – bdp Jul 18 '16 at 16:16
  • I'm sorry, I don't understand what you mean. What output are you getting? – Rani Radcliff Jul 18 '16 at 16:18
  • Check your HTML, there may be something not quite correct. There seems to be a problem. – Rani Radcliff Jul 18 '16 at 16:23
  • Yes because of some ng-show conditions, the format of excel is not as per html table. Do you know how to solve this problem? – bdp Jul 18 '16 at 16:52
  • I'm not sure off of the top of my head. I would have to have time to play with your code. Sorry. I will look at your fiddle if I get a minute. – Rani Radcliff Jul 18 '16 at 22:22
  • Hey, problem of the format of excel file is not as per html table has been resolved. check this [jsFiddle Link](https://jsfiddle.net/bdp21/meeoz7da/). But Thanks for your wonderful support. – bdp Jul 19 '16 at 18:18
  • Great! Glad I could help. Happy coding! – Rani Radcliff Jul 19 '16 at 18:30
  • To display grid lines in excel file have to add template of excel with option of "DisplayGridlines". Check this [jsFiddle Link](https://jsfiddle.net/bdp21/vvqpqhsb/). – bdp Jul 19 '16 at 18:48