本篇文章給大家分享的內容是thinkphp3.2與phpexcel完美案例,有著一定的參考價值,有需要的朋友可以參考一下
<br>##
<br>
// 导出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 }
<br>
<br>
<br>
<br>
注意事項:1.phpexcel下載位址:http://phpexcel.codeplex.com/<br><br>2.把Classes目錄下的檔案(PHPExcel.php和PHPExcel資料夾),放到ThinkPHP\Library\Org\Util目錄下(放到其他目錄下也可以,只要能載入類別都沒問題)。並把PHPExcel.php 改成PHPExcel.class.php(這一步很重要,你需要載入的php檔案都要改成xxx.class.php才行)<br><br>#3.手動導入類別(請注意"\"符號)<br>import("Org.Util.PHPExcel");<br>$objPHPExcel = new \PHPExcel();<br>(本人試過自動載入類,比較麻煩,因為phpexcel的文件都需要加上“namespace Org\Util;”,所以乾脆就選擇手動載入類別)<br><br>4.水平居中,垂直居中,高度等設定。注意放的位置,如果你放在末尾,那麼下一樣生效。放到頭部,第一行生效(上面程式碼是第一行生效,如果放到foreach裡面就是下一行生效)。 <br><br>5.import介紹。 import("Org.Util.PHPExcel.Writer.Excel2007"); 檔案位置:Org\Util\PHPExcel\Writer\Excel2007.class.php<br>
6.圖片位址一定要是本機。k]->setPath('./Upload/'.$img['goods_thumb']); 圖片位置:安裝目錄/Upload/xxx<br>
容易出現的問題: <br>1.'class xxx not found' 類別導入不成功,可以先在xxx.class.php 放個function ss(){} 然後輸出下xx->ss()輸出下看結果。 <br>
分析網址:http://www.thinkphp.cn/code/2112.html<br>
转自:http://www.cnblogs.com/wesky/p/5624002.html
<br>#
以上是thinkphp3.2與phpexcel完美案例的詳細內容。更多資訊請關注PHP中文網其他相關文章!