<?php
public static function export($data,$fileName) {
ini_set('max_execution_time', '0');
ini_set('memory_limit', '-1');
require_once Url::to('@common') . '/extensions/PHPExcel-1.8/Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new \PHPExcel();
// 设置默认字体和大小
$objPHPExcel->getDefaultStyle()->getFont()->setName('宋体');
$objPHPExcel->getDefaultStyle()->getFont()->setSize(14);
// Set alignments 设置单元格对齐方式
$objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//水平
$objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直
// 设置单元格为文本
$objPHPExcel->getDefaultStyle()->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
//Set WrapText
$objPHPExcel->getDefaultStyle()->getAlignment()->setWrapText(true);
// Create a first sheet, representing sales data
$sheet = $objPHPExcel->getActiveSheet();
$title = array("{$fileName}");
foreach ($title as $k => $t) {
$sheet->setCellValueByColumnAndRow($k, 1, $t);
}
$title_line2 = ['序号'];
foreach ($title_line2 as $k => $t) {
$sheet->setCellValueByColumnAndRow($k, 2, $t);
}
//合并
$sheet->mergeCells('A1:Q1');
// Set column widths
$sheet->getColumnDimension('A')->setWidth(7);
$sheet->getColumnDimension('B')->setWidth(10);
$sheet->getColumnDimension('C')->setWidth(20);
$sheet->freezePane('A3');
// Set row heights
$sheet->getDefaultRowDimension()->setRowHeight(30);
$sheet->getRowDimension('1')->setRowHeight(37);
$sheet->getRowDimension('2')->setRowHeight(20);
$sheet->getStyle('A1')->getFont()->setSize(30);
$sheet->getStyle('A1:C2')->getFont()->setBold(true);
$sheet->getStyle( 'A2:C2')->applyFromArray(
array(
'fill' => array(
'type' => \PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => 'B4C6E7')
)
)
);
$styleThinBlackBorderOutline = array(
'borders' => array(
'allborders' => array( //设置全部边框
'style' => \PHPExcel_Style_Border::BORDER_THIN //粗的是thick
),
),
);
$sheet->getStyle( 'A2:Q'.(count($data)+2))->applyFromArray($styleThinBlackBorderOutline);
foreach ($data as $k => $value) {
$unit = RecommendUnit::findOne($value['recommend_unit_id']);
$unit_type_name = $unit ? $unit->name : '';
$row = $k+3;
$sheet->getRowDimension($row)->setRowHeight(42);
$sheet->setCellValueByColumnAndRow(0, $row, $k+1);
$sheet->setCellValueByColumnAndRow(1, $row, $value['es_code']);
$sheet->setCellValueByColumnAndRow(2, $row, $value['es_name']);
$sheet->setCellValueByColumnAndRow(3, $row, ExpertInfo::$type_dict[$value['type']]);
}
$sheet->setTitle("{$fileName}");
$fileName = "{$fileName}.xlsx";
$fileName = PdUtils::getFileNameByUrlencode($fileName);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;Charset=utf-8;');
header('Content-Disposition: attachment;filename="' . $fileName . '"');
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setOffice2003Compatibility(true); //兼容2003
$objWriter->save("php://output");
exit;
}