PHPExcel获取单元格忽略相关工作表

时间:2015-03-03 19:16:05

标签: php phpexcel import-from-excel

我有一个带有两张纸的.xlsx文件,我想将第二张纸提取到PHP,就像它一样。问题是这张表中的一列与第一张相关,所以当我打印提取的值时得到的结果是第二张和第一张相关行之间的某种混合。

我不知道如何向您展示Excel,但我会展示代码,希望有人已经有类似的问题。

public function excel()
{
    error_reporting(E_ALL ^ E_NOTICE);

    #system variables
    header('Content-Type: text/html; charset=utf-8');
    set_time_limit(0);
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    ini_set('memory_limit', '128M');

    #includes
    include FCPATH.'application/libraries/phpexcel/PHPExcel.php';
    set_include_path(FCPATH.'application/libraries/phpexcel/PHPExcel/'); //INCLUDE DOS FICHEIROS DO PHPEXCEL
    require_once 'IOFactory.php';

    $inputFileName = BASE_DIR.'assets/misc/cc_v2.xlsx'; 

    //$reader = PHPExcel_IOFactory::createReaderForFile($inputFileName);
    $reader = PHPExcel_IOFactory::createReader('Excel2007');
    $reader->setReadDataOnly(true);
    $objPHPExcel = $reader->load($inputFileName);

    $objPHPExcel->setActiveSheetIndex(1);

    $objWorksheet = $objPHPExcel->getActiveSheet();
    $highestRow = $objWorksheet->getHighestRow(); 
    $highestColumn = $objWorksheet->getHighestColumn(); 

    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

    for ($row = 1; $row <= $highestRow; ++$row) 
    {
        for ($col = 0; $col <= $highestColumnIndex; ++$col) 
        {
            echo $objWorksheet->getCellByColumnAndRow($col, $row)->getOldCalculatedValue()."\n";
        }
        echo "<br>";
    }
}

这样,由于它们的依赖性,我得到了两张纸的混合。已经尝试删除第一张纸,但这样做会导致第二张纸的列具有损坏的参考。

提前致谢。

编辑:

以下是列B单元格中的公式导致“问题”通过PHP获取单元格的值:

=IF(ISNA(A8);"";VLOOKUP(A8;Clientes!$B:$C;2;FALSE))

“Clientes”是第一张表的名称。

总之,表单1有一个客户列表,表2列有所有费用。 表2中的B列是客户端的名称,但是当通过PHPExcel获取它时,我从表单1中获取了多个值,而不是从表2中的B列中看到的值。

例如,单元格B3具有从第一张纸张返回的值“BPI”。

编辑2:

当使用getCalculatedValue()运行代码时,我收到以下错误:

Fatal error: Uncaught exception 'PHPExcel_Calculation_Exception' with message 'CONTA!B2 -> Formula Error: An unexpected error occured' in /home/clientes/versions/v3/application/libraries/phpexcel/PHPExcel/Cell.php:300 Stack trace: #0 /home/clientes/versions/v3/application/controllers/pm_dashboard.php(269): PHPExcel_Cell->getCalculatedValue() #1 [internal function]: Pm_dashboard->excel('fyi', 'drKzj1ykfqUXXBV...') #2 /home/clientes/versions/v3/system/core/CodeIgniter.php(359): call_user_func_array(Array, Array) #3 /home/clientes/versions/v3/index.php(209): require_once('/home/clientes/...') #4 {main} thrown in /home/clientes/versions/v3/application/libraries/phpexcel/PHPExcel/Cell.php on line 300

以下是if包含Cell.php的第300行:

if ($this->_dataType == PHPExcel_Cell_DataType::TYPE_FORMULA) {
        try {
            //echo 'Cell value for '.$this->getCoordinate().' is a formula: Calculating value'.PHP_EOL;
            $result = PHPExcel_Calculation::getInstance(
                $this->getWorksheet()->getParent()
            )->calculateCellValue($this,$resetLog);
            //echo $this->getCoordinate().' calculation result is '.$result.PHP_EOL;
            //  We don't yet handle array returns
            if (is_array($result)) {
                while (is_array($result)) {
                    $result = array_pop($result);
                }
            }
        } catch ( PHPExcel_Exception $ex ) {
            if (($ex->getMessage() === 'Unable to access External Workbook') && ($this->_calculatedValue !== NULL)) {
                //echo 'Returning fallback value of '.$this->_calculatedValue.' for cell '.$this->getCoordinate().PHP_EOL;
                return $this->_calculatedValue; // Fallback for calculations referencing external files.
            }
            //echo 'Calculation Exception: '.$ex->getMessage().PHP_EOL;
            $result = '#N/A';
            #NEXT IS LINE 300
            throw new PHPExcel_Calculation_Exception(
                $this->getWorksheet()->getTitle().'!'.$this->getCoordinate().' -> '.$ex->getMessage()
            );
        }

1 个答案:

答案 0 :(得分:0)

好的,看了你的公式后:

=IF(ISNA(A8);"";VLOOKUP(A8;Clientes!$B:$C;2;FALSE))

我注意到了一个但重复的事情:你的参数用分号(;)分隔,而Excel函数参数和任何其他语言一样,用,分隔。

我认为那个公式应该去:

=IF(ISNA(A8),"",VLOOKUP(A8,Clientes!$B:$C,2,FALSE))

另一件事:VLOOPUP是不是应该将单元格范围作为第一个参数? $B:$C对我来说很奇怪,但我不是Excel专家:)