how can I read write edit pptx/docx/xlsx files usi

2019-02-10 16:12发布

问题:

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

回答1:

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



回答2:

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.

  • PHPPowerpoint can write, but not read, pptx files
  • PHPWord can write, but not read, docx files
  • PHPLiveDocx can write (and I believe also reads) docx files
  • PHPExcel can read and write xlsx files


回答3:

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


回答4:

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.



回答5:

I never used those and their documentation is lacking, but you could try with the .net and COM libraries if your server is running on a Windows platform.