0

我正在使用 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

谁可以帮我这个事。我错了。

谢谢你。

4

0 回答 0