首先,请澄清一下,我已经看到许多有关优化excel文件读取的主题,但是已经应用了一些解决方案,却没有找到对我有帮助的解决方案。
我有一个系统,该系统在某个时刻会调用另一个调用excel库的php。 该系统的工作是读取一个excel文件,并将其信息记录在SQL中,然后生成PDF。
index.php
<?php
....
include("read_excel.php");
....
?>
我已经看到索引的加载大约需要20秒,因此我开始分配代码的一部分以查看问题出在哪里,并确定问题出在“ read_excel.php”文件中
read_excel.php
<?php
$sql_host = "127.0.0.1";
$sql_username = "root";
$sql_password = "";
$sql_database = "payment_locations";
$mysqli = new mysqli($sql_host , $sql_username , $sql_password , $sql_database);
$mysqli->set_charset("utf8");
require_once("_library/PHPExcel/Classes/PHPExcel.php");
$fileName = '_excel/locations.xlsx';
if (!file_exists($fileName)){
?>
<script type="text/javascript">
alertify.alert("No exists file");
</script>
<?php
}else{
$objPHPExcel = PHPExcel_IOFactory::load($fileName);
$date = $objPHPExcel->getActiveSheet()->getCell('U11')->getValue();
$dateFormat = date('d/m/Y', PHPExcel_Shared_Date::ExcelToPHP($date));
// echo $dateFormat;
// $dto = PHPExcel_Shared_Date::ExcelToPHPObject($objPHPExcel->getActiveSheet()->getCell('U11')->getCalculatedValue());
// echo $dto->format('d-m-Y');
for ($i = 11; $i <= 31; $i++) {
if ((float) $objPHPExcel->getActiveSheet()->getCell('A' . $i)->getCalculatedValue() == 0) {
break;
}
$dataExcel[$i]['id_locator'] = $objPHPExcel->getActiveSheet()->getCell('A' . $i)->getCalculatedValue();
$dataExcel[$i]['payment'] = $objPHPExcel->getActiveSheet()->getCell('X' . $i)->getCalculatedValue();
$dataExcel[$i]['period_to'] = PHPExcel_Shared_Date::ExcelToPHPObject($objPHPExcel->getActiveSheet()->getCell('AC' . $i)->getCalculatedValue());
$dataExcel[$i]['period_from'] = PHPExcel_Shared_Date::ExcelToPHPObject($objPHPExcel->getActiveSheet()->getCell('AD' . $i)->getCalculatedValue());
$dataExcel[$i]['support'] = $objPHPExcel->getActiveSheet()->getCell('J' . $i)->getCalculatedValue();
$dataExcel[$i]['location'] = $objPHPExcel->getActiveSheet()->getCell('H' . $i)->getCalculatedValue();
$dataExcel[$i]['comments'] = $objPHPExcel->getActiveSheet()->getCell('AF' . $i)->getCalculatedValue();
$dataExcel[$i]['name_locator'] = $objPHPExcel->getActiveSheet()->getCell('E' . $i)->getCalculatedValue();
$dataExcel[$i]['ci_locator'] = $objPHPExcel->getActiveSheet()->getCell('F' . $i)->getCalculatedValue();
$dataExcel[$i]['tel_locator'] = $objPHPExcel->getActiveSheet()->getCell('G' . $i)->getCalculatedValue();
$dataExcel[$i]['address_locator'] = $objPHPExcel->getActiveSheet()->getCell('I' . $i)->getCalculatedValue();
}
foreach ($dataExcel as $dataRow => $value) {
$dateFormatPeriodTo = $value['period_to']->format('d/m/Y');
$dateFormatPeriodFrom = $value['period_from']->format('d/m/Y');
// var_dump($dateFormatPeriodTo);
//$dateFormatPeriodTo = date('d/m/Y', PHPExcel_Shared_Date::ExcelToPHP($value['period_to']));
$sql = "REPLACE data_locator (id_locator, payment, period_to, period_from, support, location, comments, name_locator, ci_locator, tel_locator, address_locator) VALUES ({$value['id_locator']}, {$value['payment']}, '$dateFormatPeriodTo', '$dateFormatPeriodFrom', '{$value['support']}', '{$value['location']}', '{$value['comments']}', '{$value['name_locator']}', {$value['ci_locator']}, '{$value['tel_locator']}', '{$value['address_locator']}')";
// echo $sql;
$result = $mysqli->query($sql);
}
for ($j = 42; $j <= 55; $j++) {
if ((float) $objPHPExcel->getActiveSheet()->getCell('A' . $j)->getCalculatedValue() == 0) {
break;
}
$dataExcelTwo[$j]['id_locator'] = $objPHPExcel->getActiveSheet()->getCell('A' . $j)->getCalculatedValue();
$dataExcelTwo[$j]['payment'] = $objPHPExcel->getActiveSheet()->getCell('X' . $j)->getCalculatedValue();
$dataExcelTwo[$j]['period_to'] = PHPExcel_Shared_Date::ExcelToPHPObject($objPHPExcel->getActiveSheet()->getCell('AC' . $j)->getCalculatedValue());
$dataExcelTwo[$j]['period_from'] = PHPExcel_Shared_Date::ExcelToPHPObject($objPHPExcel->getActiveSheet()->getCell('AD' . $j)->getCalculatedValue());
$dataExcelTwo[$j]['support'] = $objPHPExcel->getActiveSheet()->getCell('J' . $j)->getCalculatedValue();
$dataExcelTwo[$j]['location'] = $objPHPExcel->getActiveSheet()->getCell('H' . $j)->getCalculatedValue();
$dataExcelTwo[$j]['comments'] = $objPHPExcel->getActiveSheet()->getCell('AF' . $j)->getCalculatedValue();
$dataExcelTwo[$j]['name_locator'] = $objPHPExcel->getActiveSheet()->getCell('E' . $j)->getCalculatedValue();
$dataExcelTwo[$j]['ci_locator'] = $objPHPExcel->getActiveSheet()->getCell('F' . $j)->getCalculatedValue();
$dataExcelTwo[$j]['tel_locator'] = $objPHPExcel->getActiveSheet()->getCell('G' . $j)->getCalculatedValue();
$dataExcelTwo[$j]['address_locator'] = $objPHPExcel->getActiveSheet()->getCell('I' . $j)->getCalculatedValue();
}
foreach ($dataExcelTwo as $dataRow => $value) {
$dateFormatPeriodTo = $value['period_to']->format('d/m/Y');
$dateFormatPeriodFrom = $value['period_from']->format('d/m/Y');
// var_dump($dateFormatPeriodTo);
//$dateFormatPeriodTo = date('d/m/Y', PHPExcel_Shared_Date::ExcelToPHP($value['period_to']));
$sql = "REPLACE data_locator (id_locator, payment, period_to, period_from, support, location, comments, name_locator, ci_locator, tel_locator, address_locator) VALUES ({$value['id_locator']}, {$value['payment']}, '$dateFormatPeriodTo', '$dateFormatPeriodFrom', '{$value['support']}', '{$value['location']}', '{$value['comments']}', '{$value['name_locator']}', {$value['ci_locator']}, '{$value['tel_locator']}', '{$value['address_locator']}')";
// echo $sql;
$result = $mysqli->query($sql);
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<link rel="stylesheet" href="_css/datepicker.css">
<script src="_js/alertify.min.js"></script>
<link rel="stylesheet" href="//cdn.jsdelivr.net/bootstrap/3.2.0/css/bootstrap.min.css"/>
<link rel="stylesheet" href="_css/style.css">
<link rel="stylesheet" href="_css/alertify.default.css">
<link rel="stylesheet" href="_css/alertify.core.css">
<link rel="stylesheet" href="_css/alertify.bootstrap3.css">
<link href='https://fonts.googleapis.com/css?family=Roboto:400,700,300,900' rel='stylesheet' type='text/css'>
<link rel="icon" href="_images/favicon.png" type="image/gif" sizes="16x16">
<link rel="stylesheet" href="alertify.default.css">
<link rel="stylesheet" href="alertify.core.css">
<link rel="stylesheet" href="alertify.bootstrap3.css">
</head>
</html>
在删除读取文件“ read_excel.php”中的几乎所有代码块之后,我可以看到延迟了索引执行的行(调用read_excel.php)
$objPHPExcel = PHPExcel_IOFactory::load($fileName);
删除该行后,索引将以毫秒为单位加载。
所以,我的问题是,调用我要读取的excel文件的方式是否错误?
我的excel大小只有110 KB,我不认为这是一个问题,对吗?
我正在本地服务器(wamp服务器2.5)和Windows 7 64位上运行我的软件。可能有问题吗?
我看到了一些主题,其中建议对处理大量信息的文件进行优化,但就我而言,这是一个很小的文件,我只使用excel块(用于条件),这似乎不是问题所在,因为我删除该行时索引的加载速度会有所不同:
$objPHPExcel = PHPExcel_IOFactory::load($fileName);
可以是我正在使用的excel库的版本吗?
谢谢!