简体   繁体   中英

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

Is there a library extension available for efficiently handling pptx/docx/xlsx files using PHP? As of now I am more interested in PPTX files. Thanks

As per what i know, those file formats docx,xlsx,pptx are just zip files. they belong to Office Open XML (OOXML) standard.

In PHP we have this library for manipulating this type of zip documents: http://php.net/manual/en/book.zip.php

You can find all documentation about this ooxml standard here: http://www.ecma-international.org/publications/standards/Ecma-376.htm

The best way to test the structure of these ooxml file is to change the file extension to .zip, and the extract it out to find out what are inside.

If you don't want to build your own library for processing ooxml files, you can refer to a related question here for more info: PHP OOXML Libraries?

As i read from the related stackoverflow question mentioned above, you can use the phpdocx, or somewhat other called PHPWord.

Hope this may clarify some steps to help get your wants done

here is the working example that can read only docx file.

<?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; 
    }
?> 

There is no one library that can handle all three formats, but there are individual libraries that can read and/or write the individual formats.

You can build docx/xlsx/pptx documents from templates using the OpenTBS PHP tool.

The version currently in development will improve the support of XLSX.

我从来没有使用过那些文档,但是如果您的服务器在Windows平台上运行,您可以尝试使用.netCOM库。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM