PHP表格读取速度对比,PhpSpreadsheet PHPExcel fgetcsv读取大文件对比。 单读取推荐 PHPExcel,PhpSpreadsheet支持功能更多一些,csv文件 推荐 使用fgetcsv 单独出来处理。
PHP 7.3 环境
处理数据对比
条数 | 文件类型 | PhpSpreadsheet | PHPExcel | fgetcsv |
2000 | .csv | 2.778 秒 | 0.288 秒 | 0.048 秒 |
100000 | .csv | 超时 | 12.652 秒 | 4.133 秒 |
2000 | .xlsx | 0.721 秒 | 0.356 秒 | - |
100000 | .xlsx | 28.773 秒 | 15.631 秒 | - |
PhpSpreadsheet
require "vendor/autoload.php";
$inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($inputFileName);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
if ($inputFileType == 'Csv') $reader->setInputEncoding('GB2312');
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($inputFileName);
$sheet = $spreadsheet->getSheet(0);
$highestRow = $sheet->getHighestRow();
$workSheet = $spreadsheet->getActiveSheet();
PHPExcel
require_once '../PHPExcel/PHPExcel.php';
require_once '../PHPExcel/PHPExcel/IOFactory.php';
$ext = strtolower(pathinfo($inputFileName, PATHINFO_EXTENSION));
if ($ext == 'xlsx') {
$reader = PHPExcel_IOFactory::createReader('Excel2007');
} elseif ($ext == 'xls') {
require_once '../PHPExcel/PHPExcel/Reader/Excel5.php';
$reader = PHPExcel_IOFactory::createReader('Excel5');
} elseif ($ext == 'csv') {
$reader = PHPExcel_IOFactory::createReader('CSV');
$reader->setInputEncoding('GB2312');
}
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($inputFileName);
$sheet = $spreadsheet->getSheet(0);
$highestRow = $sheet->getHighestRow();
$workSheet = $spreadsheet->getActiveSheet();