用PHP读取xlsx文件

时间:2011-09-15 08:05:45

标签: php

我正在关注this tutorial以阅读xlsx文件格式。我在读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;
}

2 个答案:

答案 0 :(得分:5)

解决。只需添加此行即可。 $xml->formatOutput = true;这里有完整的代码。

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

答案 1 :(得分: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;
相关问题