php生成 excel带图片方法

//参数为数组
function exportExcel($data){
    //导入excel类
include './PHPExcel/Classes/PHPExcel.php';
/*实例化excel类*/

$excel = new PHPExcel();

/*实例化excel图片处理类*/

$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('Customer_Name', 'Customer_Email', 'Product_Image', 'Product_Name', 'Product_Sku','Product_Describe','Product_Quantity','Order ID','Send_Adress');

/*填充表格表头*/

for($i = 0;$i < count($tableheader);$i++) {

$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");

}
/*设置表格数据*/
  

/*填充表格内容*/

for ($i = 0;$i < count($data);$i++) {

$j = $i+2;

/*设置表格宽度*/

//$objActSheet->getColumnDimension("$letter[$i]")->setWidth();
     //设置自适应
$excel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
$excel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
//$excel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
      //设置固定长度
$excel->getActiveSheet()->getColumnDimension('C')->setWidth(80);
$excel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
$excel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
$excel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
$excel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
$excel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
/*设置表格高度*/

$excel->getActiveSheet()->getRowDimension($j)->setRowHeight(70);
/*向每行单元格插入数据*/
    //偶数行颜色设置
if($j%2!=1) {
$excel->getActiveSheet()->getStyle($j)->getFont()->setBold(true)->setSize(10)->getColor()->setRGB('FF0000');
}
for ($row = 0;$row < count($data[$i]);$row++) {
        //多个图片地址用#隔开,第三列设置图片
if ($row == 2&&(substr($data[$i][$row],-4)=="jpg#")) {
$img = explode('#',$data[$i][$row]);

for($m=0;$m /*实例化插入图片类*/
$objDrawing = new PHPExcel_Worksheet_Drawing();
/*设置图片路径 切记:只能是本地图片*/
//$objDrawing->setPath("./no_image.jpg");
            //根据自己的图片路由设置路径
$objDrawing->setPath("../images/" . $img[$m]);

/*设置图片高度*/
$objDrawing->setHeight(100);
/*设置图片要插入的单元格*/

$objDrawing->setCoordinates("$letter[$row]$j");

/*设置图片所在单元格的格式*/
            //设置同一个表格里图片的偏移量
$objDrawing->setOffsetX(0+$m*100 );

$objDrawing->setRotation(20);

$objDrawing->getShadow()->setVisible(true);

$objDrawing->getShadow()->setDirection(50);

$objDrawing->setWorksheet($excel->getActiveSheet());

continue;

}
}
$result = $data[$i][$row];
$excel->getActiveSheet()->setCellValue("$letter[$row]$j","$result");

}

}
/*实例化excel输入类并完成输出excel文件*/
$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=".time().".xls");

header("Content-Transfer-Encoding:binary");
$write->save('php://output');
}

你可能感兴趣的:(php生成 excel带图片方法)