2011-09-15 48 views
5

Tôi đang theo dõi this tutorial để đọc định dạng tệp xlsx. Tôi đang đọc tập tin xlsx. Làm việc tốt. Nhưng nó hiển thị tất cả các nội dung tập tin trong một dòng. Làm thế nào để thêm không gian giữa chúng? Cảm ơn Đây là mã của tôi.Đọc tệp xlsx bằng 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; 
} 

Trả lời

5

Đã giải quyết. Chỉ cần thêm dòng này. $xml->formatOutput = true; Toàn bộ mã tại đây.

 $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

Hãy thử cái này? Thử nghiệm trên 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; 
Các vấn đề liên quan