Thinkphp uses PHPExcel to export Excel

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to learn Thinkphp and use PHPExcel to export Excel.
// Export an Excel table
Public function export ($ data, $ excelFileName, $ sheetTitle ){

$ This->__ construct ();
/* Instantiate class */
$ ObjPHPExcel = new PHPExcel ();

/* Set the output excel file to 2007 compatible format */
// $ ObjWriter = new PHPExcel_Writer_Excel5 ($ objPHPExcel); // non-2007 format
$ ObjWriter = new PHPExcel_Writer_Excel2007 ($ objPHPExcel );

/* Set the current sheet */
$ ObjPHPExcel-> setActiveSheetIndex (0 );

$ ObjActSheet = $ objPHPExcel-> getActiveSheet ();

/* Set the width */
$ ObjPHPExcel-> getActiveSheet ()-> getColumnDimension ('A')-> setWidth (10 );
$ ObjPHPExcel-> getActiveSheet ()-> getColumnDimension ('B')-> setWidth (60 );



/* Sheet title */
$ ObjActSheet-> setTitle ($ sheetTitle );

$ I = 2;
Foreach ($ data as $ value)
{
/* Excel file content */
$ J = 'a ';
Foreach ($ value as $ value2)
{
// $ Value2 = iconv ("gbk", "UTF-8", $ value2 );
$ ObjActSheet-> setCellValue ($ j. $ I, $ value2 );
$ J ++;
}
$ I ++;
}


/* Generate to the browser and provide download */
Ob_end_clean (); // clear the cache
Header ("Pragma: public ");
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 ");
Header ("Content-Type: application/octet-stream ");
Header ("Content-Type: application/download ");
Header ('content-Disposition: attachment; filename = "'.w.excelfilename.'.xlsx "');
Header ("Content-Transfer-Encoding: binary ");
$ ObjWriter-> save ('php: // output ');
}
All files have been packaged. If you are interested, you can download and test them.

Thinkphp#phpexcel.zip (3.05 KB download: 1139 times)

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.