使用php加phpexcel類庫將數據導出到表格
阿新 • • 發佈:2019-02-28
表示 iter nbsp public new 直接 數據 ring amp
/**
* execl數據導出
* @param string $file_name 用於導出生成文件名的前綴
* @param array $cellName 表頭及字段名
* @param array $data 導出的表數據
* @param string $top_title 表格頂部的標題
* phpexcel下載地址:https://pan.baidu.com/s/1e6WXeg3csRCIaSFaqwLKzQ
*
* 特殊處理:合並單元格需要先對數據進行處理
*/
function Export2Excel($file_name,$cellName,$data,$top_title=‘‘)
{
// $cellName=[
// [‘數據的鍵名‘,‘列名‘,‘控制合並單元格‘,‘列寬度‘,‘內容顯示位置‘],//前兩個必填,後三個選填
// [‘id‘,‘ID‘,‘‘,‘100‘,‘LEFT‘],
// [‘name‘,‘姓名‘,‘‘,‘100‘,‘LEFT‘],
// [‘age‘,‘年齡‘,‘‘,‘100‘,‘RIGHT‘],
// ];
// $data=[
// [‘id‘=>‘1‘,‘name‘=>‘tyong‘,‘age‘=>25],
// [‘id‘=>‘2‘,‘name‘=>‘tt‘,‘age‘=>12],
// ];
//引入核心文件
vendor("PHPExcel.PHPExcel");
$objPHPExcel = new \PHPExcel();
//定義配置
$topNumber = 2;//頂部標題占用1行,所以從第二行開始
$xlsTitle = iconv(‘utf-8‘, ‘gb2312‘, $file_name);//文件名稱
$fileName = $file_name.date(‘_YmdHis‘);//文件名稱
$cellKey = 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‘,‘AP‘,‘AQ‘,‘AR‘,‘AS‘,‘AT‘,‘AU‘,‘AV‘,‘AW‘,‘AX‘,‘AY‘,‘AZ‘
);
$objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(30);//所有單元格(列)默認寬度
//寫在處理的前面(了解表格基本知識,已測試)
// $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);//所有單元格(行)默認高度
// $objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);//所有單元格(列)默認寬度
// $objPHPExcel->getActiveSheet()->getRowDimension(‘1‘)->setRowHeight(30);//設置行高度
// $objPHPExcel->getActiveSheet()->getColumnDimension(‘C‘)->setWidth(30);//設置列寬度
// $objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getFont()->setSize(18);//設置文字大小
// $objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getFont()->setBold(true);//設置是否加粗
// $objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);// 設置文字顏色
// $objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//設置文字居左(HORIZONTAL_LEFT,默認值)中(HORIZONTAL_CENTER)右(HORIZONTAL_RIGHT)
// $objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
// $objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);//設置填充顏色
// $objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getFill()->getStartColor()->setARGB(‘FF7F24‘);//設置填充顏色
//處理頂部標題
if(!empty($top_title)){
$objPHPExcel->getActiveSheet()->mergeCells(‘A1:‘.$cellKey[count($cellName)-1].‘1‘);//合並單元格(如果要拆分單元格是需要先合並再拆分的,否則程序會報錯)
$objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘A1‘,$top_title);
$objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getFont()->setSize(18);
$objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
}else{
$topNumber=1;
}
//處理表頭
foreach ($cellName as $k=>$v)
{
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellKey[$k].$topNumber, $v[1]);//設置表頭數據
//$objPHPExcel->getActiveSheet()->freezePane($cellKey[$k].($topNumber+1));//凍結窗口,作用不詳,會出現一條豎線
$objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getFont()->setBold(true);//設置是否加粗
$objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
if($v[3] > 0)//大於0表示需要設置寬度
{
$objPHPExcel->getActiveSheet()->getColumnDimension($cellKey[$k])->setWidth($v[3]);//設置列寬度
}
}
//處理數據
foreach ($data as $k=>$v)
{
foreach ($cellName as $k1=>$v1)
{
$objPHPExcel->getActiveSheet()->setCellValue($cellKey[$k1].($k+1+$topNumber), $v[$v1[0]]);
if($v[‘end‘] > 0)
{
if($v1[2] == 1)//這裏表示合並單元格
{
$objPHPExcel->getActiveSheet()->mergeCells($cellKey[$k1].$v[‘start‘].‘:‘.$cellKey[$k1].$v[‘end‘]);
$objPHPExcel->getActiveSheet()->getStyle($cellKey[$k1].$v[‘start‘])->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
}
}
if($v1[4] != "" && in_array($v1[4], array("LEFT","CENTER","RIGHT")))
{
$v1[4] = eval(‘return \PHPExcel_Style_Alignment::HORIZONTAL_‘.$v1[4].‘;‘);
//這裏也可以直接傳常量定義的值,即left,center,right;小寫的strtolower
$objPHPExcel->getActiveSheet()->getStyle($cellKey[$k1].($k+1+$topNumber))->getAlignment()->setHorizontal($v1[4]);
}
}
}
//導出execl
ob_end_clean();//清除緩沖區,避免亂碼
header(‘pragma:public‘);
header(‘Content-type:application/vnd.ms-excel;charset=utf-8;name="‘.$xlsTitle.‘.xls"‘);
header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘);
$objWriter->save(‘php://output‘);
exit;
}
使用php加phpexcel類庫將數據導出到表格