2011-09-15 18 views
5

Podążam za this tutorial, aby przeczytać format pliku xlsx. Czytam plik xlsx. Dobrze pracować. Ale wyświetla całą zawartość pliku w jednym wierszu. Jak dodać między nimi przestrzeń? Dzięki Oto mój kod.Odczytywanie pliku Xlsx w PHP

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

Odpowiedz

5

Rozwiązany. Po prostu dodaj tę linię. $xml->formatOutput = true; Pełny kod tutaj.

 $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; 
0

Spróbuj tego? Testowany na 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; 
Powiązane problemy