cost add quert,export excel,detail modify
This commit is contained in:
@@ -240,4 +240,96 @@ class Excel extends PHPExcel
|
||||
|
||||
return fclose($fp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出excel,第一行支持多列表头合并
|
||||
* @param $data
|
||||
* @param $indexs
|
||||
* @param $headers
|
||||
* @param $sheet_title
|
||||
* @param $filename
|
||||
* @return bool
|
||||
*/
|
||||
public function excel2($data, $indexs, $headers=array(), $sheet_title='sheet1', $filename=''){
|
||||
!$sheet_title && $sheet_title = 'sheet1';
|
||||
!$filename && $filename = 'excel_' . date('YmdHis');
|
||||
set_time_limit(0); // 设置页面等待时间
|
||||
ini_set('memory_limit', -1); // 不限制内存
|
||||
|
||||
$objPHPExcel = new PHPExcel();
|
||||
$objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); // 单元格整体居右
|
||||
$objPHPExcel->setActiveSheetIndex(0); // 选中默认的第一个sheet
|
||||
$objPHPExcel->getActiveSheet()->setTitle($sheet_title); // 给 Sheet 设置名字
|
||||
|
||||
# 先生成表头
|
||||
$i = 0;
|
||||
$column_num = 0;
|
||||
foreach ($indexs as $k => $v){
|
||||
$columnkey = PHPExcel_Cell::stringFromColumnIndex($i);
|
||||
if ($column_num || in_array($k, array_keys($headers))){
|
||||
if (in_array($k, array_keys($headers)) && !$headers[$k]['is_create']){
|
||||
$column_num = $headers[$k]['column_num'];
|
||||
$title = $headers[$k]['title'];
|
||||
$headers[$k]['is_create'] = 1;
|
||||
# 第一行需合并多列的列
|
||||
$columnkey2 = PHPExcel_Cell::stringFromColumnIndex($i + $column_num - 1);
|
||||
$pRange = $columnkey.'1:'.$columnkey2.'1';
|
||||
$pCoordinate = $columnkey.'1';
|
||||
$objPHPExcel->getActiveSheet()->mergeCells($pRange);
|
||||
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($pCoordinate, $title);
|
||||
|
||||
# 水平居中
|
||||
$objStyle = $objPHPExcel->getActiveSheet()->getStyle($pCoordinate); //获取要设置单元格的样式,括号里的内容也可是:('A1:E1')
|
||||
$objAlign = $objStyle->getAlignment(); //用来设置对齐属性和单元格内文本换行的一个变量
|
||||
$objAlign->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
|
||||
}
|
||||
|
||||
# 第二行的列
|
||||
$pCoordinate = $columnkey.'2';
|
||||
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($pCoordinate, $v);
|
||||
|
||||
$column_num--;
|
||||
}
|
||||
else{
|
||||
# 第一二行需合并的列
|
||||
$pRange = $columnkey.'1:'.$columnkey.'2';
|
||||
$pCoordinate = $columnkey.'1';
|
||||
$objPHPExcel->getActiveSheet()->mergeCells($pRange);
|
||||
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($pCoordinate, $v);
|
||||
|
||||
# 垂直居中
|
||||
$objStyle = $objPHPExcel->getActiveSheet()->getStyle($pCoordinate);
|
||||
$objAlign = $objStyle->getAlignment();
|
||||
$objAlign->setVertical(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
|
||||
# 再生成数据
|
||||
$data = array_slice($data, 1);
|
||||
$j = 3;
|
||||
foreach ($data as $val) {
|
||||
$i = 0;
|
||||
// 内循环产生每一行
|
||||
foreach ($indexs as $k => $v){
|
||||
// 数字列转换为字母列 如:27变为AA
|
||||
$objPHPExcel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($i) . $j, $val[$k]);
|
||||
$i++;
|
||||
}
|
||||
$j++;
|
||||
}
|
||||
|
||||
$filename = iconv('UTF-8', 'UTF-8', $filename) . '.xls';
|
||||
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=$filename");
|
||||
header('Content-Transfer-Encoding: binary');
|
||||
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
|
||||
return $objWriter->save('php://output');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user