php
public static function exportSample($headers, $data, $fileName = 'excel', $writeType = 'Xls')
{
$img_field = ['sam_img','ref_img'];
$_img_field_width = 16;
$_img_field_height = 80;
$excel = new Spreadsheet();
$sheet = $excel->setActiveSheetIndex(0);
// 设置标题单元格的样式:字体加粗,字体大小12,字体颜色红色,水平居中,垂直居中
$styleArray = [
'font' => [ //设置字体
'bold' => true,
'size' => 9,
// 'color' => array('rgb' => 'FF0000'),
],
'alignment' => [ //对其方式
'horizontal' => Excel::HORIZONTAL_CENTER, //居中
'vertical' => Excel::VERTICAL_CENTER,
'wrapText' => true, // 设置自动换行
],
'fill'=>[ //设置表格的背景色
'fillType' => Excel::FILL_SOLID,
'startColor'=>['rgb' => 'B5C6EA'], //设备背景色
]
];
$sheet->getStyle('A1:AC2')->applyFromArray($styleArray);
$styleArray = [
'borders' => [ //设备边框
'outline' => [
'borderStyle' => Excel::BORDER_THIN, // 边框样式
'color' => ['rgb' => '000000'], // 边框颜色为黑色
],
],
'alignment' => [ //对其方式
'horizontal' => Excel::HORIZONTAL_CENTER, //居中
'vertical' => Excel::VERTICAL_CENTER,
'wrapText' => true, // 设置自动换行
],
];
//循环两次-后面合并需要
for ($i=1; $i<=2;$i++){
// 设置标题
$ordA = ord('A'); //65
$key2 = ord("@"); //64
foreach ($headers as $index => $title) {
if ($ordA > ord("Z")) {
$colum = chr(ord("A")) . chr(++$key2); //超过26个字母 AA1,AB1,AC1,AD1...BA1,BB1...
} else {
$colum = chr($ordA++);
}
$_p = $colum . $i;
//写入标题并且设置样式
$sheet->setCellValue($_p, $title)->getStyle($_p)->applyFromArray($styleArray);
//图片单独处理宽度
if(in_array($index,$img_field)){
$sheet->getColumnDimension($colum)->setWidth($_img_field_width);
}
}
}
//合并单元格-列-切重写
$sheet->mergeCells('D1:M1')->setCellValue('D1', '计划产品性能参数');
$sheet->mergeCells('N1:S1')->setCellValue('N1', '对标品牌产品性能参数');
//合并单元格-行
$h = ['A','B','C','T','U','V','W','X','Y','Z','AA','AB','AC'];
foreach ($h as $k=>$y){
$sheet->mergeCells($y.'1:'.$y.'2');
}
//图片地址
$path = \Yii::getAlias('@excelImg');
self::mkdirFile($path);
$r = 3; //数据写入航标
foreach ($data as $dKey => $log) {
$column = 0;
foreach ($headers as $kk => $vv) {
$column++;
$value = $log[$kk];
if($kk == 'id'){
$value = $column;
}
if(in_array($kk,['sam_img','ref_img'])){
$_map = [
'sam_img'=>'L',
'ref_img'=>'M',
];
$img_url = $log[$kk];
if(empty($img_url)){
continue;
}
$fileInfo = pathinfo($img_url);
$file = $fileInfo['filename'] .'.'.$fileInfo['extension'];
$new_img_url = $path.'/'.$file;
//下载图片到本地
if (file_exists( $new_img_url) === false) {
$a = copy($img_url, $new_img_url);
}
if (file_exists($new_img_url)) {
$drawing = new Drawing();
$drawing->setPath($new_img_url);
$_rp = $_map[$kk].$r;
$drawing->setName('Image');
$drawing->setDescription('Image');
$drawing->setPath($new_img_url); // 设置图片路径
$drawing->setWidthAndHeight(80,80); // 设置图片宽度
$drawing->setCoordinates($_rp); // 指定单元格
// 设置图片格式选项,例如使其居中
$drawing->setResizeProportional(false); // 如果需要保持图片宽高比,设置为false
// $drawing->setOffsetX(5); // 设置横向偏移
// $drawing->setOffsetY(5); // 设置纵向偏移
$sheet->getRowDimension($r)->setRowHeight($_img_field_height);
$drawing->setWorksheet($sheet);
}
}else {
$sheet->setCellValueExplicitByColumnAndRow($column, $r, $value, DataType::TYPE_STRING2);
}
}
$r++;
}
$sheet->freezePane('A3'); //锁定行
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename=' . $fileName . date('Y-m-d') . '.xls');
header('Cache-Control: max-age=0');
$excelWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($excel, $writeType);
$excelWriter->save('php://output');
die();
}
public static function mkdirFile($imageDir,$makeDir=1)
{
if (! file_exists($imageDir)) {
if($makeDir){
$a = mkdir($imageDir,0777,true);
}else{
echo '图片目录['.$imageDir.']不存在!';
exit();
}
}
}
表格效果
图片居中暂时还没找到非常好的实现方式,有经验的大佬欢迎指导