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