public static function addColumn($content=[],$fileName='按需求列导出数据实例')
{
if(empty($content)) {
/************************按需求列导出数据实例*************/
$content = [
['title1', 'title2', 'title3', 'title4', 'title5', 'title6', 'title7', 'title8', 'title9', 'title....'],
['值以', '值以', '值以', '值以', '值以', '值以', '值以', '值以', '值以', '值...']
];
/************************按需求列导出数据实例*************/
}
$maxColumn = count($content[0]);//最大列
$maxRow = count($content);//最大行数
/* 实例化phpExcel类, 导出Excel----数据 */
$objPHPExcel = new \PHPExcel();
// 开始操作excel表
$objPHPExcel->setActiveSheetIndex(0);
// 设置工作薄名称
$objPHPExcel->getActiveSheet()->setTitle(iconv('gbk', 'utf-8', 'sheet1'));
// 设置默认字体和大小
$objPHPExcel->getDefaultStyle()->getFont()->setName(iconv('gbk', 'utf-8', ''));
// $objPHPExcel->getActiveSheet()->getStyle(‘A2:D2’)->getFont()->setBold(false); //第二行是否加粗
// $objPHPExcel->getActiveSheet()->getStyle(‘A1’)->getFont()->setBold(true); //第一行是否加粗
$objPHPExcel->getDefaultStyle()->getFont()->setSize(11);
$styleArray = array(
‘font’ => array(
‘bold’ => true,
‘color’=>array(
‘argb’ => ‘ffffffff’,
)
),
‘borders’ => array (
‘allborders’ => array (
‘style’ => \PHPExcel_Style_Border::BORDER_THIN, //设置border样式
‘color’ => array (‘argb’ => ‘FF000000’), //设置border颜色
)
)
);
$Column=[];
/**************数据列表*************/
for ($i = 0; $i < $maxColumn; $i++) {
for ($j = 0; $j < $maxRow; $j++) {
$pCoordinate = \PHPExcel_Cell::stringFromColumnIndex($i) . '' . ($j + 1);
// objPHPExcel−>getActiveSheet()−>getColumnDimension(pCoordinate)->setWidth(25);
$pValue = content[j][$i];
//设置值单元数据
objPHPExcel−>getActiveSheet()−>setCellValue(pCoordinate, $pValue);
//设置单元格宽度
KaTeX parse error: Expected 'EOF', got '\PHPExcel' at position 55: …olumnDimension(\̲P̲H̲P̲E̲x̲c̲e̲l̲_Cell::stringFr…i))->setWidth(20);
Column[]=pCoordinate;
}
}
$current=current($Column);
$end=end($Column);
// // 字体和样式
$objPHPExcel->getActiveSheet()->getStyle($current.':'.$end)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle($current.':'.$end)->getFill()->getStartColor()->setARGB('333399');
$objPHPExcel->getActiveSheet()->getStyle($current.':'.$end)->applyFromArray($styleArray);
/**************数据列表*************/
// 设置水平居中
$objPHPExcel->getActiveSheet()->getStyle($current.':'.$end)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// 设置垂直居中
$objPHPExcel->getActiveSheet()->getStyle($current.':'.$end)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
// 设置行高度
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20); //设置默认行高
// // 合并
// $objPHPExcel->getActiveSheet()->mergeCells(‘A1:G1’);
//写入数据
$objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
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');
\Yii::$app->end();
spl_autoload_register(array('YiiBase','autoload'));
exit();
}