gpt4 book ai didi

PHPExcel - 使用示例读取过滤器

转载 作者:可可西里 更新时间:2023-11-01 01:04:39 26 4
gpt4 key购买 nike

我刚刚开始使用 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 的大约十二个专栏的内容,但我也无法弄清楚。

非常感谢您的帮助。

最佳答案

range('A','AA'); 无效尝试创建您自己的自定义范围

例子

echo "<pre>";
print_r(xrange('AA', 'ZZ'));

使用的函数

function xrange($start, $end, $limit = 1000) {
$l = array();
while ($start !== $end && count($l) < $limit) {
$l[] = $start;
$start ++;
}
$l[] = $end;
return $l;
}

See Live Demo

关于PHPExcel - 使用示例读取过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14113870/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com