簡體   English   中英

如何使用PHP讀取編輯pptx / docx / xlsx文件?

[英]how can I read write edit pptx/docx/xlsx files using PHP?

是否有可用於使用PHP有效處理pptx / docx / xlsx文件的庫擴展? 截至目前,我對PPTX文件更感興趣。 謝謝

據我所知,那些文件格式docx,xl​​sx,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庫?

當我從上面提到的相關stackoverflow問題中讀到時,您可以使用phpdocx,或者其他一些名為PHPWord的問題。

希望這可以澄清一些步驟,以幫助您完成您的需求

這是可以只讀取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; 
    }
?> 

沒有一個庫可以處理所有三種格式,但是有單獨的庫可以讀取和/或寫入各種格式。

您可以使用OpenTBS PHP工具從模板構建docx / xlsx / pptx文檔。

目前正在開發的版本將改進XLSX的支持。

我從來沒有使用過那些文檔,但是如果您的服務器在Windows平台上運行,您可以嘗試使用.netCOM庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM