/**
* Excels
*
* @param string $file
* @param array $sheet_names
*
* @return array|bool
* @throws \PHPExcel_Exception
* @throws \PHPExcel_Reader_Exception
*/
public static function importExcel($file, $sheet_names)
{
foreach ($sheet_names as $sheet_name) {
$rowSize = 200;
$startRow = 1;//
$endRow = $rowSize;
$data = [];
while (true) {
$excel_orders = self::readFromExcel($file, $startRow, $endRow, $sheet_name);
if (empty($excel_orders)) {
break;
}
$data = array_merge($data, $excel_orders);
$startRow = $endRow + 1;
$endRow = $endRow + $rowSize;
}
$result[$sheet_name] = $data;
}
return $result;
}
/**
* excel
*
* @param string $excelFile
* @param int $startRow
* @param int $endRow
* @param string $sheet_name sheet
*
* @return array
* @throws \PHPExcel_Exception
* @throws \PHPExcel_Reader_Exception
*/
private static function readFromExcel($excelFile, $startRow = 1, $endRow = 100, $sheet_name)
{
$excelType = PHPExcel_IOFactory::identify($excelFile);
$excelReader = PHPExcel_IOFactory::createReader($excelType);
if (strtoupper($excelType) == "CSV") {
$excelReader->setInputEncoding("GBK");
}
if ($startRow && $endRow) {
$excelFilter = new PHPExcelReadFilter();
$excelFilter->startRow = $startRow;
$excelFilter->endRow = $endRow;
$excelReader->setReadFilter($excelFilter);
}
$php_excel = $excelReader->load($excelFile);
$activeSheet = $php_excel->getSheetByName($sheet_name);
if ( !$activeSheet) {
return [];
}
$highestColumn = $activeSheet->getHighestColumn(); //1A
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); //
$data = [];
for ($row = $startRow; $row <= $endRow; $rowPP) {
for ($col = 0; $col < $highestColumnIndex; $colPP) {
$data[$row][] = (string)$activeSheet->getCellByColumnAndRow($col, $row)->getFormattedValue();
}
if (implode($data[$row], "") == "") {
unset($data[$row]);
}
}
return $data;
}
:
$data = CommonHelper::importExcel($file, ["sheet", "sheet1", "sheet2", "sheet3"]);
* * when reading, the cpu instant 98 99% excel file is not very large or smaller. This is tested locally, and the effect is the same on the server. The configuration of 4-core 16G is the same. Is there any other way to write
? * *