如何使用PHP读写编辑pptx/docx/xlsx文件?

5

有没有一个可用于使用PHP高效处理pptx / docx / xlsx文件的库扩展?目前,我对PPTX文件更感兴趣。

5个回答

12
据我所知,docx、xlsx、pptx这些文件格式只是zip文件,它们属于Office Open XML(OOXML)标准。
在PHP中,我们有一个用于处理此类zip文档的库: http://php.net/manual/en/book.zip.php 您可以在此处找到有关此ooxml标准的所有文档: http://www.ecma-international.org/publications/standards/Ecma-376.htm 测试这些ooxml文件结构的最佳方法是将文件扩展名更改为.zip,然后提取出来查看其中内容。
如果您不想构建自己的库以处理ooxml文件,则可以参考相关问题了解更多信息: PHP OOXML Libraries? 根据上述提到的相关stackoverflow问题,您可以使用phpdocx或其他一些称为PHPWord的工具。
希望这些步骤能够澄清一些疑问,帮助您实现所需的功能。

3

没有一种库可以处理所有三种格式,但有单独的库可以读取和/或写入单个格式。

  • PHPPowerpoint可以写入pptx文件,但无法读取。
  • PHPWord可以写入docx文件,但无法读取。
  • PHPLiveDocx可以写入(我认为还可以阅读)docx文件。
  • PHPExcel可以读取和写入xlsx文件。

2
这是一个可以只读取docx文件的工作示例。
<?php
    $filename = "file.docx"; // or /var/www/html/file.docx 

    $content = read_file_docx($filename); 
    if($content !== false) {
        echo nl2br($content); 
    }  
    else {
        echo 'Couldn\'t the file. Please check that file.'; 
    }

    function read_file_docx($filename){ 

        $striped_content = ''; 
        $content = ''; 

        if(!$filename || !file_exists($filename)) return false;  

        $zip = zip_open($filename);

        if (!$zip || is_numeric($zip)) return false; 

        while ($zip_entry = zip_read($zip)) { 

            if (zip_entry_open($zip, $zip_entry) == FALSE) continue; 

            if (zip_entry_name($zip_entry) != "word/document.xml") continue; 

            $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); 

            zip_entry_close($zip_entry); 
        }// end while 

        zip_close($zip); 

        //echo $content; 

        //file_put_contents('1.xml', $content);  

        $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content); 
        $content = str_replace('</w:r></w:p>', "\r\n", $content); 
        $striped_content = strip_tags($content); 

        return $striped_content; 
    }
?> 

好多个<br>!看起来你复制粘贴的时候忘记删除了br。 - Kumar
这是抄袭。代码是从http://www.blogs.zeenor.com/it/read-ms-word-docx-ms-word-2007-file-document-using-php.html简单的复制/粘贴。没有给作者任何信誉。 - Fr0zenFyr

2
你可以使用PHP工具OpenTBS,从模板构建docx / xlsx / pptx文档。
目前正在开发的版本将改进对XLSX的支持。

看起来它可以编辑而不影响其他数据,比如图表...非常好的推荐 :) - Abe Petrillo

0

我从未使用过这些库,它们的文档也比较缺乏,但是如果你的服务器运行在Windows平台上,你可以尝试使用.netCOM库。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接