PHPExcel脚本需要很长时间才能使用大型数据库

时间:2014-11-14 10:31:02

标签: php phpexcel

这是PHP代码,我不太了解PHPExcel使其运行得更快,理想情况下我不想限制为10000行(在发送excel文件之前至少需要5分钟)

有什么想法吗?

该脚本基本上从sqlite数据库中选择所有数据,然后循环第一行的键以添加为列的标题。
然后它循环所有行并设置每个单元格值 在此之后,它添加公式列 然后将excel数据发送给用户。

脚本运行在:http://example-site.org/getStatsExcel.php - 因此每次用户访问该页面时,它都会运行此脚本 - 我想我应该每天存储数据库,如果它已经存储了那天,那么只需返回文件,否则再次生成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;

1 个答案:

答案 0 :(得分:0)

您使用的数据越多,所需的时间就越长。这就是为什么我们建议生成大型电子表格应该用于后端流程,这样它就不会让用户在构建电子表格时等待。

如果您可以“离线”构建这些大型数据电子表格,那么就这样做;如果你可以缓存它们,那么就这样做。