Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading xlsx file in PHP

Tags:

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;
}
like image 595
no_freedom Avatar asked Sep 15 '11 08:09

no_freedom


1 Answers

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;
like image 83
no_freedom Avatar answered Sep 24 '22 12:09

no_freedom