Export Excel under Thinkphp

Source: Internet
Author: User

This method uses Phpexcel to troubleshoot exporting Excel from a database

First, you need to download a phpexcel into the thinkphp directory under the three-party class library extend directory Vendor

Call Method Vendor (' PHPExcel.Classes.PHPExcel ');

Use a pub data to make a presentation.

The code is as follows:

1  Public functionExcel () {2         //Introducing the Phpexcel library file3Vendor (' PHPExcel.Classes.PHPExcel ');4         //Creating Objects5         $excel=NewPhpexcel ();6         //Excel table format, where 8 columns are briefly written7         $letter=Array(' A ', ' B ', ' C ', ' D ', ' E ', ' f ', ' f ', ' G ');8         //Table Header Array9         $tableheader=Array(' Serial number ', ' Inn name ', ' Inn address ', ' landline ', ' phone ', ' QQ ', ' email '));Ten         //Populating header information One          for($i= 0;$i<Count($tableheader);$i++) { A             $excel->getactivesheet ()->setcellvalue ("$letter[$i]1 ","$tableheader[$i]"); -         } -         //Table Array the         $data=Array( -                 Array(' 1 ', ' Lijiang Inn ', ' Kunming Lijiang ', ' 023-65987458 ', ' 13598784587 ', ' 1317615477 ', ' [email protected] '), -                 Array(' 2 ', ' Lijiang Inn ', ' Kunming Lijiang ', ' 023-65987458 ', ' 13598784587 ', ' 1317615477 ', ' [email protected] '), -                 Array(' 3 ', ' Lijiang Inn ', ' Kunming Lijiang ', ' 023-65987458 ', ' 13598784587 ', ' 1317615477 ', ' [email protected] '), +                 Array(' 4 ', ' Lijiang Inn ', ' Kunming Lijiang ', ' 023-65987458 ', ' 13598784587 ', ' 1317615477 ', ' [email protected] '), -                 Array(' 5 ', ' Lijiang Inn ', ' Kunming Lijiang ', ' 023-65987458 ', ' 13598784587 ', ' 1317615477 ', ' [email protected] '), +                 Array(' 6 ', ' Lijiang Inn ', ' Kunming Lijiang ', ' 023-65987458 ', ' 13598784587 ', ' 1317615477 ', ' [email protected] '), A                 Array(' 7 ', ' Lijiang Inn ', ' Kunming Lijiang ', ' 023-65987458 ', ' 13598784587 ', ' 1317615477 ', ' [email protected] ') at         ); -         //Populating table Information -          for($i= 2;$i<=Count($data) + 1;$i++) { -             $j= 0; -             foreach($data[$i-2] as $key=$value) { -                 $excel->getactivesheet ()->setcellvalue ("$letter[$j]$i","$value"); in                 $j++; -             } to         } +         //Create an Excel input object -         $write=NewPhpexcel_writer_excel5 ($excel); the         Header("Pragma:public"); *         Header("expires:0"); $         Header("Cache-control:must-revalidate, post-check=0, pre-check=0");Panax Notoginseng         Header("Content-type:application/force-download"); -         Header("Content-type:application/vnd.ms-execl"); the         Header("Content-type:application/octet-stream"); +         Header("Content-type:application/download");; A         Header(' content-disposition:attachment;filename= ' Inn information. xls "'); the         Header("Content-transfer-encoding:binary"); +         $write->save (' Php://output '); -}

The front-end code appears as follows

1 <  class= "Excel"><input  type= "button" value = "Export to Excel" onclick = "location.href= ' __root__/admin.php/admin/excel ';" /></ Div >

The results are as follows

This method is simple and quick, and I hope to help our friends!

Export Excel under Thinkphp

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.