2

我有以下代码(使用 PHPExcel):

        $inputFileType = 'Excel2007'; 
        $sheetname = 'Upload'; 
        $objReader = PHPExcel_IOFactory::createReader( $inputFileType );
        $objReader->setLoadSheetsOnly( $sheetname ); 
        $objPHPExcel = $objReader->load( $fileName );

            foreach( $objPHPExcel->_workSheetCollection as $columnMeta )
            {
                foreach( $columnMeta->_cellCollection->_cellCache as $columnHeader )
                {
                    echo $columnHeader->_value;
                }    
            }

当然,我不能实际使用这些属性或遍历它们,因为它们是 $objPHPExcel 的私有属性......我如何才能真正从这个数组中获取一些东西?

从 print_r 返回的数组片段:

[_workSheetCollection:PHPExcel:private] => Array
        (
            [0] => PHPExcel_Worksheet Object
                (
                    [_parent:PHPExcel_Worksheet:private] => PHPExcel Object
 *RECURSION*
                    [_cellCollection:PHPExcel_Worksheet:private] => PHPExcel_CachedObjectStorage_Memory Object
                        (
                            [_parent:protected] => PHPExcel_Worksheet Object
 *RECURSION*
                            [_currentObject:protected] => 
                            [_currentObjectID:protected] => AG1
                            [_currentCellIsDirty:protected] => 1
                            [_cellCache:protected] => Array
                                (
                                    [A1] => PHPExcel_Cell Object
4

1 回答 1

1

您必须执行以下操作才能从单元格中获取信息:

$objReader = PHPExcel_IOFactory::createReader( $inputFileType );
$objReader->setLoadSheetsOnly( $sheetname ); // Load specific sheet             
$objPHPExcel = $objReader->canRead( $fileName );
$objPHPExcel = $objReader->load( $fileName );

// Prepare loop to extract values from cells
$worksheet = $objPHPExcel->getActiveSheet();


$cell = $worksheet->getCell( "A1" );
$cell->getValue();
于 2013-10-29T11:32:50.537 回答