thinkphp Excel Batch Export-sub-table export

Source: Internet
Author: User
Tags local time

The day before yesterday, a brother asked me if I can the data in the database, batch, sub-table, set the number of each table, so the data exported, think about. Think it can be achieved, think of a simple to write a code, but also encountered difficulties, that is, "set the number of each table" data when there is a problem, the last compromise of a way.

Do a front-end submission of each table how many data, can be set parameters.

Experimental data 5,000 article

Front-end Code

<! DOCTYPE html> 

Background code

<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends  controller {//Add Data     public function index () {    $ Count = d ("User")->count ();     if (is_post) {     $num   = i (' Post.num ');     $grade  =  $count/$num +1;    for ($i =1 , $i < $grade, $i + +) {     $url  =  "http://127.0.0.1/index.php/Home/Index/demo?strat=". $i. " &end= ". $num;echo " <script>window.open (' $url ') </script> ";    }     echo  "Done";    }else{     $this->assign ("num", $count);     $this->display ();        }     }    //Bulk Export Data Public function demo () {$strat  = i (' Get.strat '); $end = i (' get.end '); $ResultInfo  = d (' User ')->page ("$strat, $end")->select ();     //  Empty (Erase) buffer and turn off output buffer     ob_end_clean ();     // Introduce Phpexcel library file     import ("Org.Util.PHPExcel");     //Create Object       $excel  = new \phpexcel ();     $excel->getactivesheet () Settitle (' complaint list ');    //  set cell height     //  all cells default height       $excel->getactivesheet ()->getdefaultrowdimension ()->setrowheight ();     //  default height of first row      $excel->getactivesheet ()->getrowdimension (' 1 ') Setrowheight (;    // ) Vertical Center      $excel->getactivesheet () Getdefaultstyle ()->getalignment ()->setvertical (\phpexcel_style_alignment::vertical_center);     //  Set Horizontal Center      $excel->getactivesheet ()->getdefaultstyle ()->getalignment ()->sethorizontal (\PHPExcel_ style_alignment::horizontal_center)     //excel table format      $letter  =  array (' A ', ' B ', ' C ', ' D ', ' E ', ' f ', ' f ', ' G ', ' H ', ' I ', ' Z ', ' K ');     //Set Header       $tableheader  = array (' Serial number ', ' Insertion time ', ' Modified time ', ' data status ', ' name ', ' Password ', ' phone number ', ' gender ', ' age ', ' email ', ' address ');     //Set table Header table width      $tablestyle  = array (         array (' width ' = ' 5 '),     //serial number          array (' width ' = ' + '),    //insertion time          array (' width ' = ' + '),    //modified time         array (' Width ' = ' + '),    //data status         array (' width ' = ' 30 '),    //name &nbsP;       array (' width ' = '),    //password          array (' width ' = ' + '),    //password          array (' width ' = ' + '),    //phone number          array (' width ' = ' ten '),    //sex         array (' Width ' = '),    //age         array (' width ' = ' 80 ') ,    //mailbox         array (' width ' = '),    //address     )     //Filling header information     for ($i  =  0; $i  < count ($tableheader), $i + +)  {        $ Excel->getactivesheet ()->setcellvalue ("$letter [$i]1", "$tableheader [$i]");          $excel->getacTivesheet ()->getcolumndimension ($letter [$i])->setwidth ($tablestyle [$i] [' width ']);     }    //Fill table Information     for  ($i  = 2; $i  <=  Count ($ResultInfo)  + 1; $i + +)  {         $j  = 0;         foreach  ($ResultInfo [$i  - 2] as  $key = > $value)  {             $excel Getactivesheet ()->setcellvalue ("$letter [$j] $i", "$value");              $j ++;        }    }     //The Open browser window does not automatically close     //  $write when you save the file to the server  = new \ Phpexcel_writer_excel5 ($excel);    //  $filename  =  "./public/excel/". Date (' Y-m-d ', Time ()). " -". $strat.". XLS ";    //  $write->save ($filename);     //Direct download Code       $write  = new \phpexcel_writer_excel5 ($excel);     $write->save ($ filename);     header ("Pragma: public");     header ("Expires:  0 ");     header (" expires: 0 ");     header (" Cache-Control: Must-revalidate, post-check=0, pre-check=0 ");     header (" Content-Type: Application/force-download ");     header (" content-type:application/vnd.ms-execl;charset= Utf-8 ");     header (" Content-type:application/octet-stream ");     header ("Content-type:application/download");     header (' Content-disposition:attachment;filename= '. Date (' y-m-d '). " -". $strat." XLS ');     header ("Content-transfer-encoding:binary");     $write Save (' PHP://output ');}} 

Shortcomings, is saved in the local time, the download completed blank window does not automatically close, this is not a perfect solution.

About thinkphp using Phpexcel to import and export batches, you can view related articles.

thinkphp Excel Batch Export-sub-table export

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.