php导出照片,TP5导出excel图片和数据--先下载图片到本地服务器
## :-: TP5導出excel圖片和數據--先下載圖片到本地服務器
~~~
public function Export( ) {
set_time_limit(0);
ini_set('memory_limit','512M');
ini_set('max_execution_time',0);
// 獲取數據
$data = $this->service->setHost('shop_data')->post('/goods/goods/all', input());
$data = json_decode($data,true)['data'];
$objPHP = new \PHPExcel();
$objActSheet = $objPHP->getActiveSheet();
//設置當前的表格
$objPHP->setActiveSheetIndex(0);
$Color = new \PHPExcel_Style_Color(); /*實例化插入圖片類*/
// 設置表格第一行顯示內容
$objPHP->getActiveSheet()
->setCellValue('A1', '圖片')
->setCellValue('B1', '商品名稱')
->setCellValue('C1', '商品簡稱')
->setCellValue('D1', '商品編號')
->setCellValue('E1','品牌')
->setCellValue('F1','年份')
->setCellValue('G1','季節')
->setCellValue('H1','大類')
->setCellValue('I1','品類')
->setCellValue('J1','子類')
->setCellValue('K1','層級')
->setCellValue('L1','單號')
->setCellValue('M1','銷售價')
->setCellValue('N1','上下架')
->setCellValue('O1','可銷數')
->setCellValue('P1','執行標準')
->setCellValue('Q1','添加時間');
/*以下就是對處理Excel里的數據,橫著取數據*/
foreach($data as $k=>$v){
$v['bojun_season']=$v['bojun_season']=='A'?'秋冬':'春夏';
$v['image'] = imgUrl($v['image']);
$num = $k + 2; //設置循環從第二行開始
if ($v['image']){
// 遠程圖片下載的本地臨時目錄
$local_pic_path = $this->download($v['image'],'../public/uploads/excelImg/');
$objDrawing = new \PHPExcel_Worksheet_Drawing(); /*實例化插入圖片類*/
$objDrawing->setPath(ROOT_PATH.'/public/uploads/excelImg/'. date('Ymd') .DS.$local_pic_path); /*設置圖片路徑 */
// 設置高度、寬度、指定格中的X、Y坐標值
$objDrawing->setHeight(80);
$objDrawing->setWidth(80);
$objDrawing->setOffsetX(5);
$objDrawing->setOffsetY(5);
$objDrawing->setCoordinates('A' . $num);
$objDrawing->setWorksheet($objPHP->getActiveSheet()); //把圖片寫到當前的表格中
}
//設置行寬度
$objActSheet->getColumnDimension('A')->setWidth(15);
$objActSheet->getColumnDimension('B')->setWidth(strlen(iconv('utf-8','gb2312',$v['goods_name'])));
$objActSheet->getColumnDimension('D')->setWidth(strlen(iconv('utf-8','gb2312',$v['sku_sn']))+3);
$objActSheet->getColumnDimension('P')->setWidth(strlen(iconv('utf-8','gb2312',$v['ex_standard']))+2);
$objActSheet->getColumnDimension('Q')->setWidth(strlen(iconv('utf-8','gb2312',$v['create_time'])));
$objActSheet->getRowDimension($num)->setRowHeight(80); // 設置單元格行高
$objPHP->getActiveSheet()
->setCellValue('B'.$num, $v['goods_name'])
->setCellValue('C'.$num, $v['abb_name'])
->setCellValue('D'.$num, $v['sku_sn'])
->setCellValue('E'.$num, $v['brand_name'])
->setCellValue('F'.$num, $v['bojun_year'])
->setCellValue('G'.$num, $v['bojun_season'])
->setCellValue('H'.$num, $v['bojun_dalei'])
->setCellValue('I'.$num, $v['bojun_pinlei'])
->setCellValue('J'.$num, $v['bojun_sonlei'])
->setCellValue('k'.$num, $v['bojun_cengji'])
->setCellValue('L'.$num, $v['bojun_oddnums'])
->setCellValue('M'.$num, $v['sell_price'])
->setCellValue('N'.$num, $v['status_desc'])
->setCellValue('O'.$num, $v['sum_saleable_qty'])
->setCellValue('P'.$num, $v['ex_standard'])
->setCellValue('Q'.$num, $v['create_time']);
}
ob_end_clean(); //清除緩沖區,避免亂碼
header('Content-Type: application/vnd.ms-excel');
header('Cache-Control: max-age=0');
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-Transfer-Encoding:binary");
header('Content-Type: text/html; charset=utf-8'); //編碼
$excel = \PHPExcel_IOFactory::createWriter($objPHP, 'Excel2007'); //Excel2007 兼容好
$excel->save('php://output');
exit;
}
~~~
```
//下載遠程圖片 到指定目錄
private function download($file_url, $path)
{
$basepath = $path . date('Ymd');
$dir_path = $basepath;
if (!is_dir($dir_path)) {
mkdir($dir_path, 0777, true);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $file_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何證書
$file = curl_exec($ch);
curl_close($ch);
$filename = pathinfo($file_url, PATHINFO_BASENAME);
$resource = fopen($basepath .'/'. $filename, 'a');
fwrite($resource, $file);
fclose($resource);
return $filename;
}
```
總結
以上是生活随笔為你收集整理的php导出照片,TP5导出excel图片和数据--先下载图片到本地服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: redhat搭建php环境,rhel5搭
- 下一篇: php导出excel表格需要隐藏行,ph