将PHP数组导出到Excel文档。我在文档的最后部分收到错误Resource id #7
。 PHP数组是
(
[0] => Array
(
[0] => Reg
[1] => Fname
[2] => Lname
)
[1] => Array
(
[0] => 63
[1] => Praveen
[2] => s
)
[2] => Array
(
[0] => 57
[1] => Prasad
[2] => K
)
[3] => Array
(
[0] => 456
[1] => Prabharan
[2] => G
)
[4] => Array
(
[0] => 080105306062
[1] => prasad
[2] => kppp
)
)
并且excel文档中的输出是
Reg Fname Lname
63 Praveen s
57 Prasad K
456 Prabharan G
80105306062 prasad kppp
Resource id #7
php代码是:
$fp = fopen('php://output', 'w');
foreach ($list1 as $fields)
{
fputcsv($fp, $fields);
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=export.csv');
header('Content-Transfer-Encoding: binary');
header('Content-length:'.count($list1));
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
fclose($fp);
die($fp);
答案 0 :(得分:0)
尝试使用 ob_start()和 ob_get_clean()
ob_start();
$fp = fopen('php://output', 'w');
//Your code to create CSV content
foreach ($list1 as $fields)
{
fputcsv($fp, $fields);
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=export.csv');
header('Content-Transfer-Encoding: binary');
header('Content-length:'.count($list1));
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
fclose($fp);
//After file is created, make it output
die(ob_get_clean());