如何在php中使用大数据和多个函数调用加速excel转换


How to speed up excel conversion in php with large data and multiple function calls

我的示例php脚本如下所示。我想将数据库数据保存为Excel格式。这张表有35列。我需要在这个表中显示16到18000个数据。有多个函数可以获取不同列的数据。

这个脚本可以工作,但需要8-10分钟才能获得excel文件。该输出excel文件的大小为18mb。

我认为每行都有多个函数调用,而文件大小太大是导致速度缓慢的原因。

    include('../include/functions.php'); //includes functions
    $sql_query=$sql; //here $sql is some sql query
    $result_details=mysql_query($sql_query);
     $data=
    '<table border="1">
        <tr style="font-weight:bold">
            <td>Title 1</td>
            <td>Title 2</td>
            .
            .
            .
            <td>Title n</td>
        </tr>';
    while(($row_details=mysql_fetch_array($result_details))){           
     $data.=
        '<tr>
            <td>'.function_1($row_details['col-1']).'</td> //some function function_1 to get first column
            <td>'.function_2($row_details['col-2']).'</td> //some function function_2 to get second column
            .
            .
            .
            <td>'.function_n($row_details['col-n']).'</td>  //some function function_n to get nth column                                
        </tr>';
            }
    $data.='</table>';  
    $file_type = "vnd.ms-excel";
    $file_name= "details.xls";
    header("Content-Type: application/$file_type");
    header("Content-Disposition: attachment; filename=$file_name");
    header("Pragma: no-cache");
    header("Expires: 0");
    print $data;

如果能帮我加快excel文件的转换速度,我将不胜感激。非常感谢。

生成一个真正的csv文件,而不是混淆csv/html.xls

include('../include/functions.php'); //includes functions
$sql_query=$sql; //here $sql is some sql query
$result_details=mysql_query($sql_query);
$file_type = "text/csv";
$file_name= "details.csv";
header("Content-Type: $file_type");
header("Content-Disposition: attachment; filename=$file_name");
... other headers here
$fh = fopen('php://output', 'w');
while($row_details = mysql_fetch_array($result_details)){
    fputcsv($fh, $row_details);
}
fclose($fh);

加快这项工作的简单方法:

  • 将此=>" ... INTO OUTFILE '/tmp/orders.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY ''n'; 添加到您的查询中

  • 显示这个来自磁盘的临时文件和你的头

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");

因为您将大量数据附加到$data变量中,从而将其带入内存。你需要稍微改变一下方法。

循环中首先将每一行直接写入文件,而不是追加到$data。这将减少ram的使用。在写入所有行之后,直接使用readfile函数读取带有excel标题的文件。

这应该会加快excel的构建/下载过程。

$file_path= "/tmp/details.xls";
$file_name= "details.xls";
$fp = fopen($file_path, "a");
$data =
    '<table border="1">
        <tr style="font-weight:bold">
            <td>Title 1</td>
            <td>Title 2</td>
            .
            .
            .
            <td>Title n</td>
        </tr>';
fwrite($fp, $data);
while(($row_details=mysql_fetch_array($result_details))) {
    $data =
        '<tr>
            <td>'.function_1($row_details['col-1']).'</td> //some function function_1 to get first column
            <td>'.function_2($row_details['col-2']).'</td> //some function function_2 to get second column
            .
            .
            .
            <td>'.function_n($row_details['col-n']).'</td>  //some function function_n to get nth column                                
        </tr>';
            } 
     fwrite($fp, $data);        
}
$data = '</table>';
fwrite($fp, $data); 
fclose($fp); 
$file_type = "vnd.ms-excel";
header("Content-Type: application/$file_type");
header("Content-Disposition: attachment; filename=$file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile($file_path);