简单粗爆
$objPHPExcel = new PHPExcel();
// 全局上下左右居中
$objPHPExcel->getDefaultStyle()
->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER) //水平居中
->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直居中
// 设置统一字体
$objPHPExcel->getDefaultStyle()->getFont()->setName('微软雅黑');
// 设置列宽
$objPHPExcel->getActiveSheet()->getColumnDimension("A")->setWidth(5);
// 设置行高
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(44);
// 合并必须放到第一步
$sheet->mergeCells("A{$topNumber}:" . end($cell) . $topNumber);
// 插入内容
$objPHPExcel->setActiveSheetIndex(0)->setCellValue("A{$topNumber}", $title);
// 设置字体大小、加粗、颜色、对齐方式
$font_style_array = array(
'font' => array(
'size' => 14,
'bold' => true,
'color' => array('rgb' => '669900'),
'alignment' => array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER)
)
);
$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($font_style_array);
// 背景颜色
$sheet$objPHPExcel->getActiveSheet()->getStyle("A{$startRow}:" . end($cell) . $startRow)
->getFill()
->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()
->setRGB('C0C0C0');
// 设置边框
$border_style= array('borders' => array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN)));
$objPHPExcel->getActiveSheet()->getStyle("A{$startRow}:" . end($cell) . $startRow)->applyFromArray($border_style);
// 设置活动 sheet 名称
$objPHPExcel->getActiveSheet()->setTitle($sheet_name);
// 科学计数法
$val = decimalNotation($val);
/**
* 科学计数法转换
* @param $num
* @return string
*/
function decimalNotation($num)
{
$parts = explode('E', $num);
if (count($parts) != 2) {
return $num;
}
$exp = abs(end($parts)) + 3;
$decimal = number_format($num, $exp, '.', '');
$decimal = rtrim($decimal, '0');
return rtrim($decimal, '.');
}
// 日期为数字,转为正常日期
$val = gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($val));