<?php include '/phpexcel/PHPExcel.php'; $excel = new PHPExcel(); $objDrawing = new PHPExcel_Worksheet_Drawing(); /*設置文本對齊方式*/ $excel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $excel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); $objActSheet = $excel->getActiveSheet(); $letter = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N'); /*設置表頭數據*/ $tableheader = array('姓名', '性別', '年齡', '班級', '頭像'); /*填充表格表頭*/ for($i = 0;$i < count($tableheader);$i++) { $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]"); } /*設置表格數據*/ $data = array( array('小王', '男', '20', 'CS12', 'test.jpg'), array('小李', '女', '21', 'CS12', 'test.jpg'), array('小周', '男', '22', 'CS12', 'test.jpg'), array('小趙', '女', '23', 'CS12', 'test.jpg'), array('小張', '男', '24', 'CS12', 'test.jpg') ); /*填充表格內容*/ for ($i = 0;$i < count($data);$i++) { $j = $i + 2; /*設置表格寬度*/ $objActSheet->getColumnDimension("$letter[$i]")->setWidth(20); /*設置表格高度*/ $excel->getActiveSheet()->getRowDimension($j)->setRowHeight(100); /*向每行單元格插入數據*/ for ($row = 0;$row < count($data[$i]);$row++) { if ($row == (count($data[$i]) -1 )) { /*實例化插入圖片類*/ $objDrawing = new PHPExcel_Worksheet_Drawing(); /*設置圖片路徑*/ $objDrawing->setPath($data[$i][$row]); /*設置圖片高度*/ $objDrawing->setHeight(100); /*設置圖片要插入的單元格*/ $objDrawing->setCoordinates("$letter[$row]$j"); /*設置圖片所在單元格的格式*/ $objDrawing->setOffsetX(80); $objDrawing->setRotation(20); $objDrawing->getShadow()->setVisible(true); $objDrawing->getShadow()->setDirection(50); $objDrawing->setWorksheet($excel->getActiveSheet()); continue; } $excel->getActiveSheet()->setCellValue("$letter[$row]$j","111"); } } $write = new PHPExcel_Writer_Excel5($excel); header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/force-download"); header("Content-Type:application/vnd.ms-execl"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download");; header('Content-Disposition:attachment;filename="測試文件.xls"'); header("Content-Transfer-Encoding:binary"); $write->save('php://output'); |
|
來自: xibeifneg3 > 《IT-php》