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


PHPExcel Get cells ignoring related sheets

我有一个.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

这是包含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函数参数与任何其他语言一样,用,分隔。

我认为公式应该去:

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

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