thinkphp3.2中實現phpexcel匯出帶產生圖片

來源:互聯網
上載者:User
本篇文章主要介紹了thinkphp3.2中實現phpexcel匯出帶產生圖片樣本,內容挺不錯的,現在分享給大家,也給大家做個參考。

首先下載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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.