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

PHPExcel Get cells ignoring related sheets

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

我不确定如何向您展示 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>";
    }
}

这样我就得到了两个 sheet 的组合,因为它们的依赖性。已经尝试删除第一个 sheet,但这样做会导致第二个 sheet 的列中包含损坏的引用。

提前致谢。

编辑:

这是导致 "problem" 通过 PHP 获取单元格值的列 B 单元格的公式:

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

"Clientes"是第一个sheet的名字。

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

的 B 列

例如,单元格 B3 具有从第一个 sheet.

返回的值 "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

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

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()
            );
        }

好的,看了你的公式后:

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

我注意到一个单一但重复的事情:您的参数由分号 (;) 分隔,而 Excel 函数参数与任何其他语言一样,由 [=14= 分隔].

我认为公式应该是:

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

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