thinkphp Exporting CSV format documents

Source: Internet
Author: User
Here the export CSV format file and export Excel is similar, you can refer to the code, to achieve the export function.

/** * CSV export */public function exportvoter () {Header ("cache-control:public"); Header ("Pragma:public"); Header ("content- Type:application/vnd.ms-excel "); header (" Content-disposition:attachment;filename= voter information. csv "); Header (' Content-type:application/octet-stream '); Ob_start (); $header _str =  iconv ("Utf-8", ' GBK ', "name, gender, telephone, password \ n"); $ Voter = new \admin\model\votersmodel ();//$data = I (' Get. '); Print_r ($data); exit (); if (I (' get.name ') = "") {$name = I (' Get.name ');} else {$name = "";} $where = ($name = = '? ': "and v.name like '%{$name}% '); $list = $voter->query (" Select v.*,d.deptname from voters v inner Join Wkrj_auth_dept D on V.deptid=d.id ". $where); $file _str=", if ($list) {foreach ($list as $row) {$file _str.= $row [' name ' ]. ', '. $row [' sex ']. ', '. $row [' phone ']. ', ' $row [' Password ']. " \ n ";}} Else{echo "Export failed!";} Exit ($file _str);//iconv transcoding function $file_str=  iconv ("Utf-8", ' GBK ', $file _str); Ob_end_clean (); Echo $header _str;echo $file _str;}

The above describes the thinkphp export CSV format documents, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.