首页 > 代码库 > 将html页面导出为word
将html页面导出为word
利用js和php导出页面到word的代码:
$(‘#toWord‘).click(function(){ setTimeout(function(){ var time=‘<?php echo $time ?>‘; var style = document.getElementsByTagName(‘style‘)[0].innerHTML; style="<style>"+style+"</style>"; //console.log(style); var t = document.getElementById(‘printtext‘).innerHTML;//获取要生成的word的内容 t = b.encode(style+t);//加密操作 $.ajax({ url:"get.php", data:{"html":t,"time":time},//用post方式提交到get.php type:‘post‘, success:function(data){ console.log(data);//返回过来的是路径,本地和服务器上有所不同 var url = "http"+data.substr(data.lastIndexOf(":")); console.log(url); // window.history.go(0); // window.location.href=http://www.mamicode.com/url; } }); },2000); });
get.php的代码是这样的:
<?php if(isset($_POST[‘html‘])) { $html=$_POST[‘html‘]; } if(isset($_POST[‘time‘])){ $time = $_POST[‘time‘]; } // echo $html; $html = base64_decode($html); class word { function save($path, $html) { $data = ‘<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 http-equiv="Content-Type" content="text/html; charset=utf-8"/> <xml><w:WordDocument><w:View>Print</w:View> </w:WordDocument> </xml> <style> @page WordSection1//这里可以修改导出word后的一些样式 { size:595.3pt 841.9pt; margin:72.0pt 54.0pt 72.0pt 54.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; } span{ font-size:10.0pt!important; } div.WordSection1 {page:WordSection1;} </style> </head><body><div class="WordSection1">‘ . $html . ‘</div></body></html>‘; $this->wirtefile($path,$data); } function wirtefile ($fn,$data) { $fp=fopen($fn,"wb"); fwrite($fp,$data); fclose($fp); } } $word = new word(); // $html = "aaa".$i; $wordname = ‘taiyuanReports/taiyuan‘.$time.‘.doc‘; $word->save($wordname, $html); // ob_flush();//每次执行前刷新缓存 // flush(); $url=‘http://‘.$_SERVER[‘SERVER_NAME‘].$_SERVER["REQUEST_URI"]; // 127.0.0.1/palm/new2/module/report/taiyuan/get.php; echo substr($url,0,-7).$wordname; // echo $url; // $url = ‘http://127.0.0.1/palm/new2/module/report/taiyuan/‘; // echo $url . $wordname; ?>
另外,上传的服务器后还需要有权限才能成功.
将html页面导出为word
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。