1 <?php 2 //header("Content-type:text/html;charset=utf-8"); 3 //word轉html 展示 4 $lj=$_GET['file'];//傳來的是文件位置 具體看自己的傳值 upload/user/20170306/20170306xgtlne.doc 5 $lj=str_replace("/",'\\',$lj);//把路徑改為\號 例如 upload\user\20170306\20170306xgtlne.doc 6 7 function word2html($wordname,$htmlname) 8 { 9 $word = new COM("word.application") or die("Unable to instanciate Word"); 10 $word->Visible = 1; 11 $word->Documents->Open($wordname); 12 $word->Documents[1]->SaveAs($htmlname,8); 13 $word->Quit(); 14 $word = null; 15 unset($word); 16 17 } 18 //服務器或本地的word具體位置 例如'D:\phpStudy\WWW\GongshuUnion\.upload\user\20170306\20170306xgtlne.doc' 19 $address='D:\phpStudy\WWW\GongshuUnion\\'; 20 word2html($address.$lj,$address.$lj.".html"); 21 //跳轉時可以在后最加上.html 22 //例如 $url= http://localhost/GongshuUnion/upload/user/20170306/20170306xgtlne.doc.html 23 //我這里一共傳了兩個值 一個是 $_GET['file'] = upload/user/20170306/20170306xgtlne.doc 24 //另一個是$_GET['url']=http://localhost/GongshuUnion/upload/user/20170306/20170306xgtlne.doc.html 25 $url= $_GET['url']; 26 Header("Location:$url"); 27 ?>
希望你們能看的懂。
本地最好要支持office word 組件不然不能完美轉換,wps沒有api。
先確認com模塊是不是開啟,phpinfo里面如果有com_dotnet模塊,說明已開啟,如果沒有,修改php.ini,
1
|
com.allow_dcom = true
|
前面的注釋去掉,重啟就OK了,php官方網站說,php5.4.5之前,com模塊是內置的,其實也不一定全是,官網下的php 5.3.39,com模塊就沒有內置。
如果不是內置模塊的話,php.ini加上,前提你的ext文件夾下,有該擴展
1
|
extension=php_com_dotnet.dll
|
然后重啟就OK了
部分代碼轉載(http://www.jb51.net/article/78492.htm)
最近又找了幾個方法我把他寫在這里是實現在線預覽office的
<a href="http://ow365.cn/?i=12592&furl=http://abc.qybk.cn/{$file.file}" target="_blank">查看</a> //一天免費查看500次 優點速度快 非會員 <a href="http://api.idocv.com/view/url?url=http://abc.qybk.cn/{$file.file}" target="_blank">查看</a>//每個文件能看只能看5次 好像沒有每次限制就是有每個文件查看次數限制? 非會員 <a href="http://view.officeapps.live.com/op/view.aspx?src=http://abc.qybk.cn/{$file.file}" target="_blank">查看</a> //微軟官方自帶的
主要實現方法自己看我發的鏈接 除了第一個要注冊試用 其他全部可以免費使用
第一個超鏈接http://www.officeweb365.com/ furl=是自己域名下文件的地址
第二個超鏈接https://www.idocv.com/ url=是自己域名下文件的地址
第三個使用微軟提供的Office Online平台只需要一個網址即可在線查看Xls,doc,PPT等文檔
這些都可以實現Xls,doc,PPT等文檔在線預覽