将表导出到MS-Word

时间:2017-02-23 03:59:23

标签: javascript angularjs

enter image description here

function export2Word() {

    var html, link, blob, url, css;

    css = (
               '<style>  table {   border-collapse: collapse;  border-style:none;  } .tables td,th{border:1px solid black;}' +
               //'@page WordSection1{size: 841.95pt 595.35pt;mso-page-orientation: landscape;}' +
               //'div.WordSection1 {page: WordSection1;} ' +
               '</style>'
             );

     html = document.getElementById("tblPrint").outerHTML;
     blob = new Blob(['\ufeff', css + html], {
                 type: 'application/msword'
             });
     url = URL.createObjectURL(blob);
     link = document.createElement('A');
     link.href = url;
     link.download = 'Document';  // default name without extension 
     document.body.appendChild(link);
     if (navigator.msSaveOrOpenBlob) 
         navigator.msSaveOrOpenBlob(blob, 'Document.doc'); // IE10-11
     else link.click();  // other browsers
         document.body.removeChild(link);
};

当我尝试不显示任何只显示数据的表结构时,我想将表导出到MS-Word

我使用像<div style="page-break-after:always;">&nbsp;&nbsp;&nbsp;&nbsp;</div>一样的新的rollno使用分页符,这也不起作用

我的代码有什么问题?以及如何在ms-word中进行分页?

提前致谢

1 个答案:

答案 0 :(得分:1)

请尝试此操作

export html table as word file

var docDOM = document.getElementById('example');

var docObject = docx.export(docDOM, // required DOM Object
{
creator: "Creator", // optional String
lastModifiedBy: "Last person to modify", // optional String
created: new Date(), // optional Date Object
modified: new Date() // optional Date Object
});

var link = document.createElement('a');
link.appendChild(document.createTextNode("Download Link Text"));
link.title = "Download Title";
link.download = "FilenameHere.docx";
link.href = docObject.href();
document.getElementById("example").appendChild(link);

convert html table to word