需要引用的js:
jquery.wordexport.js和FileSaver.js
使用 :
$('#Print').wordExport("文档名称"); //导出页面部分内容//样式 需要在表格内定义使用style 不能使用class 引用样式
jquery.wordexport.js 兼容IE8的修改后js,ie8中会弹出弹出框
$.fn.wordExport = function(fileName){
try {
fileName = typeof fileName !== 'undefined' ? fileName : "jQuery-Word-Export";
var static = {
mhtml: {
top: "<html>\n_html_</html>",
head: "<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n<style>\n_styles_\n</style>\n</head>\n",
body: "<body>_body_</body>"
}
};
var options = {
maxWidth: 624
};
//Clone selected element before manipulating it
var markup = $(this).clone();
//Remove hidden elements from the output
markup.each(function() {
var self = $(this);
if (self.is(':hidden'))
self.remove();
});
//TODO: load css from included stylesheet
var styles = '<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4;} @font-face {font-family:"\@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0cm; margin-bottom:0pt; font-size:12.0pt; font-family:宋体;} p.MsoHeader, li.MsoHeader, div.MsoHeader {mso-style-link:"页眉 Char"; margin:0cm; margin-bottom:.0001pt; text-align:center; layout-grid-mode:char; border:none; padding:0cm; font-size:9.0pt; font-family:宋体;} p.MsoFooter, li.MsoFooter, div.MsoFooter {mso-style-link:"页脚 Char"; margin:0cm; margin-bottom:.0001pt; layout-grid-mode:char; font-size:9.0pt; font-family:宋体;} p.MsoAcetate, li.MsoAcetate, div.MsoAcetate {mso-style-link:"批注框文本 Char"; margin:0cm; margin-bottom:.0001pt; font-size:9.0pt; font-family:宋体;} span.msonormal0 {mso-style-name:msonormal;} span.Char {mso-style-name:"批注框文本 Char"; mso-style-link:批注框文本; font-family:宋体;} span.Char0 {mso-style-name:"页眉 Char"; mso-style-link:页眉; font-family:宋体;} span.Char1 {mso-style-name:"页脚 Char"; mso-style-link:页脚; font-family:宋体;} .MsoChpDefault {font-size:10.0pt;} /* Page Definitions */ @page WordSection1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.WordSection1 {page:WordSection1;} -->';
//Aggregate parts of the file together
var fileContent = static.mhtml.top.replace("_html_", static.mhtml.head.replace("_styles_", styles) + static.mhtml.body.replace("_body_", markup.html()));
fileSave(fileName+".doc",fileContent);
function fileSave(filename,exportData) {
var w = window.open("about:blank", "export", "height=0,width=0,toolbar=no,menubar=no,scrollbars=no,resizable=on,location=no,status=no");
w.document.charset = "gb2312";
w.document.write(exportData);
w.document.execCommand("SaveAs", false, filename);
w.close();
}
}catch(e){
}
}