thinkphp3.2 phpexcel导出带生成图片 phpexcel导入到数据库
首先下载PHPEXCEL 下载地址 http://phpexcel.codeplex.com/ https://github.com/PHPOffice/PHPExcel
百度网盘:https://pan.baidu.com/s/1o8vAKKq
把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); //合并单元格 $objPHPExcel->getActiveSheet()->mergeCells('A1:I1'); $objPHPExcel->getActiveSheet()->getStyle('A:I')->getFont()->setSize(10); //字体大小 $objActSheet->setCellValue('A1', '商品报价表'); $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18)->setBold(true)->setName('Verdana')->getColor()->setRGB('f96c67'); //第一行设置字体大小,加粗字体 颜色 $objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getFont()->setBold(true); //多列加粗 $objActSheet->setCellValue('A2', '商品货号'); $objActSheet->setCellValue('B2', '商品名称'); $objActSheet->setCellValue('C2', '商品图'); $objActSheet->setCellValue('D2', '商品条码'); $objActSheet->setCellValue('E2', '商品属性'); $objActSheet->setCellValue('F2', '报价(港币)'); // 设置个表格宽度 $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:F')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $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 +=3; $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[$k]->setPath('./Upload/'.$img['goods_thumb']); 图片位置:安装目录/Upload/xxx
下面则是导入EXCEL文件并保存至数据库的方法
HTML:
<form action="{:U('Admin/price/studentImportExcel')}" method="post" enctype="multipart/form-data"> <div class="fileExcel"><span>导入excel:</span><input type="file" name="excel" id="fileField" value="" /> <input class="btn btn-default pull-left" type="submit" name="button" value="导入excel" /> </div> </form>
PHP:
<?php public function studentImportExcel(){ if (!empty ( $_FILES)){ $upload = new \Think\Upload(); // 实例化上传类 $upload->maxSize = 10485760 ; // 设置附件上传大小 $upload->exts = array('xls','xlsx'); // 设置附件上传类型 $upload->rootPath = './Public/Excel/'; // 设置附件上传根目录 $upload->autoSub = false; // 将自动生成以photo后面加时间的形式文件夹,关闭 // 上传文件 $info = $upload->upload(); // 上传文件 $exts = $info['excel']['ext']; // 获取文件后缀 $filename = $upload->rootPath.$info['excel']['savename']; // 生成文件路径名 if(!$info) { // 上传错误提示错误信息 $this->error($upload->getError()); }else{ // 上传成功 import("Org.Util.PHPExcel"); // 导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入 $PHPExcel = new \PHPExcel(); // 创建PHPExcel对象,注意,不能少了\ if ($exts == 'xls') { // 如果excel文件后缀名为.xls,导入这个类 import("Org.Util.PHPExcel.Reader.Excel5"); $PHPReader = new \PHPExcel_Reader_Excel5(); } else if ($exts == 'xlsx') { import("Org.Util.PHPExcel.Reader.Excel2007"); $PHPReader = new \PHPExcel_Reader_Excel2007(); } $PHPExcel = $PHPReader->load($filename); // 载入文件 $currentSheet = $PHPExcel->getSheet(0); // 获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推 $allColumn = $currentSheet->getHighestColumn(); // 获取总列数 $allRow = $currentSheet->getHighestRow(); // 获取总行数 for ($currentRow = 0; $currentRow <= $allRow; $currentRow ++) {// 循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始 for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn ++) {// 从哪列开始,A表示第一列 $address = $currentColumn . $currentRow; // 数据坐标 $ExlData[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();// 读取到的数据,保存到数组$arr中 } } // 读数组并写入数据库操作 for($i = 2,$j=0;$i<sizeof($ExlData);$i++,$j++){ import("Org.Util.PHPExcel.Shared.Date"); // 获取excel A的文本 这里需要转成富文本 重要 $username = $ExlData[$i]['C']; if (is_object($username)) { $username = $username->__toString(); } $data[] = array( 'username'=>$username, 'pwd' =>MD5(11), 'realname'=>$ExlData[$i]['B'], 'stunum' =>$ExlData[$i]['C'], 'email' =>$ExlData[$i]['D'], 'phone' =>$ExlData[$i]['E'], 'photo' =>$Hint->randPhoto(), 'time' =>gmdate("Y-m-d H:i:s", \PHPExcel_Shared_Date::ExcelToPHP($ExlData[$i]['F']));// 表格中的excel 时间转换 'role' =>'1', 'intro' =>'这家伙很懒什么都没留下', 'regdate' =>date('Y-m-d H:i:s', time()) // 写入注册时间 ); } $users = M('users'); // 生成数据库对象 $result = $users->addAll($data); // 批量写入数据库 if ($result) { // 验证 $this->success("导入成功", "Admin/Users/index");// 跳转页面 } else { $this->error("导入失败。或表格格式错误");// 提示错误 } } }else { $this->display(); } }