PHP export CSV format data and convert numbers to text ideas and code sharing _php instances

Source: Internet
Author: User
Tags set time

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"));

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.