Php export csv format data and convert numbers into text ideas and code sharing

Source: Internet
Author: User

Php export csv format data implementation:
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

Copy codeThe 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

Copy codeThe 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:

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

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.