2011-09-15 104 views
5

我在使用this tutorial來讀取xlsx文件格式。我正在閱讀xlsx文件。工作正常。但它將所有文件內容顯示在一行中。如何在它們之間添加空間?謝謝 這是我的代碼。使用PHP讀取xlsx文件

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

回答

5

已解決。只需添加這一行。 $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; 
0

試試這個嗎?測試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;