PHP Export CSV format data implementation:
First define a string to store the content, for example
$exportdata = ' rule 111, Rule 222, 222, gauge 222, take 2222, rule 1, Rule 2, Rule 3, match characters, set time, validity '. \ n ";
Then a foreach loop is performed on the array that needs to save the CSV, for example
Copy Code code 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 content in CSV format is separated by ', ' and can be divided in real time. After each line, one ' \ n ' will be able to branch.
Then it's OK to run the output in the back. For example
Copy Code code as follows:
$filename = "plcnetinfo_{$date}.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"));
But when you export a number, CSV will remove the previous 0, for example, I want to show 00001, if the output will show 1. The solution is to have a ' \ t ' on the output, which is a tab, and is displayed as a space. You can convert the value into text. However, in the import of the time will appear ' ". This kind of thing, the use of PHP with the trim function is good. The complete code is as follows:
Copy Code code as follows:
Var_dump ($sql);
$lists = $this->dbo->query ($sql);
$exportdata = ' rule 111, Rule 222, 222, gauge 222, take 2222, rule 1, Rule 2, Rule 3, match characters, set time, validity '. \ 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_{$date}.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"));