1

I have a Json array of object as follows

[{name: test1, age:20},{name: test2, age:23},{name: test3, age:30}]

I want to print these values to a table in Microsoft word document. In another way, I want to generate a word document with above values displayed in a table using angularjs. How can I proceed?

2
  • Have you looked at any libraries for generating a Word document on npm? Commented Jun 10, 2019 at 4:35
  • I Googled several libraries such as html-docx-js, docx.js. I wanted to know if there is any otherway other than using these libraries Commented Jun 10, 2019 at 5:15

1 Answer 1

2

I could achieve this without using any third party libraries as follows. Here I am displaying the values in a HTML table and that table is converted into a word document.

<script>
$scope.arrayValFunc = function()
{
$scope.arrayVal =[];
//some code to assign values to the json array

}

$scope.downloadWordDoc = Export2Doc(element, filename = ''){
    var preHtml = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'><head><meta charset='utf-8'><title>Export HTML To Doc</title></head><body>";
    var postHtml = "</body></html>";
    var html = preHtml+document.getElementById(element).innerHTML+postHtml;

    var blob = new Blob(['\ufeff', html], {
        type: 'application/msword'
    });

    // Specify link url
    var url = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(html);

    // Specify file name
    filename = filename?filename+'.doc':'document.doc';

    // Create download link element
    var downloadLink = document.createElement("a");

    document.body.appendChild(downloadLink);

    if(navigator.msSaveOrOpenBlob ){
        navigator.msSaveOrOpenBlob(blob, filename);
    }else{
        // Create a link to the file
        downloadLink.href = url;

        // Setting the file name
        downloadLink.download = filename;

        //triggering the function
        downloadLink.click();
    }

    document.body.removeChild(downloadLink);
}

</script>

<html>
<div id="divOne">
<table>
<tr ng-repeat="record in arrayVal">
<th>Name</th>
<th>Age</th>
<tr>
<td>{{record.name}}</td>
<td>{{record.age}}</td>
</tr>
</div>
<input type="submit" ng-click="downloadWordDoc('divOne');"/>
</html>
Sign up to request clarification or add additional context in comments.

Comments

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.