dounaidu0204 2014-01-08 09:14
浏览 39
已采纳

在PHP中为用户创建Excel文件

I have data in a MySQL database. I am sending the user a URL to get their data out as a excel file.

How can I, when they click the link, have a pop-up to download a excel with the record from MySQL?

I have all the information to get the record already. I just don't see how to have PHP create the excel file and let them download a file

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("
");


$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("/
|
|
|/", ' ', $output);
print(trim($output))."\t
";

$j++;
} // while close

When i hit link it creates excel file but data not coming properly

it writes only points in excel file not email & date

please help me on this.

  • 写回答

1条回答 默认 最新

  • dongtao4890 2014-01-08 09:49
    关注

    For example, you can use PEAR Spreadsheet lib http://pear.php.net/package/Spreadsheet_Excel_Writer

    This lib creates excel file and than you should send content to user with setting right headers

    via mail:

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

    via browser:

            $length = filesize($filePath);
            header("Content-type: application/octet-stream");
            header("Content-Disposition: attachment; filename=\"$fileTitle.xls\"");
            header("Content-Length: $length");
            readfile($filePath);
            exit;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?