Simple example of exporting CSV files in php
/**
- * Export a csv file. example of the header function
- * Edit: bbs.it-home.org
- */
- Header ("Cache-Control: public ");
- Header ("Pragma: public ");
- Header ("Content-type: application/vnd. ms-excel ");
- Header ("Content-Disposition: attachment?filename=txxx.csv ");
- Header ('content-Type: APPLICATION/OCTET-STREAM ');
Ob_start ();
- $ Header_str = iconv ("UTF-8", 'gbk', "information id, title, name, phone number, QQ, Email, content, time \ n ");
- $ File_str = "";
-
- $ Mysqli = new mysqli ('localhost', 'root', '', 'test ');
If (mysqli_connect_errno ()){
- Printf ("Connect failed: % s \ n", mysqli_connect_error ());
- Exit ();
- }
- $ SQL = 'select * from messages ';
- $ Mysqli-> query ("set names utf8 ;");
- $ Result = $ mysqli-> query ($ SQL );
If ($ result ){
- While ($ row = mysqli_fetch_assoc ($ result )){
- $ File_str. = $ row ['id']. ','. $ row ['title']. ','. $ row ['name']. ','. "'{$ row ['telphone']}'". ','. $ row ['QQ']. ','. $ row ['email ']. ','. str_ireplace (', $ row ['content']). ','. $ row ['retime']. "\ n ";
- }
- } Else {
- Echo "nonono !!! ";
- }
- $ File_str = iconv ("UTF-8", 'gbk', $ file_str );
- Ob_end_clean ();
- Echo $ header_str;
- Echo $ file_str;
- ?>
|