Php export csv format data implementation:
First define a string storage content, such
$ Exportdata = 'Rule 111, Rule 222, audit 222, Rule 222, server 2222, rule 1, rule 2, rule 3, matching character, set time, valid time '. "\ n ";
Then perform a foreach loop on the array to save the csv, for example
Copy codeThe Code is as follows:
If (! Empty ($ lists )){
Foreach ($ lists as $ key => $ value ){
$ Time = date ("Y-m-d_H: I: s", $ value ['add _ time']);
$ Exportdata. = "\" \ t ". $ value ['rule_id']. "\", \ "\ t ". $ value ['rule_name']. "\", \ "\ t ". $ value ['matching _ level']. "\", \ "\ t ". "{$ value ['Rule _ action']}". "\", \ "\ t ". $ value ['service _ type']. "\", \ "\ t ". $ value ['keyword1 ']. "\", \ "\ t ". $ value ['keyword2 ']. "\", \ "\ t ". $ value ['keyword3 ']. "\", \ "\ t ". $ value ['matching _ word']. "\", \ "\ t ". $ value ['set _ time']. "\", \ "\ t ". $ value ['validation _ time']. "\" \ n ";
}
}
The contents in csv format are separated by commas (,). In reality, the content can be split. Each line is followed by a '\ n' branch.
Then, execute the output at the end. For example
Copy codeThe Code is as follows:
$ Filename = "plcnetinfo_00000000date0000.csv ";
Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: attachment; filename = $ filename ");
Header ("Expires: 0 ");
Header ("Pragma: public ");
Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
Header ("Cache-Control: public ");
Echo (mb_convert_encoding ($ exportdata, "gb2312", "UTF-8 "));
However, when exporting a number, the csv will remove the preceding 0. For example, if I want to display 00001, the output will show 1. this solution is to generate a '\ "\ t' at the time of output, which is a tab and will be displayed as a space. The value can be converted into text. However, '"' will appear during the import. Just use the trim function that comes with php. The complete code is as follows:
Copy codeThe Code is as follows:
// Var_dump ($ SQL );
$ Lists = $ this-> dbo-> query ($ SQL );
$ Exportdata = 'Rule 111, Rule 222, audit 222, Rule 222, server 2222, rule 1, rule 2, rule 3, matching character, set time, valid time '. "\ n ";
$ Date = date ("YmdHis ");
If (! Empty ($ lists )){
Foreach ($ lists as $ key => $ value ){
$ Time = date ("Y-m-d_H: I: s", $ value ['add _ time']);
$ Exportdata. = "\" \ t ". $ value ['rule_id']. "\", \ "\ t ". $ value ['rule_name']. "\", \ "\ t ". $ value ['matching _ level']. "\", \ "\ t ". "{$ value ['Rule _ action']}". "\", \ "\ t ". $ value ['service _ type']. "\", \ "\ t ". $ value ['keyword1 ']. "\", \ "\ t ". $ value ['keyword2 ']. "\", \ "\ t ". $ value ['keyword3 ']. "\", \ "\ t ". $ value ['matching _ word']. "\", \ "\ t ". $ value ['set _ time']. "\", \ "\ t ". $ value ['validation _ time']. "\" \ n ";
}
}
$ Filename = "plcnetinfo_00000000date0000.csv ";
Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: attachment; filename = $ filename ");
Header ("Expires: 0 ");
Header ("Pragma: public ");
Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
Header ("Cache-Control: public ");
Echo (mb_convert_encoding ($ exportdata, "gb2312", "UTF-8 "));