简体   繁体   中英

PHP - How can I dynamically export data to excel file (.xls)?

I'm using php. I'd like to know how can I dynamically export data to excel file (.xls). in the query below in entered the fields in the clause Select which i want to see in the excel file.

$query = "SELECT f.attendee_id as id1, f.answer as Prenom, l.answer as Nom, c1.answer as choix
         FROM wp_events_answer f
         JOIN wp_events_answer l ON f.attendee_id = l.attendee_id
         JOIN wp_events_answer c1 ON f.attendee_id = c1.attendee_id
         WHERE (f.question_id = 1) AND (l.question_id = 2) AND (c1.question_id = 28);";

And i also entered the labels of this fields:

$tbl= " <table border='1'>
<tr height='50px'><td WIDTH='50px' align='center'>ID</td><td WIDTH='50px'   align='center'>Prenom</td><td WIDTH='50px'align='center'>Nom</td><td WIDTH='50px' align='center'>Choix 1</td><td WIDTH='50px' align='center'>Choix 2</td></tr>";.;

But i'd like that all this became dynamic because the user can create and modify the forms. So if the user add or modify a field the export will only works if this field is also modified in the code (Select clause).

Can you explain me how can I do this?

Thanks

MYSQL code:

SELECT f.attendee_id as id1, f.answer as Prenom, l.answer as Nom, c1.answer as choix
    INTO OUTFILE '/path/to/file.csv'
    FIELDS
            TERMINATED BY ';'
            OPTIONALLY ENCLOSED BY '"'
    FROM wp_events_answer f
     JOIN wp_events_answer l ON f.attendee_id = l.attendee_id
     JOIN wp_events_answer c1 ON f.attendee_id = c1.attendee_id
     WHERE (f.question_id = 1) AND (l.question_id = 2) AND (c1.question_id = 28);

只需将您的数据以CSV(逗号分隔)格式写入响应缓冲区或磁盘文件(以后您可能会允许用户下载或强制下载)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM