显示文档、docx文件内容php
我在显示文档、docx文件内容php,php,laravel-5,phpword,Php,Laravel 5,Phpword,我在php(laravel 5)中显示内容或docx文件时遇到问题 我没有找到任何解决方案来显示内容。我使用phpwordlib阅读,我阅读了phpword的文档,但没有找到解决方案。 这是我的代码: +以html格式上载: <form method="post" action="doc/upload" enctype="multipart/form-data"> {{csrf_field()}} <input type="fil
php(laravel 5)
中显示内容或docx
文件时遇到问题我没有找到任何解决方案来显示内容。我使用
phpword
lib阅读,我阅读了phpword的文档,但没有找到解决方案。这是我的代码:
+以html格式上载:
<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>
+结果:如果要显示所有word文档内容,最好的方法是将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>");
}
}
}
foreach($phpWord->getSections()作为$section){
foreach($section->getElements()作为$element){
如果(方法_存在($element,'getText')){
echo($element->getText()。“
”);
}
}
}
希望这有帮助。如果您想显示所有word文档内容,那么最好的方法是将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>");
}
}
}
foreach($phpWord->getSections()作为$section){
foreach($section->getElements()作为$element){
如果(方法_存在($element,'getText')){
echo($element->getText()。“
”);
}
}
}
希望这能有所帮助。我也在寻找答案,下面是我使用PHPWord读取docx文件的代码。我正在完成安装步骤(这是针对laravel 5的) 第一步。添加到您的composer文件中。(composer.json) 第二步。将IOFactory添加到控制器中
use PhpOffice\PhpWord\IOFactory;
第三步。创建读取器并加载文件
$phpWord = IOFactory::createReader('Word2007')->load($request->file('file')->path());
第四步。您已经可以在$phpWord中查看文档的属性和内容
第五步。如果要提取文档的内容。使用下面的代码
$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>");
}
}
}
$phpWord=IOFactory::createReader('Word2007')->load($request->file('file')->path());
foreach($phpWord->getSections()作为$section){
foreach($section->getElements()作为$element){
如果(方法_存在($element,'getText')){
echo($element->getText()。“
”);
}
}
}
我也在寻找这个问题的答案,下面是我使用PHPWord读取docx文件的代码。我正在完成安装步骤(这是针对laravel 5的)
第一步。添加到您的composer文件中。(composer.json)
第二步。将IOFactory添加到控制器中
use PhpOffice\PhpWord\IOFactory;
第三步。创建读取器并加载文件
$phpWord = IOFactory::createReader('Word2007')->load($request->file('file')->path());
第四步。您已经可以在$phpWord中查看文档的属性和内容
第五步。如果要提取文档的内容。使用下面的代码
$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>");
}
}
}
$phpWord=IOFactory::createReader('Word2007')->load($request->file('file')->path());
foreach($phpWord->getSections()作为$section){
foreach($section->getElements()作为$element){
如果(方法_存在($element,'getText')){
echo($element->getText()。“
”);
}
}
}
对于那些正在寻找将Microsoft Word文档转换为纯文本的更简单(在我的情况下,更准确)的方法的人,我建议看一下这里的这个帖子:对于那些正在寻找将Microsoft Word文档转换为纯文本的更简单(在我的情况下,更准确)的方法的人,我建议您看看这里的这个帖子:您缺少标题信息,即浏览器没有获得足够的关于如何处理输出的信息。检查类似问题。您缺少标题信息,即浏览器没有获得有关如何处理输出的足够信息。检查类似的问题。谢谢,遗憾的是,我找不到任何关于阅读Word文档的高级文档,你有没有链接?@SergeyNeskhodovskiy,对不起,没有任何文档,我只是在网上找到了解决方案,并进行了反复试验。文档在这里我想:谢谢,遗憾的是,我找不到任何关于阅读Word文档的高级文档,你有链接吗?@SergeyNeskhodovskiy,对不起,没有任何文档,我只是在网上找到了解决方案,并进行了反复试验。文档在这里,我想: