如何限制PHPExcel读取的列?

时间:2014-07-22 18:13:27

标签: php phpexcel

我有一个很大的excel文件(两个工作表,大约4000行,列到AF)。 excel表的格式非常糟糕,我无法控制它。有数百个空白列一直到XFC。

如何让PHPExcel根据下面的代码选择读/写哪些列?我尝试使用文档所说的内容,但显然无效。

代码:

<?php 

require('./Classes/PHPExcel/IOFactory.php');


ini_set('max_execution_time', 800);
ini_set('memory_limit', 200M);  

$inputFileType = 'Excel2007';
$inputFileName = $_FILES['uploaded']['tmp_name'];

//٧٧ this is what documentation suggested ٧٧//
class MyReadFilter implements PHPExcel_Reader_IReadFilter {     
    public function readCell($column, $row, $worksheetName = '') {
          // Read columns from 'A' to 'AF'
          if ($column >= '0' && $column <= '32'){
              return true;
          }
          return false;
      }
}
//^^this is what documentation suggested^^//

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcelReader = $objReader->load($inputFileName);

$loadedSheetNames = $objPHPExcelReader->getSheetNames();

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');

foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
                          $objWriter->setSheetIndex($sheetIndex);
                          $objWriter->save('abc.csv');}

$files = fopen('abc.csv', 'r'); 
    while (($line = fgetcsv($files)) !== FALSE) {
    $csv_array[] = array_combine(range(1, count($line)), array_values($line));
    }

?>

1 个答案:

答案 0 :(得分:3)

简单地创建一个类是不够的:你需要告诉PHPExcel实际使用你的MyReadFilter

/**  Create an Instance of the Read Filter  **/ 
$filterSubset = new MyReadFilter(); 

/**  Create a new Reader of the type defined in $inputFileType  **/ 
$objReader = PHPExcel_IOFactory::createReader($inputFileType); 
/**  Tell the Reader that we want to use the Read Filter  **/ 
$objReader->setReadFilter($filterSubset); 
/**  Load only the rows and columns that match our filter to PHPExcel  **/ 
$objPHPExcel = $objReader->load($inputFileName); 

请参阅Reading Only Specific Columns and Rows from a File (Read Filters)文档的第5.3节 - PHPExcel User Documentation - Reading Spreadsheet Files -

修改

如果您正在使用列,那么您需要使用列字母,因为传入readfilter的列ID是列ID,而不是列号

转换为数字(如果您在readCell()中执行此操作,则效率低下):

class MyReadFilter implements PHPExcel_Reader_IReadFilter {
    public function readCell($column, $row, $worksheetName = '') {
          // Read columns from 'A' to 'AF'
          if (PHPExcel_Cell::columnIndexFromString($column) -1 >= 0) &&
              PHPExcel_Cell::columnIndexFromString($column) -1 <= 32) {
              return true;
          }
          return false;
      }
}

作为列ID进行比较

class MyReadFilter implements PHPExcel_Reader_IReadFilter {

    public function __construct($fromColumn, $toColumn) {
        $this->columns = array();
        $toColumn++;
        while ($fromColumn !== $toColumn) {
            $this->columns[] = $fromColumn++;
        }
    }

    public function readCell($column, $row, $worksheetName = '') {
          // Read columns from 'A' to 'AF'
          if (in_array($column, $this->columns)) {
              return true;
          }
          return false;
      }
}

并使用以下方法实例化读取过滤器:

$filterSubset = new MyReadFilter('A', 'AF');