PHP Export CSV format data implementation:
Define a string to store the contents, for example
$exportdata = ' rule 111, Rule 222, Trial 222, Regulation 222, service 2222, rule 1, Rule 2, Rule 3, match character, set time, validity period '. \ n ";
The array that needs to save the CSV is then made into a foreach loop, for example
Copy the 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 contents of the CSV format are separated by ', ' and can be divided in real time. Each line is followed by a ' \ n ' to branch.
Then execute the output on the back. For example
Copy the 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"));
However, when exporting the number, CSV will remove the previous 0, for example, I want to display 00001, if the output will display 1. This solution is the output of a ' \ ' \ t ', this is a tab, will be displayed as a space. You can convert the values into text. However, the import will appear ' ". This stuff, just use the trim function from PHP. The complete code is as follows:
Copy the Code code as follows:
Var_dump ($sql);
$lists = $this->dbo->query ($sql);
$exportdata = ' rule 111, Rule 222, Trial 222, gauge 222, Service 2222, rule 1, Rule 2, Rule 3, match character, set time, validity period '. \ n ";
$date = Date ("Ymdhis");
if (!empty ($lists)) {
foreach ($lists as $key = = $value) {
$time = date ("Y-m-d_h:i:s", $value [' add_time ']); br> $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"));
http://www.bkjia.com/PHPjc/779565.html www.bkjia.com true http://www.bkjia.com/PHPjc/779565.html techarticle PHP Export CSV format data implementation: First define a string storage content, such as $exportdata = ' rule 111, Rule 222, Audit 222, 222, take 2222, rule 1, Rule 2, Rule 3, match character, set ...