我刚刚开始使用 PHPExcel。我的非常大的电子表格无法全部加载到内存中(内存故障)。为了只加载我需要的工作表的部分,我正在尝试使用文档中提供的 MyReadFilter 代码,但代码有点高于我,我希望有人能帮助我理解它。
在 PHPExcel 文档中,函数如下:
class ReadFilter implements PHPExcel_Reader_IReadFilter
{
private $_startRow = 0;
private $_endRow = 0;
private $_columns = array();
/** Get the list of rows and columns to read */
public function __construct($startRow, $endRow, $columns) {
$this->_startRow = $startRow;
$this->_endRow = $endRow;
$this->_columns = $columns;
}
public function readCell($column, $row, $worksheetName = '') {
// Only read the rows and columns that were configured
if ($row >= $this->_startRow && $row <= $this->_endRow) {
if (in_array($column,$this->_columns)) {
return true;
}
}
return false;
}
}
我正在使用以下几行来调用 PHPExcel
// Get the selected Excel file, passed from form
$testFile = $_FILES['upload_test']['tmp_name'];
// Identify the file type of the selected file
$inputFileType = PHPExcel_IOFactory::identify($testFile);
// Create a reader object of the correct file type
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
// Instantiate the filter class and apply it to the reader object
$filterSubset = new ReadFilter(1,1000,range('A','Z'));
$objReader->setReadFilter($filterSubset);
// Load the selected file into the reader
$objWorkbook = $objReader->load($testFile);
我正在使用以下语法从生成的工作表对象中检索数据:
$someValue= $objWorkbook->getSheet($idx)->getCell('B11')->getCalculatedValue();
我敢肯定,我还会有其他问题,但我的第一个问题是关于调用该函数。如果我将上面的行从:
$filterSubset = new ReadFilter(1,1000,range('A','Z'));
至:
$filterSubset = new ReadFilter(1,1000,range('A','AA')); //Last column changed
整个读取失败。我实际上只需要 B 列的计算值,但该列的引用远至 AS 列,所以我也需要阅读它们。有人可以告诉我如何使用这个函数来阅读过去的 Z 列,或者修改它吗?理想情况下,我只想阅读从 B 到 AS 分布的大约十几个列的内容,但我也无法弄清楚。
非常感谢您的帮助。