PHPExcel只读取第一行大文件

时间:2018-06-08 07:03:10

标签: php excel phpexcel

我使用PHPExcel库从Excel文件中读取数据。我的文件大约是5mb,70列和20000行。我试图从excel的标题行中删除像(){} [] - * /这样的特殊字符。我使用下面的代码来读取excel文件,但读取文件需要很长时间。

$inputFileName = 'path\file.xlsx';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($inputFileName);
$objWorksheet = $objPHPExcel->getActiveSheet();

$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

for ($row = 1; $row <2; ++$row) {
  for ($col = 0; $col <= $highestColumnIndex; ++$col) {
    $cell = $objWorksheet->getCellByColumnAndRow($col, $row);
    $cellValue = cleanName($cell->getValue());
    $colIndex = PHPExcel_Cell::stringFromColumnIndex($col);
    $objWorksheet->setCellValue($colIndex.$row, $cellValue);
  }

}

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($inputFileName);

是否可以只读取第一行而不是整个excel文件?或者还有其他方法可以从标题行中删除这些字符吗?

0 个答案:

没有答案