对于大型数据库,PHPExcel脚本需要很长时间
这是PHP代码,我对PHPExcel了解不多,无法让它运行得更快,理想情况下我不想限制为10000行(在它发送excel文件之前至少需要5分钟) 有什么想法吗 脚本基本上从sqlite数据库中选择所有数据,然后循环第一行的键以添加列标题。对于大型数据库,PHPExcel脚本需要很长时间,php,phpexcel,Php,Phpexcel,这是PHP代码,我对PHPExcel了解不多,无法让它运行得更快,理想情况下我不想限制为10000行(在它发送excel文件之前至少需要5分钟) 有什么想法吗 脚本基本上从sqlite数据库中选择所有数据,然后循环第一行的键以添加列标题。 然后循环所有行并设置每个单元格值。 然后添加公式列。 然后将excel数据发送给用户 脚本在:-处运行,因此每次用户进入该页面时,它都会运行此脚本-我认为我应该每天存储数据库,如果已经存储了该数据库,则只需返回文件,否则将再次生成excel <?php
然后循环所有行并设置每个单元格值。
然后添加公式列。
然后将excel数据发送给用户 脚本在:-处运行,因此每次用户进入该页面时,它都会运行此脚本-我认为我应该每天存储数据库,如果已经存储了该数据库,则只需返回文件,否则将再次生成excel
<?php
date_default_timezone_set('Europe/Zurich');
require_once 'phpexcel/Classes/PHPExcel.php';
ini_set('max_execution_time', 900);
$dbname = 'admin';
$fullPath = sprintf('/var/www/fullpathtosqlite/%s.sqlite', $dbname);
$dbh = new PDO('sqlite:' . $fullPath);
$phpExcel = new PHPExcel();
$phpExcel->getProperties()->setTitle('Export : Statistics');
$phpExcel->getProperties()->setCreator('PHPExcel Stats Script');
$sheet = $phpExcel->getActiveSheet();
$sheet->setTitle('stats');
$phpExcel->setActiveSheetIndex(0);
$sql = 'SELECT * FROM (SELECT * FROM statistics ORDER BY timestamp DESC LIMIT 10000) ORDER BY timestamp ASC';
if(!$stmt = $dbh->prepare($sql, array(PDO::ATTR_CURSOR, PDO::CURSOR_SCROLL))) {
die(var_export($dbh->errorinfo(), TRUE));
}
$stmt->execute();
// Fetch the first row
$row = $stmt->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT);
$results = array();
// Iterate over the results and print each one in a line
while ($row != false) {
$results[] = $row;
// Fetch the next line
$row = $stmt->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT);
}
$row = 1;
$col = 0;
foreach ($results[0] as $key => $value) {
$sheet->setCellValueByColumnAndRow($col, $row, $key);
$col++;
}
// date
$sheet->setCellValueByColumnAndRow($col, $row, 'date');
$col++;
// time
$sheet->setCellValueByColumnAndRow($col, $row, 'time');
$col++;
// full_date
$sheet->setCellValueByColumnAndRow($col, $row, 'full_date');
$row = 2;
foreach ($results as $result) {
$col = 0;
foreach ($result as $key => $value) {
$sheet->setCellValueByColumnAndRow($col, $row, $value);
$col++;
}
// date
$sheet->setCellValueByColumnAndRow($col, $row, '=DATE(LEFT(A' . $row . ',4),MID(A' . $row . ',5,2),MID(A' . $row . ',7,2))');
$col++;
// time
$sheet->setCellValueByColumnAndRow($col, $row, '=TIME(MID(A' . $row . ',9,2),MID(A' . $row . ',11,2),MID(A' . $row . ',13,2))');
$col++;
// full_date
$sheet->setCellValueByColumnAndRow($col, $row, '=F' . $row . '+G' . $row);
$row++;
}
$sheet->getStyle('F2:F' . $row)
->getNumberFormat()
->setFormatCode('dd/mm/yyyy');
$sheet->getStyle('G2:G' . $row)
->getNumberFormat()
->setFormatCode('h:mm AM/PM');
$sheet->getStyle('H2:H' . $row)
->getNumberFormat()
->setFormatCode('dd/mm/yyyy hh:mm');
foreach(range('A','H') as $columnID) {
$sheet->getColumnDimension($columnID)->setAutoSize(true);
}
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"statistics.xls\"");
header("Cache-Control: max-age=0");
$objWriter = PHPExcel_IOFactory::createWriter($phpExcel, "Excel5");
$objWriter->save("php://output");
exit;
处理的数据越多,所需时间越长。这就是为什么我们建议将生成大型电子表格的工作分配给后端流程,这样用户在生成电子表格时就不会等待
如果您可以“离线”构建这些大型数据电子表格,那么就这样做;如果可以缓存它们,那么就这样做