no_freedom
no_freedom

Reputation: 1961

Reading xlsx file in PHP

I'm following this tutorial to read the xlsx file format. I'm reading xlsx file. Working fine. But it display all the file content in one line. How to add space between them? Thanks Here is my code.

    $file_upload = 'book.zip';
$zip = new ZipArchive;
// the string variable that will hold the file content
$file_content = " ";
// the uploaded file
//$file_upload = $file -> upload["tmp_name"];
if ($zip -> open($file_upload) === true) {
  // loop through all slide#.xml files
  if ( ($index = $zip -> locateName("xl/sharedStrings.xml")) !== false ) {
                    $data = $zip -> getFromIndex($index);

                    $xml = DOMDocument::loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);

                    $file_content = strip_tags($xml -> saveXML());
              }
echo $file_content;
}

Upvotes: 6

Views: 4222

Answers (2)

Scott Arciszewski
Scott Arciszewski

Reputation: 34093

Try this? Tested on PHP 5.5.3

$file_upload = 'book.zip';
$zip = new ZipArchive;
$dom = new DOMDocument;
// the string variable that will hold the file content
$file_content = " ";
// the uploaded file
//$file_upload = $file -> upload["tmp_name"];
if ($zip->open($file_upload) === true) {
    // loop through all slide#.xml files
    $index = $zip->locateName("xl/sharedStrings.xml");
    if ($index !== false) {
        $data = $zip->getFromIndex($index);
        $dom->loadXML(
            $data,
            LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING
        );
        $dom->formatOutput = true;
        $file_content = strip_tags($dom->saveXML());
    }
}
echo $file_content;

Upvotes: 0

no_freedom
no_freedom

Reputation: 1961

Solved. Just add this line. $xml->formatOutput = true; Full code here.

        $file_upload = 'book.zip';
$zip = new ZipArchive;
// the string variable that will hold the file content
$file_content = " ";
// the uploaded file
//$file_upload = $file -> upload["tmp_name"];
if ($zip -> open($file_upload) === true) {
  // loop through all slide#.xml files
  if ( ($index = $zip -> locateName("xl/sharedStrings.xml")) !== false ) {
                    $data = $zip -> getFromIndex($index);
                    $xml->formatOutput = true;
                    $xml = DOMDocument::loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);

                    $file_content = strip_tags($xml -> saveXML());
              }
echo $file_content;

Upvotes: 6

Related Questions