thinkphp3.2中实现phpexcel导出带生成图片示例
程序员文章站
2024-03-07 11:25:39
首先下载phpexcel 下载地址: https://github.com/...
首先下载phpexcel 下载地址: https://github.com/phpoffice/phpexcel
把classes目录下的文件(phpexcel.php和phpexcel文件夹),放到thinkphp\library\org\util目录下
phpexcel.php 改名为 :phpexcel.class.php
// 导出exl public function look_down(){ $id = i('get.id'); $m = m ('offer_goods'); $where['offer_id'] = $id; $data = $m->field('goods_id,goods_sn,goods_name,barcode,goods_type,price')->select(); // 导出exl import("org.util.phpexcel"); import("org.util.phpexcel.worksheet.drawing"); import("org.util.phpexcel.writer.excel2007"); $objphpexcel = new \phpexcel(); $objwriter = new \phpexcel_writer_excel2007($objphpexcel); $objactsheet = $objphpexcel->getactivesheet(); // 水平居中(位置很重要,建议在最初始位置) $objphpexcel->setactivesheetindex(0)->getstyle('a')->getalignment()->sethorizontal(\phpexcel_style_alignment::horizontal_center); $objphpexcel->setactivesheetindex(0)->getstyle('b1')->getalignment()->sethorizontal(\phpexcel_style_alignment::horizontal_center); $objphpexcel->setactivesheetindex(0)->getstyle('c')->getalignment()->sethorizontal(\phpexcel_style_alignment::horizontal_center); $objphpexcel->setactivesheetindex(0)->getstyle('d')->getalignment()->sethorizontal(\phpexcel_style_alignment::horizontal_center); $objphpexcel->setactivesheetindex(0)->getstyle('e')->getalignment()->sethorizontal(\phpexcel_style_alignment::horizontal_center); $objphpexcel->setactivesheetindex(0)->getstyle('f')->getalignment()->sethorizontal(\phpexcel_style_alignment::horizontal_center); $objactsheet->setcellvalue('a1', '商品货号'); $objactsheet->setcellvalue('b1', '商品名称'); $objactsheet->setcellvalue('c1', '商品图'); $objactsheet->setcellvalue('d1', '商品条码'); $objactsheet->setcellvalue('e1', '商品属性'); $objactsheet->setcellvalue('f1', '报价(港币)'); // 设置个表格宽度 $objphpexcel->getactivesheet()->getcolumndimension('a')->setwidth(16); $objphpexcel->getactivesheet()->getcolumndimension('b')->setwidth(80); $objphpexcel->getactivesheet()->getcolumndimension('c')->setwidth(15); $objphpexcel->getactivesheet()->getcolumndimension('d')->setwidth(20); $objphpexcel->getactivesheet()->getcolumndimension('e')->setwidth(12); $objphpexcel->getactivesheet()->getcolumndimension('f')->setwidth(12); // 垂直居中 $objphpexcel->getactivesheet()->getstyle('a')->getalignment()->setvertical(\phpexcel_style_alignment::vertical_center); $objphpexcel->getactivesheet()->getstyle('b')->getalignment()->setvertical(\phpexcel_style_alignment::vertical_center); $objphpexcel->getactivesheet()->getstyle('d')->getalignment()->setvertical(\phpexcel_style_alignment::vertical_center); $objphpexcel->getactivesheet()->getstyle('e')->getalignment()->setvertical(\phpexcel_style_alignment::vertical_center); $objphpexcel->getactivesheet()->getstyle('f')->getalignment()->setvertical(\phpexcel_style_alignment::vertical_center); foreach($data as $k=>$v){ $k +=2; $objactsheet->setcellvalue('a'.$k, $v['goods_sn']); $objactsheet->setcellvalue('b'.$k, $v['goods_name']); $img = m('goods')->where('goods_id = '.$v['goods_id'])->field('goods_thumb')->find(); // 图片生成 $objdrawing[$k] = new \phpexcel_worksheet_drawing(); $objdrawing[$k]->setpath('./upload/'.$img['goods_thumb']); // 设置宽度高度 $objdrawing[$k]->setheight(80);//照片高度 $objdrawing[$k]->setwidth(80); //照片宽度 /*设置图片要插入的单元格*/ $objdrawing[$k]->setcoordinates('c'.$k); // 图片偏移距离 $objdrawing[$k]->setoffsetx(12); $objdrawing[$k]->setoffsety(12); $objdrawing[$k]->setworksheet($objphpexcel->getactivesheet()); // 表格内容 $objactsheet->setcellvalue('d'.$k, $v['barcode']); $objactsheet->setcellvalue('e'.$k, $v['goods_type']); $objactsheet->setcellvalue('f'.$k, $v['price']); // 表格高度 $objactsheet->getrowdimension($k)->setrowheight(80); } $filename = '报价表'; $date = date("y-m-d",time()); $filename .= "_{$date}.xls"; $filename = iconv("utf-8", "gb2312", $filename); //重命名表 // $objphpexcel->getactivesheet()->settitle('test'); //设置活动单指数到第一个表,所以excel打开这是第一个表 $objphpexcel->setactivesheetindex(0); header('content-type: application/vnd.ms-excel'); header("content-disposition: attachment;filename=\"$filename\""); header('cache-control: max-age=0'); $objwriter = \phpexcel_iofactory::createwriter($objphpexcel, 'excel5'); $objwriter->save('php://output'); //文件通过浏览器下载 // end }
水平居中,垂直居中,高度 等设置。注意放的位置,如果你放在末尾,那么是下一样生效。放到头部,第一行生效(上面代码是第一行生效,如果放到foreach里面就是下一行生效)。
import介绍。import("org.util.phpexcel.writer.excel2007"); 文件位置:org\util\phpexcel\writer\excel2007.class.php
图片地址一定要是本地。objdrawing[ objdrawing[k]->setpath('./upload/'.$img['goods_thumb']); 图片位置:安装目录/upload/xxx
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。