我正在尝试使用过滤器读取一些工作表,但问题是当这些单元格使用单元格时,我无法获取单元格值
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objReader->setLoadSheetsOnly('Data Sheet #1');
$myFilter = new CellReadFilter(1, 7, range('A', 'F'));
$objReader->setReadDataOnly(true);
$objReader->setReadFilter($myFilter);
$objPHPExcel = $objReader->load('sampleData/example1.xls');
$loadedSheetNames = $objPHPExcel->getSheetNames();
foreach ($loadedSheetNames as $sheetIndex => $loadedSheetName) {
echo $sheetIndex, ' => ', $loadedSheetName, '<br />';
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);
}
我的 excel 文件,在F1
单元格上,我使用以下公式:=C2
,但我的脚本的输出显示 F1 是null
,而不是 23.45 符合预期。
0 => Data Sheet #1
array (size=3)
1 =>
array (size=6)
'A' => string 'Integer Numbers' (length=15)
'B' => float 123
'C' => float 234
'D' => float -345
'E' => float 456
'F' => null
2 =>
array (size=6)
'A' => string 'Floating Point Numbers' (length=22)
'B' => float 1.23
'C' => float 23.45
'D' => float 3.45E-6
'E' => float -45.678
'F' => float 56.78
3 =>
array (size=6)
'A' => string 'Strings' (length=7)
'B' => string 'Hello' (length=5)
'C' => string 'World' (length=5)
'D' => null
'E' => string 'PHPExcel' (length=8)
'F' => null
我的单元过滤器类在文档示例中看起来像:
class CellReadFilter implements PHPExcel_Reader_IReadFilter {
private $_startRow = 0;
private $_endRow = 0;
private $_column = array();
public function __construct($startRow, $endRow, $column) {
$this->_startRow = $startRow;
$this->_endRow = $endRow;
$this->_column = array_merge($column, array('AA'));
}
public function readCell($column, $row, $worksheetName = '') {
if ($row >= $this->_startRow && $row <= $this->_endRow) {
if (in_array($column, $this->_column)) { return true; }
}
return false;
}
}