2016-06-07 181 views
0

我有問題,顯示的內容或docx文件在php(laravel 5)
我沒有找到任何解決方案來顯示內容。我用phpword lib閱讀,我讀了phpword的文檔,但沒有找到解決方案。
這裏是我的代碼:
+上傳HTML:show doc,docx文件內容php

<form method="post" action="doc/upload" enctype="multipart/form-data"> 
      {{csrf_field()}} 
      <input type="file" name="file" accept=".doc, .docx"/> 

      <button class="btn btn-primary" style="margin-top: 5px"><span class="glyphicon glyphicon-import" aria-hidden="true"></span> Upload</button> 
     </form> 

+過程:

public function upload(Request $request){ 
    $file = $request->file('file'); 
    $phpWord = \PhpOffice\PhpWord\IOFactory::load($file); 
    //i use this line below for showing but it can not show exactly 
    $phpWord->save('php://output'); 
} 

+結果:
enter image description here

+0

您缺少標題信息,即瀏覽器沒有獲得足夠的有關如何處理輸出的信息。檢查http://stackoverflow.com/questions/33872714/php-word-send-generated-file-to-browser-as-output-without-saving-it-on-disc的類似問題。 – ejuhjav

回答

2

如果你想顯示所有Word文檔contetns ,那麼最好的辦法是將word文件保存爲html,然後在iframe中顯示html內容。

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML'); 
$objWriter->save('doc.html'); 

如果你想抓住剛剛從文檔的純文本內容,你可以嘗試這樣的事情

foreach($phpWord->getSections() as $section) { 
    foreach($section->getElements() as $element) { 
     if(method_exists($element,'getText')) { 
      echo($element->getText() . "<br>"); 
     } 
    } 
} 

希望這有助於。

2

我也在尋找這個答案,這裏是我的代碼使用PHPWord讀取docx文件。我正在完成安裝步驟(這是針對laravel 5)

第1步。在composer文件中添加https://github.com/PHPOffice/PHPWord。 (composer.json)

"require": { 
     "phpoffice/phpword": "v0.13.*" 
    } 

第2步。添加到您的控制器的IOFactory。

use PhpOffice\PhpWord\IOFactory; 

第3步。創建一個閱讀器並加載該文件。

$phpWord = IOFactory::createReader('Word2007')->load($request->file('file')->path()); 

第4步。您可以檢查$ phpWord文檔的屬性和內容。

第5步。如果你想提取文件的內容。使用下面的代碼

$phpWord = IOFactory::createReader('Word2007')->load($request->file('file')->path()); 

foreach($phpWord->getSections() as $section) { 
      foreach($section->getElements() as $element) { 
       if(method_exists($element,'getText')) { 
        echo($element->getText() . "<br>"); 
       } 
      } 
     } 
+0

謝謝,對於我的恥辱,我無法找到關於閱讀Word文檔的任何高級文檔,您是否有任何鏈接? –

+0

@SergeyNeskhodovskiy,對不起,沒有任何文檔,我剛剛在網上找到了解決方案,試錯了。 –