我正在使用PHPExcel读取Excel文件并将其放入数组中。我正在阅读.xlsx
和.xls
个文件。我写了xlsExtract
和xlxsExtract
函数。
功能是:
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
。
任何人都可以帮助我。哪里我错了。
谢谢。