2016-06-07 182 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,对不起,没有任何文档,我刚刚在网上找到了解决方案,试错了。 –