【发布时间】:2015-03-03 19:16:05
【问题描述】:
我有一个包含两张表的 .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>";
}
}
由于它们的依赖性,这样我就可以混合使用两张纸。已尝试删除第一张工作表,但这样做会导致第二张工作表的列中引用损坏。
提前致谢。
编辑:
这是导致“问题”通过 PHP 获取单元格值的列 B 单元格的公式:
=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()
);
}
【问题讨论】:
-
您是否尝试过调用
getCalculatedValue()而不是getOldCalculatedValue()? -
是的,我这样做了,但是由于在 cell.php 文件中使用 =& 的引用,它返回了一个异常。我试图替换它但没有成功,所以我尝试了
getOldCalculatedValue()。也尝试了getValue(),但它返回了包含所有公式的单元格内容。 -
你能告诉我们那个原始公式吗?在引用另一张纸时,我有一些粗略的经验:)
-
你能解释一下
=&作为公式的含义吗,这不是我熟悉的东西,但是如果我尝试在MS Excel中输入它会返回错误 -
您能否展示一个来自 MS Excel 的单元格值的示例,以及您使用 PHPExcel 获得的这些“两张表的混合”单元格之一的结果
标签: php phpexcel import-from-excel