php讀取和建立word文檔

來源:互聯網
上載者:User

總結下本人在日常工作中使用php操作word的一些實戰。

 方法一:利用php com模組。也即利用word提供的本地api,所有只適用於windows系統上。

<?php<br />$word = new com('word.application') or die('無法開啟word');<br />$word->Visiable = false;<br />$doc_file = '/path/to/doc';<br />$word->Open($doc_file);<br />$text = '這段文字將被寫到word文檔中去';<br />$word->Selection->TypeText($text);<br />//儲存<br />$word->ActiveDocument->Save();<br />//讀取內容<br />$doc_file_contents = $word->ActiveDocument->Content->Text;<br />//輸出word內容<br />$word->PrintOut();<br />$word->Close();<br />?>

利用com串連word,經常無法預期運行成功,目前也不知道是什麼原因,暫時靠運氣吧。

方法二:利用catdoc。catdoc是linux上的工具,需要自行安裝配置。

<?php<br />//catdoc位置<br />$cat_doc = '/usr/local/bin/catdoc';<br />$doc_file = 'this is a doc file';<br />//讀取word檔案內容<br />$doc_file_contents = shell_exec($cat_doc . ' ' . $doc_file);<br />echo nl2br($doc_file_contents);<br />?>

更多catdoc的功能,請查閱catdoc文檔。

由於catdoc是linux的工具,所以上面的這段代碼只能運行在linux伺服器上,而已必須安裝了catdoc。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.