使用 Laravel Excel 3.1 版本來做匯出圖片到 Excel 的功能沒有成功,後來作者回復了相關的寫法 點選檢視 因為回覆的時候已經用 PhpSpreadsheet 實現了,所以沒有再嘗試 Laravel Excel 的寫法
以下是 PhpSpreadsheet 匯出部分的程式碼
public function export($data)
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
//設定sheet的名字 兩種方法
$sheet->setTitle('phpspreadsheet——demo');
$spreadsheet->getActiveSheet()->setTitle('Hello');
//設定第一行小標題
$k = 1;
$sheet->setCellValue('A' . $k, '問題');
$sheet->setCellValue('B' . $k, '選項');
$sheet->setCellValue('C' . $k, '答案');
$sheet->setCellValue('D' . $k, '圖片');
// 設定個表格寬度
$spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(16);
$spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(80);
$spreadsheet->getActiveSheet()->getColumnDimension('C')->setWidth(15);
$spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(20);
// 垂直居中
$spreadsheet->getActiveSheet()->getStyle('A')->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
$spreadsheet->getActiveSheet()->getStyle('B')->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
$spreadsheet->getActiveSheet()->getStyle('C')->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
$spreadsheet->getActiveSheet()->getStyle('D')->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
$info = $data;
// 設定A單元格的寬度 同理設定每個
$spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(20);
// 設定第三行的高度
$spreadsheet->getActiveSheet()->getRowDimension('3')->setRowHeight(50);
// A1水平居中
$styleArray = [
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
];
$sheet->getStyle('A1')->applyFromArray($styleArray);
// 將A3到D4合併成一個單元格
$spreadsheet->getActiveSheet()->mergeCells('A3:D4');
// 拆分合並單元格
$spreadsheet->getActiveSheet()->unmergeCells('A3:D4');
// 將A2到D8表格邊框 改變為紅色
$styleArray = [
'borders' => [
'outline' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => ['argb' => 'FFFF0000'],
],
],
];
// $sheet->getStyle('A2:E8')->applyFromArray($styleArray);
// 設定超連結
// $sheet->setCellValue('D6', 'www.baidu.com');
// $spreadsheet->getActiveSheet()->setCellValue('E6', 'www.baidu.com');
// 迴圈賦值
$k = 2;
foreach ($info as $key => $value) {
$sheet->setCellValue('A' . $k, $value['question']);
$sheet->setCellValue('B' . $k, $value['question_options']);
$sheet->setCellValue('C' . $k, $value['answer']);
$img = self::curlGet($value['img']);
$dir = public_path('/temp/image/');
$file_info = pathinfo($value['img']);
if (!empty($file_info['basename'])) { //過濾非檔案型別
$basename = $file_info['basename'];
is_dir($dir) OR mkdir($dir, 0777, true); //進行檢測檔案是否存在
file_put_contents($dir . $basename, $img);
$drawing[$k] = new Drawing();
$drawing[$k]->setName('Logo');
$drawing[$k]->setDescription('Logo');
$drawing[$k]->setPath($dir . $basename);
$drawing[$k]->setWidth(80);
$drawing[$k]->setHeight(80);
$drawing[$k]->setCoordinates('D'.$k);
$drawing[$k]->setOffsetX(12);
$drawing[$k]->setOffsetY(12);
$drawing[$k]->setWorksheet($spreadsheet->getActiveSheet());
} else {
$sheet->setCellValue('D' . $k, '');
}
$sheet->getRowDimension($k)->setRowHeight(80);
$k++;
}
$file_name = date('Y-m-d', time()) . rand(1000, 9999);
// 第一種儲存方式
/*$writer = new Xlsx($spreadsheet);
//儲存的路徑可自行設定
$file_name = '../'.$file_name . ".xlsx";
$writer->save($file_name);*/
// 第二種直接頁面上顯示下載
$file_name = $file_name . ".xls";
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $file_name . '"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($spreadsheet, 'Xls');
// 注意createWriter($spreadsheet, 'Xls') 第二個引數首字母必須大寫
$writer->save('php://output');
}
public function getClient(){
$client = new Client();
return $client;
}
public static function curlGet($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 這個是重點 請求https。
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結