将数据库中的块保存到xls中

时间:2021-07-15 20:22:08

I'm using this code by Krasimir to output data from PHP to .xls

我正在使用Krasimir的这段代码将数据从PHP输出到.xls

function xlsBOF() {
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
}
function xlsEOF() {
    echo pack("ss", 0x0A, 0x00);
}
function xlsWriteNumber($Row, $Col, $Value) {
    echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
    echo pack("d", $Value);
}
function xlsWriteLabel($Row, $Col, $Value) {
    $L = strlen($Value);
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
    echo $Value;
} 
// prepare headers information
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=\"export_".date("Y-m-d").".xls\"");
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("Expires: 0");
// start exporting
xlsBOF();
// first row 
xlsWriteLabel(0, 0, "id");
xlsWriteLabel(0, 1, "name");
xlsWriteLabel(0, 2, "email");
// second row 
xlsWriteNumber(1, 0, 230);
xlsWriteLabel(1, 1, "John");
xlsWriteLabel(1, 2, "john@yahoo.com");
// third row 
xlsWriteNumber(2, 0, 350);
xlsWriteLabel(2, 1, "Mark");
xlsWriteLabel(2, 2, "mark@yahoo.com");
// end exporting
xlsEOF();

It works fine for a small amount of data, but when I export from the database, for example, a large amount of data (100.000 records) I get server timeout error.

它适用于少量数据,但是当我从数据库导出时,例如,大量数据(100.000条记录),我收到服务器超时错误。

How can I change this code so that I can write small chunks of data to the file progressivly?

如何更改此代码以便我可以逐步向文件中写入小块数据?

Currently I'm using a method that works only for tab separated files or CSVs

目前我使用的方法仅适用于制表符分隔文件或CSV

I'm writing chunks of 1000 rows in the .xls file. After exporting a chunk I use window.location.href=the_script.php?offset=new_offset to call the script for the next offset, and open the file again and append the next 1000 rows.

我在.xls文件中写了1000行的块。导出块后,我使用window.location.href = the_script.php?offset = new_offset来调用下一个偏移的脚本,然后再次打开文件并追加下一行1000行。

The problem with this method is that Excel outputs the warning The file format and the extension don't match. The file could be corrupted or unsafe. Unless you trust the source, don't open it. Do you want to open it anyway?

此方法的问题是Excel输出警告文件格式和扩展名不匹配。该文件可能已损坏或不安全。除非您信任来源,否则请勿打开它。你想打开它吗?

How can I achieve something similar for the script above?

如何为上面的脚本实现类似的功能?

1 个解决方案

#1


1  

Increase the time limit for PHP scripts.

增加PHP脚本的时间限制。

#1


1  

Increase the time limit for PHP scripts.

增加PHP脚本的时间限制。