Home > Backend Development > PHP Tutorial > Ideas and code sharing for exporting csv format data and converting numbers into text_PHP tutorial

Ideas and code sharing for exporting csv format data and converting numbers into text_PHP tutorial

WBOY
Release: 2016-07-13 10:29:01
Original
933 people have browsed it

PHP export of csv format data implementation:
First define a string to store the content, for example
$exportdata = 'Rule 111, Rule 222, Review 222, Rule 222, Service 2222, Rule 1, Rule 2, Rule 3, match characters, set time, validity period'."n";

Then perform a foreach loop on the array that needs to be saved as csv, such as

Copy code The code is as follows:

if (!empty($lists)){
foreach($lists as $key => $value){
                      $time = date("Y-m-d_H:i:s", $value['add_time']); ['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";
                                                                                                                                                                                          . It can be divided into grids. Just an 'n' after each line can branch the line.

Then execute the output later. For example


Copy code

The code is as follows:

                                                > header("Content-type:application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=$filename");          header("Expires: 0");
             header("Pragma: public"); );
header("Cache-Control: public");

echo(mb_convert_encoding($exportdata,"gb2312","UTF-8"));

But when exporting numbers, csv will remove the leading 0. For example, if I want to display 00001, if it is output, it will display 1. The solution is to use a '"t' when outputting, this is Tab characters will be displayed as spaces. The value can be converted into text. However, ' " ' will appear when importing. Just use the trim function that comes with PHP. The complete code is as follows:




Copy code

The code is as follows:
                                                                                                                                                                                                                                                                                   
$exportdata = 'Rule 111, Rule 222, Review 222, Rule 222, Service 2222, Rule 1, Rule 2, Rule 3, Matching characters, 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']);
                                               $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"; attachment; filename=$filename");

         header("Expires: 0");
             header("Pragma: public"); );
header("Cache-Control: public");

echo(mb_convert_encoding($exportdata,"gb2312","UTF-8"));






http://www.bkjia.com/PHPjc/779565.html

www.bkjia.comtrue

http: //www.bkjia.com/PHPjc/779565.htmlTechArticlePHP implementation of exporting csv format data: First define a string to store the content, for example $exportdata = 'Rule 111, Rule 222, review 222, rule 222, server 2222, rule 1, rule 2, rule 3, matching characters, when setting...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template