没有在PHPEXCEL中放置正确的数组格式XLS文件

时间:2013-10-03 05:43:38

标签: php arrays phpexcel

我正在使用PHPExcel读取Excel文件并将其放入数组中。我正在阅读.xlsx.xls个文件。我写了xlsExtractxlxsExtract函数。

功能是:

function xlsxExtract() 
{  

    $inputFileType = PHPExcel_IOFactory::identify($this->filepath); /*file path */
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);  
    $objReader->setReadDataOnly(true);

    /**  Load $inputFileName to a PHPExcel Object  **/  
    $objPHPExcel = $objReader->load($this->filepath);  
    $total_sheets=$objPHPExcel->getSheetCount(); // here 4  
    $allSheetName=$objPHPExcel->getSheetNames(); // array ([0]=>'student',[1]=>'teacher',[2]=>'school',[3]=>'college')  
    $objWorksheet = $objPHPExcel->setActiveSheetIndex(0); // first sheet  
    $highestRow = $objWorksheet->getHighestRow(); 
    $highestColumn = $objWorksheet->getHighestColumn();  
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);  
    for ($row = 1; $row <= $highestRow; ++$row) 
    {  
        for ($col = 0; $col <$highestColumnIndex; ++$col)
        {  
            $value=$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();  
            if(is_array($this->arraydata) ) 
            { 
                $this->arraydata[$row-1][$col]=$value; 
            }  
        }  
    }
    print_r($this->arraydata); /*array display*/ 

}   



function xlsExtract() 
{
$inputFileType = PHPExcel_IOFactory::identify($this->filepath);  
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);  
    $objReader->setReadDataOnly(true);

    /**  Load $inputFileName to a PHPExcel Object  **/  
    $objPHPExcel = $objReader->load($this->filepath);  
    $total_sheets=$objPHPExcel->getSheetCount(); // here 4  
    $allSheetName=$objPHPExcel->getSheetNames(); // array ([0]=>'student',[1]=>'teacher',[2]=>'school',[3]=>'college')  
    $objWorksheet = $objPHPExcel->setActiveSheetIndex(0); // first sheet  
    $highestRow = $objWorksheet->getHighestRow(); 
    $highestColumn = $objWorksheet->getHighestColumn();  
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);  
    for ($row = 1; $row <= $highestRow; ++$row) 
    {  
        for ($col = 0; $col <$highestColumnIndex; ++$col)
        {  
            $value=$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();  
            if(is_array($this->arraydata) ) 
            { 
                $this->arraydata[$row-1][$col]=$value; 
            }  
        }  
    }
    print_r($this->arraydata);

}

我的xlsExtract给了我这样的输出:

Array ( [0] => Array ( [0] =>Name city Address  ) [1] => Array ( [0] => abc X wxy ) [2] => Array ( [0] => bb Y mno ) 

和我的xlsxExtract给我这样的输出:

Array ( [0] => Array ( [0] => Name [1] => City [2] => Addres ) [1] => Array ( [0] => AA [1] => XYZ [2] => xxx) 

我希望我的数组输出像第二个一样。那是xlsxExtract

任何人都可以帮助我。哪里我错了。

谢谢。

0 个答案:

没有答案