Thinkphp3.2+phpexcel exporting query data to an instance of an Excel table

Source: Internet
Author: User

First download the Phpexcel plugin, we need to put the phpexcel.php and Phpexcel folder in the D:\XAMPP\htdocs\fsxb\ThinkPHP\Library\Vendor\PHPExcel directory, Then the theme code is implemented:

     Public functionDownload () {Vendor (' Phpexcel.phpexcel '); Vendor (' PHPExcel.PHPExcel.Writer.Excel2007 '); //First, create a new object Phpexcel        $objPHPExcel=New\phpexcel (); $objWriter=New\phpexcel_writer_excel2007 ($objPHPExcel); //Position AAA * Provides anchor for code location below             $objPHPExcel->setactivesheetindex (0)                              ->setcellvalue (' A1 ', ' Serial number '))                              ->setcellvalue (' B1 ', ' Merchant name ')                              ->setcellvalue (' C1 ', ' Merchant name '))                              ->setcellvalue (' D1 ', ' Merchant number '))                              ->setcellvalue (' E1 ', ' Terminal number '))                              ->setcellvalue (' F1 ', ' Serial number '))                              ->setcellvalue (' G1 ', ' model '))                              ->setcellvalue (' H1 ', ' agents ')                            ->setcellvalue (' I1 ', ' mobile number '))                            ->setcellvalue (' J1 ', ' people single time '); $startTime=i (' Get.starttime '); $endTime=i (' Get.endtime '); $agent=i (' Get.agent '); $model=i (' Get.model '); $istrue=i (' get.istrue ') ==0?0:1; if($startTime! = ' &&$endTime! = ") {                $str=$startTime.",".$endTime; $where[' Time ']=Array(' Between ',$str); }            if($agent! = ") {                $where[' Agent ']=$agent; }            if($model! = ") {                $where[' Model ']=$model; }            if($istrue==1) {                $where[' IsTrue ']=$istrue; }            $data=m (' Consumer ')->where ($where),Select (); if($data)              {                  $count=1; foreach($data  as $v)                {                      $count+=1; $l 1= "A". "$count"; $l 2= "B". "$count"; $l 3= "C". "$count"; $l 4= "D". "$count"; $l 5= "E". "$count"; $l 6= "F". "$count"; $l 7= "G". "$count"; $l 8= "H". "$count"; $l 9= "I". "$count"; $l Ten= "J". "$count"; $objPHPExcel->setactivesheetindex (0)                                              ->setcellvalue ($l 1,$v[' ID '])                                  ->setcellvalue ($l 2,$v[' CNAME '])                                   ->setcellvalue ($l 3,$v[' Mname '])                                  ->setcellvalue ($l 4, " ".$v[' Cnumber '])                                   ->setcellvalue ($l 5,$v[' Tnumber '])                                  ->setcellvalue ($l 6,$v[' Snumber '])                                   ->setcellvalue ($l 7,$v[' Model '])                                  ->setcellvalue ($l 8,$v[' Agent '])                                   ->setcellvalue ($l 9,$v[' Phone '])                                   ->setcellvalue ($l Ten,$v[' Time ']); }              }                  //get the currently active table and note that the $objactsheet is often used in the tutorial below        $objActSheet=$objPHPExcel-Getactivesheet (); //position BBB * Provides anchor for the following code location//sets the name for the currently active table        $objActSheet->settitle (' list '); //generate xlsx files in 2007excel format         Header("Content-type:application/force-download"); Header("Content-type:application/octet-stream"); Header("Content-type:application/download"); Header("Content-disposition:inline;filename= ' consumer.xlsx"); Header("Content-transfer-encoding:binary"); Header("Cache-control:must-revalidate, post-check=0, pre-check=0"); Header("Pragma:no-cache"); $objWriter->save (' Php://output '); Exit; }

Thinkphp3.2+phpexcel exporting query data to an instance of an Excel table

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.