1.导出excel表(Thinkphp)
/**
* 导出excel表
* @param string $fileTitle 生成文件名称
* @param string $title 名称
* @return [type] [description]
*/
function newImportExcelData($fileName,$title,$valueArray='',$tableTopArray=''){
//require_once(dirname(__FILE__).'/phpexcel/PHPExcel.php');
//
vendor('phpexcel.PHPExcel');
$objPHPExcel = new \PHPExcel();
// work start
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle($title);
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); // Needs to be set to true in order to enable any worksheet protection!
//$objPHPExcel->getActiveSheet()->protectCells('A3:E13', 'PHPExcel');
// 处理表格开始
// 表格头
$cols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$num = 1; // execl从1开始
foreach ($tableTopArray as $k => $v) {
$key_i = $cols{$k}.$num;
$objPHPExcel->getActiveSheet()->setCellValue($key_i,$v); // 设置第一行为表头
$objPHPExcel->getActiveSheet()->getStyle($key_i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); // 第一行全部居中
$objPHPExcel->getActiveSheet()->getStyle($key_i)->getFont()->setBold(true); // 第一行字体加粗
$objPHPExcel->getActiveSheet()->getColumnDimension($key_i)->setAutoSize(true);
}
// 设置数据,表格具体数据
foreach ($valueArray as $k => $v) {
$i = $k+2;
$j = 0;
foreach ($v as $value) {
$objPHPExcel->getActiveSheet()->setCellValue($cols{$j} . $i, $value);
$j++;
}
}
// 处理表格结束
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=".$fileName.".xls");
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}