无法使用PHPexcel脚本读取数据或可配置读取过滤器到指定的颜色和行


unable to read data or Configurable Read Filter to a specified col and row using PHPexcel script

我试过使用PHPExcel脚本从excel文件中提取数据的示例文件。数据提取成功,但没有受到限制。

关于这个问题的详细信息:这个(下面提到的)文件必须读取具有特定行数的excel文件到一个范围列,即(' a ','Z')。行被计算在内,但列不被计算在内。它只在列的任何指定范围内占用列0。甚至我已经测试了提供更多的列范围,它显示从列"A"到列"O"的范围,不超过"O"

谁能把这个问题整理一下,如果我错了请告诉我。

等待您宝贵的建议和意见。

等待最早的回复

提前感谢,

Jimson何塞,

所有代码如下所示

<?php
error_reporting(E_ALL);
set_time_limit(0);
date_default_timezone_set('Europe/London');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHPExcel Reader Example #10</title>
</head>
<body>
<h1>PHPExcel Reader Example #10</h1>
<h2>Simple File Reader Using a Configurable Read Filter</h2>
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '/import/Classes/');
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
$inputFileType = 'Excel2007';
$inputFileName = './book2.xlsx';
$sheetname = 'Sheet2';
class MyReadFilter implements PHPExcel_Reader_IReadFilter
{
    private $_startRow = 0;
    private $_endRow = 0;
    private $_columns = array();
    public function __construct($startRow, $endRow, $columns) {
        $this->_startRow    = $startRow;
        $this->_endRow      = $endRow;
        $this->_columns     = $columns;
    }
    public function readCell($column, $row, $worksheetName = '') {
        if ($row >= $this->_startRow && $row <= $this->_endRow) {
            if (in_array($column,$this->_columns)) {
                return true;
            }
        }
        return false;
    }
}
$filterSubset = new MyReadFilter(9,15,range('G','K'));
echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of 
',$inputFileType,'<br />';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
echo 'Loading Sheet "',$sheetname,'" only<br />';
$objReader->setLoadSheetsOnly($sheetname);
echo 'Loading Sheet using configurable filter<br />';
$objReader->setReadFilter($filterSubset);
$objPHPExcel = $objReader->load($inputFileName);
echo '<hr />';
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);
?>
<body>
</html>

toArray()方法将显示从单元格A1到最高加载行和列的工作表,所以即使你没有加载行1-8,它们仍然会在返回的数组中显示为空单元格;同样,列A-F也将在数组中显示为空单元格。

请注意,样式仍然会为工作表中的所有单元格加载,而不仅仅是那些在读取筛选器范围内的单元格。这是因为所有的样式都是在单元格加载之前加载的,而read过滤器只检查单元格。

如果你想重新定义一个只包含你通过过滤器加载的单元格的数组,你应该使用rangeToArray()方法。

$sheetData = $objPHPExcel->getActiveSheet()->rangeToArray('G9:K15',null,true,true,true);