In a recent project, you need to export some statistical results and other items into CSV files for reporting. based on the previous experience, you can now send your ideas and code. if you have a better method, I hope that you can refer to php to export csv data:
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
The 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
The 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:
The 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 "));