画像ファイルの挿入

Demo

sample.php

PHP

PHPindex.phpdownload

<?php

	require_once 'vendor/autoload.php';

	/* PHPExcelオブジェクトの作成 */
	$spreadsheet = new PHPExcel();

	$sheet = $spreadsheet->getActiveSheet();

	$fileName = "sample.xlsx";			//ファイル名を設定

	$sheet->setTitle("シートタイトル");	//シートタイトル名を設定

	/* 画像の追加 */
	$drawing = new PHPExcel_Worksheet_Drawing();

	$drawing->setPath('logo1.png');		//貼り付ける画像のパス

	$drawing->setCoordinates('B3');		//追加するセル位置

	//$drawing->setWidth(100);				//画像の幅(px)

	//$drawing->setHeight(100);			//画像の高さ(px)

	//$drawing->setName('名前');			//ファイル名

	//$drawing->setDescription('概要');	//画像の概要

	//$drawing->setOffsetX(110);			//横方向へ位置をずらす(px)

	//$drawing->setRotation(30);			//回転(30度右に傾く)

	//$drawing->getShadow()->setVisible(true);	//ドロップシャドウ

	$drawing->setWorksheet($spreadsheet->getActiveSheet());

	/* Excelファイルのダウンロード */
	header("Content-Description: File Transfer");

	header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');	//vnd.openxmlformats-officedocument.spreadsheetml.sheet:EXCELファイル( .xlsx OFFICE 2007以降), vnd.ms-excel:EXCELファイル( .xls OFFICE 2007より過去)

	header('Content-Disposition: attachment; filename="'.$fileName.'"');	//ダウンロード時のファイル名をセット

	header('Cache-Control: no-store, max-age=0');	//新しいリソースのキャッシュを防止、キャッシュのクリア

	ob_end_clean();	//バッファ消去

	$writer = PHPExcel_IOFactory::createWriter($spreadsheet, 'Excel2007');	//Excel5:Excel97~2003, Excel2007:Excel2007~

	$writer->save('php://output');
?>
© 2025 wayday