gpt4 book ai didi

php - 在 PHP 中为用户创建 Excel 文件

转载 作者:行者123 更新时间:2023-11-30 00:50:51 25 4
gpt4 key购买 nike

我的 MySQL 数据库中有数据。我向用户发送一个 URL,以将他们的数据作为 Excel 文件导出。

当他们点击链接时,我怎样才能弹出一个窗口,从 MySQL 下载包含记录的 Excel?

我已经掌握了获取记录的所有信息。我只是不知道如何让 PHP 创建 excel 文件并让他们下载文件

include("con.php");

// Qry to fetch all records against date
$dateonedayback=strtotime('-1 day',strtotime(date("Y/m/d")));
$one_day=date("Y-m-d",$dateonedayback);


$qry_user_points = "SELECT u.points,u.db_add_date,u.user_email_id FROM tbl_user AS u WHERE u.points != '0' AND date(u.db_add_date)='".$one_day."' ";

$query_result= $con->db_query($qry_user_points);

header('Content-Type: application/vnd.ms-excel'); //define header info for browser
header('Content-Disposition: attachment; filename=Users-Points-Report-'.date('Ymd').'.xls');

$excel_header = array("Email","Points","Date");

for ($i = 0; $i < count($excel_header); $i++)
{
echo $excel_header[$i]."\t";
}
print("\n");


$j=0;
while($rowValue = $con->db_fetch_array($query_result))
{
$points = $rowValue['points'];
$emailID = $rowValue['user_email_id'];
$addedDate = $rowValue['db_add_date'];

// create an array to insert in excel with url encoding
$final_array = array(urlencode($points),urlencode($emailID),urlencode($addedDate));

$result_count =$con->db_num_rows($query_result);

$output = '';
for($k=0; $k < $con->db_num_rows($query_result); $k++)
{
if(!isset($final_array[$k]))
$output .= "NULL\t";
else
$output .= "$final_array[$k]\t";
}
$output = preg_replace("/\r\n|\n\r|\n|\r/", ' ', $output);
print(trim($output))."\t\n";

$j++;
} // while close

当我点击链接时,它会创建 Excel 文件,但数据未正确传送

它只在 Excel 文件中写入点,而不是电子邮件和日期

请帮我解决这个问题。

最佳答案

例如,您可以使用 PEAR Spreadsheet lib http://pear.php.net/package/Spreadsheet_Excel_Writer

此库创建 Excel 文件,然后您应该通过设置正确的标题将内容发送给用户

通过邮件:

        $sid = md5(uniqid(time()));
$header = "From: Automatic report system<admin@example.com>\nReply-to: admin@example.com\nMIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"$sid\"\n\n";
$header .= "This is multi-part message in MIME format.\n--$sid\n";
$header .= "Content-type: text/plain; charset=utf-8\n\n";
$length = filesize($filePath);
$header .= "--$sid\nContent-type: application/octet-stream; name=\"$fileTitle.xls\"\n";
$header .= "Content-Transfer-Encoding: base64\n";
$header .= "Content-Disposition: attachment; filename=\"$fileTitle.xls\"\nContent-Length: $length\n\n";
$header .= chunk_split(base64_encode(file_get_contents($filePath))) . "\n\n\n\n";
mail('recepient@example.com', 'New report', null, $header);

通过浏览器:

        $length = filesize($filePath);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$fileTitle.xls\"");
header("Content-Length: $length");
readfile($filePath);
exit;

关于php - 在 PHP 中为用户创建 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20991288/

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