js导出Word

需要引用的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){
	}
}

 

转载于:https://my.oschina.net/u/3715963/blog/1829386

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值