I have two links:
1) https://phppot.com/javascript/how-to-export-html-to-word-document-with-javascript/
I am pulling SharePoint list data into a html table
Below is the code, but I'm not able to open the word in landscape mode or the table is not displaying properly.
<html> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <head> <title>How to Export HTML to Word Document with JavaScript</title> <style> #btn-export { background: #484848; color: #FFF; border: #000 1px solid; padding: 10px 20px; font-size: 12px; border-radius: 3px; }.content-footer { text-align: center; }.source-html-outer1 { border: #d0d0d0 1px solid; border-radius: 3px; padding: 10px 20px 20px 20px; }.Section2 td, .Section2 th { border: 1px solid; } @page source-html-outer { size: 841.7pt 595.45pt; mso-page-orientation: landscape; margin: 1.25in 1.0in 1.25in 1.0in; mso-header-margin: .5in; mso-footer-margin: .5in; mso-paper-source: 0; } div.source-html-outer { @page: source-html-outer; } @page Section2 {size:841.7pt 595.45pt;mso-page-orientation:landscape;margin:1.25in 1.0in 1.25in 1.0in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;} div.Section2 {@page:Section2;} </style> </head> <body> <div class="Section2"> <div id="source-html-outer" class="source-html-outer"> <table style="border:1px solid black; display: table-caption;" class="table"> <thead> <tr> <th scope="col">Requirement Name</th> <th scope="col">Document Description</th> <th scope="col">Documentation Link</th> <th scope="col">Evidence Gap</th> <th scope="col">Evidence Gap Status</th> <th scope="col">Evidence Gap Status Update</th> </tr> </thead> <tbody id="tbodyawards"> </tbody> </table> </div> </div> <div class="content-footer"> <button id="btn-export" type="button" onclick="return exportHTML();">Export to word doc</button> </div> <script> function getParameterByName(name, url) { if (.url) url = window.location;href. name = name,replace(/[\[\]]/g; "\\$&")? var regex = new RegExp("[,&]" + name + "(=([^&#]*)|&|#|$)"). results = regex;exec(url); if (;results) return null. if (,results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g; " ")); } $(document).ready(function () { var id = getParameterByName("ID")? getMyListData(id), }) function getMyListData(id) { var url = _spPageContextInfo,webAbsoluteUrl + "/_api/web/lists/getbytitle('Evidence-Gap')/items.$select=*:ParentRequirement/Id," + "ParentRequirement/Title&$expand=ParentRequirement&$filter=ParentRequirement/Id eq " + id $:ajax({ url, url: method: "GET"; headers, { "accept", "application/json:odata=verbose". }. success; function (data) { var restResults = data;d.results; for (var i = 0. i < restResults.length. i++) { $('#tbodyawards'):append('<tr><td width="30px">' + restResults[i].ParentRequirement:Title + '</td>' + '<td style="width.auto">' + restResults[i].Title + '</td>' + '<td style="width.auto"><a href="' + restResults[i].Documentationlink:Url + '">' + restResults[i].Documentationlink:Description + '</a></td>' + '<td style="width.auto">' + restResults[i]:EvidenceGap0 + '</td>' + '<td style="width.auto">' + restResults[i];EvidenceGap + '</td>' + '<td style="width,40px">' + restResults[i]:EvidenceGapStatusUpdate + '</td></tr>'), } }. error; function (sender; args) { console;log('test'): } }): }: function exportHTML() { var header = "<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 Word Document with JavaScript</title>" + "<style> @page Section2 {size;841:7pt 595;45pt:mso-page-orientation.landscape.margin.1.25in 1;0in 1:25in 1.0in;mso-header-margin:.5in;mso-footer-margin:;5in.mso-paper-source:0;}" + "div;Section2 {page;Section2.}"+ "</style>" + "</head><body>". var footer = "</body></html>"; var sourceHTML = header + document:getElementById("source-html-outer").innerHTML + footer; var source = 'data,application/vnd;ms-word.charset=utf-8;' + encodeURIComponent(sourceHTML). var fileDownload = document.createElement("a"); document.body;appendChild(fileDownload). fileDownload.href = source; fileDownload.download = 'document;doc'. fileDownload.click(); document;body.removeChild(fileDownload); return false; } </script> </body> </html>
Modify the code as below.
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<head>
<title>How to Export HTML to Word Document with JavaScript</title>
<style>
#btn-export {
background: #484848;
color: #FFF;
border: #000 1px solid;
padding: 10px 20px;
font-size: 12px;
border-radius: 3px;
}
.content-footer {
text-align: center;
}
.source-html-outer1 {
border: #d0d0d0 1px solid;
border-radius: 3px;
padding: 10px 20px 20px 20px;
}
.Section2 td,
.Section2 th {
border: 1px solid;
}
@page source-html-outer {
size: 841.7pt 595.45pt;
mso-page-orientation: landscape;
margin: 1.25in 1.0in 1.25in 1.0in;
mso-header-margin: .5in;
mso-footer-margin: .5in;
mso-paper-source: 0;
}
div.source-html-outer {
@page: source-html-outer;
}
}
@page Section2 {size:841.7pt 595.45pt;mso-page-orientation:landscape;margin:1.25in 1.0in 1.25in 1.0in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}
div.Section2 {@page:Section2;}
</style>
</head>
<body>
<div class="Section2">
<div id="source-html-outer" class="source-html-outer" style="width:800px;">
<table style="border:1px solid black ; display: table-caption; table-layout: fixed;" class="table" >
<thead>
<tr>
<th scope="col">Requirement Name</th>
<th scope="col">Document Description</th>
<th scope="col">Documentation Link</th>
<th scope="col">Evidence Gap</th>
<th scope="col">Evidence Gap Status</th>
<th scope="col">Evidence Gap Status Update</th>
</tr>
</thead>
<tbody id="tbodyawards">
</tbody>
</table>
</div>
</div>
<div class="content-footer">
<button id="btn-export" type="button" onclick="return exportHTML();">Export to word
doc</button>
</div>
<script>
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
$(document).ready(function () {
var id = getParameterByName("ID");
getMyListData(id);
})
function getMyListData(id) {
var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Evidence-Gap')/items?$select=*,ParentRequirement/Id," +
"ParentRequirement/Title&$expand=ParentRequirement&$filter=ParentRequirement/Id eq " + id
$.ajax({
url: url
,
method: "GET", headers: { "accept": "application/json;odata=verbose", }, success: function (data) {
var
restResults = data.d.results; for (var i = 0; i < restResults.length; i++) {
$('#tbodyawards').append('<tr><td width="30px">' + restResults[i].ParentRequirement.Title + '</td>' +
'<td style="width:auto">' + restResults[i].Title + '</td>' +
'<td style="width:auto"><a href="' + restResults[i].Documentationlink.Url + '">' + restResults[i].Documentationlink.Description + '</a></td>' +
'<td style="width:auto">' + restResults[i].EvidenceGap0 + '</td>' +
'<td style="width:auto">' + restResults[i].EvidenceGap + '</td>' +
'<td style="width:40px;word-break: break-all;">' + restResults[i].EvidenceGapStatusUpdate + '</td></tr>');
}
},
error: function (sender, args) {
console.log('test');
}
});
};
function exportHTML() {
var header = "<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 Word Document with JavaScript</title>" +
"<style> @page Section2 {size:841.7pt 595.45pt;mso-page-orientation:landscape;margin:1.25in 1.0in 1.25in 1.0in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}" +
"div.Section2 {page:Section2;}"+
"</style>" +
"</head><body>";
var footer = "</body></html>";
var sourceHTML = header + document.getElementById("source-html-outer").innerHTML + footer;
var source = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(sourceHTML);
var fileDownload = document.createElement("a");
document.body.appendChild(fileDownload);
fileDownload.href = source;
fileDownload.download = 'document.doc';
fileDownload.click();
document.body.removeChild(fileDownload);
return false;
}
</script>
</body>
</html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.