PHP匯出Excel表格及設定表格樣式
阿新 • • 發佈:2021-06-16
首先引入excel類,用的TP框架
use think\PHPExcel;
#建立物件
$objPHPExcel = new \PHPExcel();
顯示錯誤資訊
error_reporting(E_ALL);
設定標題之類屬性
$objPHPExcel->getProperties()->setCreator("這磚有點沉") ->setLastModifiedBy("這磚有點沉") ->setTitle("資料EXCEL匯出") ->setSubject("資料EXCEL匯出") ->setDescription("匯出資料")->setKeywords("excel") ->setCategory("result file");
設定表頭(自行加粗等)
$objPHPExcel->getActiveSheet()->setCellValue('A1', '時間'); $objPHPExcel->getActiveSheet()->setCellValue('B1', '進線人數'); $objPHPExcel->getActiveSheet()->setCellValue('C1', '註冊人數'); $objPHPExcel->getActiveSheet()->setCellValue('D1', '註冊比');$objPHPExcel->getActiveSheet()->setCellValue('E1', '簽約人數'); $objPHPExcel->getActiveSheet()->setCellValue('F1', '成單比');
#表頭合併並加粗
$objPHPExcel->getActiveSheet()->mergeCells("A1:F1");//合併 $objPHPExcel->getActiveSheet()->setCellValue("內容");//插入資料 $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);//加粗
#合併示例:
$objPHPExcel->getActiveSheet()->mergeCells('A1:F1'); $objPHPExcel->getActiveSheet()->mergeCells('A2:F2'); $objPHPExcel->getActiveSheet()->mergeCells('A6:F6'); $objPHPExcel->getActiveSheet()->mergeCells('A11:B11');
#字型與樣式
$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(12);//字型大小 $objPHPExcel->getActiveSheet()->getStyle('A1:F1')->getFont()->setBold(false);//是否加粗 $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);//是是否加粗 $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(12);//第一行字型大小
#設定單元格行高
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);//設定預設行高 $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);//設定第一行行高
#設定單元格寬度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);//設定A列寬度
#設定垂直居中
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
#設定水平居中
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A1:F3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
#設定左對齊
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
插入資料
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', $data['a']) ->setCellValue('B1', $data['b']) ->setCellValue('C1', $data['c']) ->setCellValue('D1', $data['d']);
設定sheet頁簽名稱
$objPHPExcel->getActiveSheet()->setTitle('資料統計-1');
將活動工作表索引設定為第一個工作表,以便Excel將其作為第一個工作表開啟
$objPHPExcel->setActiveSheetIndex(0);
設定輸出
$filename = '資料統計-' . $t; //清空緩衝區並關閉輸出緩衝 ob_end_clean(); header("Content-Type:application/force-download"); header("Content-Type:application/vnd.ms-excel"); header('Content-Disposition:attachment;filename="'.$filename.'"'); header("Content-Type:application/octet-stream"); header("Content-Type:application/download"); header("Pragma: no-cache"); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); exit;
OVER~