PHP export CSV format data and convert numbers into text ideas and code sharing _php tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.