gpt4 book ai didi

php - 如何在 yii 1 框架中将表格数据下载为 excel 表格

转载 作者:行者123 更新时间:2023-12-04 20:55:33 24 4
gpt4 key购买 nike

我正在使用 Yii 1 框架。我尝试将表格详细信息下载为 .csv 文件并使用 ID 从表格中检索数据。

单击按钮时,它会检索数据并显示在检查元素的网络部分。没有错误,唯一的问题是不下载文件。

这是 Controller 方法的代码。

public function actionDownload()
{


$id = $_REQUEST['id'];


$vote_record = Vote::model()->findByAttributes(array('id' => $id));


$name = $vote_record['name'];

header('Content-Description: File Transfer');
header("Content-type: application/csv");
header('Content-Disposition: attachment; filename="'. str_replace(" ", "_", strtolower($name)) . '_results.csv"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();



$titile = "Option, Vote Count\n";
echo $titile;
if (count($vote_record) > 0) {

echo $vote_record['name'] . "," . $vote_record['total'] . "\n";

echo "\n\n";
echo $vote_record['opname1'] . "," . $vote_record['voteCount1'] . "\n";
echo $vote_record['opname2'] . "," . $vote_record['voteCount2'] . "\n";
echo $vote_record['opname3'] . "," . $vote_record['voteCount3'] . "\n";
echo $vote_record['opname4'] . "," . $vote_record['voteCount4'] . "\n";

}
}

所以请帮忙解决这个问题。

最佳答案

您可以针对您的问题尝试此解决方案:

<?php 
public function actionDownload() {
$id = $_REQUEST['id'];
$vote_record = Vote::model()->findByAttributes(array('id' => $id));
$name = $vote_record['name'];

header("Content-type: application/csv");
header('Content-Disposition: attachment; filename="'. str_replace(" ", "_", strtolower($name)) . '_results.csv"');
header("Pragma: no-cache");
header("Expires: 0");

$handle = fopen('php://output', 'w');
if (count($vote_record) > 0) {
fputcsv($handle, array('Option', 'Vote Count'));
fputcsv($handle, array($vote_record['name'], $vote_record['total']));
fputcsv($handle, array($vote_record['opname1'], $vote_record['voteCount1']));
fputcsv($handle, array($vote_record['opname2'], $vote_record['voteCount2']));
fputcsv($handle, array($vote_record['opname3'], $vote_record['voteCount3']));
fputcsv($handle, array($vote_record['opname4'], $vote_record['voteCount4']));
fclose($handle);
exit;
}
ob_clean();
flush();
}
?>

我希望它会有所帮助。

关于php - 如何在 yii 1 框架中将表格数据下载为 excel 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48396901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com