將html頁面匯出為word

來源:互聯網
上載者:User

標籤:har   pre   ade   type   上傳   base64   log   還需要   pap   

利用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=url;                        }                    });                },2000);            });

get.php的代碼是這樣的:

<?phpif(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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.