首先下载PHPEXCEL 下载地址:http://phpexcel.codeplex.com/ https://github.com/PHPOffice/PHPExcel
把Classes目录下的文件(PHPExcel.PHP和PHPExcel文件夹),放到ThinkPHP\Library\Org\Util目录下
PHPExcel.PHP 改名为 :PHPExcel.class.PHP
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.<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel");
import("Org.Util.<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel.Worksheet.Drawing");
import("Org.Util.<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel.Writer.Excel2007");
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel = new \<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel();
$objWriter = new \<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Writer_Excel2007($obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel);
$objActSheet = $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet();
// 水平居中(位置很重要,建议在最初始位置)
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Alignment::HORIZONTAL_CENTER);
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()->setHorizontal(\<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Alignment::HORIZONTAL_CENTER);
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Alignment::HORIZONTAL_CENTER);
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Alignment::HORIZONTAL_CENTER);
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(\<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Alignment::HORIZONTAL_CENTER);
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setHorizontal(\<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->setCellValue('A1','商品货号');
$objActSheet->setCellValue('B1','商品<a href="https://www.jb51.cc/tag/mingcheng/" target="_blank" class="keywords">名称</a>');
$objActSheet->setCellValue('C1','商品图');
$objActSheet->setCellValue('D1','商品条码');
$objActSheet->setCellValue('E1','商品<a href="https://www.jb51.cc/tag/shuxing/" target="_blank" class="keywords">属性</a>');
$objActSheet->setCellValue('F1','报价(港币)');
// 设置个表格宽度
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getColumnDimension('A')->setWidth(16);
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getColumnDimension('B')->setWidth(80);
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getColumnDimension('E')->setWidth(12);
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getColumnDimension('F')->setWidth(12);
// 垂直居中
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('A')->getAlignment()->setVertical(\<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Alignment::VERTICAL_CENTER);
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('B')->getAlignment()->setVertical(\<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Alignment::VERTICAL_CENTER);
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('D')->getAlignment()->setVertical(\<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Alignment::VERTICAL_CENTER);
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('E')->getAlignment()->setVertical(\<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Alignment::VERTICAL_CENTER);
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('F')->getAlignment()->setVertical(\<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_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();
// <a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a><a href="https://www.jb51.cc/tag/shengcheng/" target="_blank" class="keywords">生成</a>
$objDrawing[$k] = new \<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Worksheet_Drawing();
$objDrawing[$k]->setPath('./Upload/'.$img['goods_thumb']);
// 设置宽度高度
$objDrawing[$k]->setHeight(80);//照片高度
$objDrawing[$k]->setWidth(80); //照片宽度
/*设置<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>要插入的单元格*/
$objDrawing[$k]->setCoordinates('C'.$k);
// <a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>偏移距离
$objDrawing[$k]->setOffsetX(12);
$objDrawing[$k]->setOffsetY(12);
$objDrawing[$k]->setWorksheet($obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet());
// 表格<a href="https://www.jb51.cc/tag/neirong/" target="_blank" class="keywords">内容</a>
$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);
//<a href="https://www.jb51.cc/tag/zhongmingming/" target="_blank" class="keywords">重命名</a>表
// $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->setTitle('test');
//设置活动单指数到第一个表,所以Excel打开这是第一个表
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=\"$fileName\"");
header('Cache-Control: max-age=0');
$objWriter = \<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_IOFactory::createWriter($obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel,'Excel5');
$objWriter->save('<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>://output'); //<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>通过浏览器下载
// 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
原文链接:https://www.f2er.com/thinkphp/17961.html