PHP-side code
Require plug-ins PHPExcel, on ThinkPHP / Library / Vendor / directory
Call the export method
public function expUser () {// derived Excel
$ xlsName = "User Datagram the User";
$ xlsCell = Array (
Array ( 'ID', 'ID'),
Array ( 'the userid', 'the userid'),
Array ( ' card ',' card '),
Array (' addr ',' Bank '),
);
$ xlsModel = M (' Bank ');
$ = $ xlsModel- xlsData> Field, (' ID, the userid, Card, addr ' ) -> SELECT ();
$ this-> exportExcel ($ xlsName, xlsCell $, $ xlsData);
}
Export method
function exportExcel public ($ expTitle, expCellName $, $ expTableData) {
$ xlsTitle = iconv ( 'UTF-. 8', 'GB2312', $ expTitle); // file name
$ fileName = $ expTitle.date ( '_ Ymd_His'); // or $ xlsTitle file name can be set according to its own
$ cellNum = COUNT (expCellName $);
$ dataNum = COUNT ($ expTableData);
Vendor ( "PHPExcel.PHPExcel");
$ objPHPExcel = new new \ PHPExcel ();
$ cellName = array ( 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L ',' M ',' N ',' O ',' P ',' Q ',' R ',' S ',' T ',' U ',' V ',' W ',' X ', 'Y', 'Z', 'AA', 'AB', 'AC ',' AD ',' AE ',' AF ',' AG ',' AH ',' AI ',' AJ ',' AK ',' AL ',' AM ',' AN ',' AO ' , 'the AP', 'AQ', 'the AR', 'the AS', 'the AT', 'AU', 'the AV', 'AW', 'AX', 'AY', 'AZ');
$ objPHPExcel-> getActiveSheet (0) -> mergeCells ( .. 'A1:' $ cellName [$ cellNum-1] '1'); // merge cells//Merge Cells//Merge Cells
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.' Export time:'.date('Y-m-d H:i:s'));//第一行标题
for($i=0;$i<$cellNum;$i++){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
}
// Miscellaneous glyphs, UTF-8
for($i=0;$i<$dataNum;$i++){
for($j=0;$j<$cellNum;$j++){
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
}
}
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
header ( "Content-Disposition: attachment ; filename = $ fileName.xls"); // attachment new window Print this window inline print
$ objWriter = \ PHPExcel_IOFactory :: createWriter ($ objPHPExcel, 'Excel 5');
$ objWriter-> the Save ( 'PHP: // Output');
Exit;
}