## 利用PHPExcel 实现excel数据的导入导出
首先需要载入phpexcel ,以下为参考代码。
```
//载入PHPExcel
cp::re('phpexcel');
//引入PHPExcel
$objPHPExcel = new PHPExcel();
```
### 导入
相比导出,导入就简单多了
利用form表单获取上传的excel文件
```
$file = $_FILES['file'];
if ($file['error'] == 4) $this->error('请选择上传excel文件');
$file_types = explode ( ".", $file['name'] );
$excel_type = array('xls','csv','xlsx');
if (!in_array(strtolower(end($file_types)),$excel_type)){
$this->error("不是Excel文件,请重新上传");
}
```
然后,读取excel文件里面的数据,这里是边读取边处理,或者全部读取出来再处理
```
//设置获取excel对象
$objReader = \PHPExcel_IOFactory::createReader('Excel5');//配置成2003版本,因为office版本可以向下兼容
$objPHPExcel = $objReader->load($file['tmp_name'],$encode='utf-8');//$file 为解读的excel文件
//dump($objPHPExcel);die;
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$success_item = $fail_item = 0;
//发货
$order_model = new OrderModel();
//开始读取数据
for($j=3;$j<=$highestRow;$j++)
{
$order_num = $objPHPExcel->getActiveSheet()->getCell('A'.$j)->getValue();
$poster = $objPHPExcel->getActiveSheet()->getCell("AB".$j)->getValue();//物流公司
$logistics = $objPHPExcel->getActiveSheet()->getCell("AC".$j)->getValue();//物流单号
$comm = $objPHPExcel->getActiveSheet()->getCell("V".$j)->getValue();//卖家备注
//判断条件
if(!is_null($order_num) && $order_num){
$res = $order_model->where('order_num',$order_num)->field('itemid,status')->find();
if ($res && $res['status'] == 2) {
if ((!is_null($poster) && $poster) || (!is_null($logistics) && $logistics)) {
//更改状态
OrderPlanModel::addLog($res['itemid'],'确认发货',$comm);
$order_model->Update([
'status'=>3,
'poster'=>$poster,
'logistics'=>$logistics,
],['itemid'=>$res['itemid']]);
$success_item ++;
} else {
$fail_item ++;
}
} else {
$fail_item ++;
}
} else {
$fail_item++;
}
}
$this->success('成功条数:'.$success_item.',失败条数:'.$fail_item);
```
### 导出
设置Excel导出文件的样式以及为每个单元格赋值,这一步是导出最重要最复杂的一步,好在我都做了注释
```
// 设置excel文档的属性
$objPHPExcel->getProperties()->setCreator("cyf")
->setLastModifiedBy("cyf Test")
->setTitle("order")
->setSubject("Test1")
->setDescription("Test2")
->setKeywords("Test3")
->setCategory("Test result file");
//设置excel工作表名及文件名
$title = '待发货订单';
$excel_filename = '待发货订单_'.date('Ymd_His');
// 操作第一个工作表
$objPHPExcel->setActiveSheetIndex(0);
//第一行设置内容
$objPHPExcel->getActiveSheet()->setCellValue('A1',$excel_filename);
//合并
$objPHPExcel->getActiveSheet()->mergeCells('A1:AC1');
//设置单元格内容加粗
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
//设置单元格内容水平居中
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//设置excel的表头
$sheet_title = array('订单编号','下单时间','订单金额','订单状态','支付方式','付款时间','支付单号','配送方式',
'收货人','联系电话','省','市','区','地址','运费','商品名称','商品货号','商品规格','商品数量','商品单价',
'发货单号','卖家备注','实付金额','微信/支付宝金额','余额支付','优惠金额','买家备注','物流公司','物流单号');
// 设置第一行和第一行的行高
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);
$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(25);
$letter = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T', 'U','V','W','X','Y','Z','AA','AB','AC');
//设置单元格
$objPHPExcel->getActiveSheet()->getStyle('A2:AC2')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
//首先是赋值表头
for ($k=0;$k<29;$k++) {
$objPHPExcel->getActiveSheet()->setCellValue($letter[$k].'2',$sheet_title[$k]);
$objPHPExcel->getActiveSheet()->getStyle($letter[$k].'2')->getFont()->setSize(10)->setBold(true);
//设置单元格内容水平居中
$objPHPExcel->getActiveSheet()->getStyle($letter[$k].'2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//设置每一列的宽度
$objPHPExcel->getActiveSheet()->getColumnDimension($letter[$k])->setWidth(18);
$objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(30);
}
//开始赋值
for ($i=0;$i<$count;$i++) {
//先确定行
$row = $i+3;//再确定列,最顶部占一行,表头占用一行,所以加3
$temp = $res[$i];
for ($j = 0;$j<29;$j++) {
//开始为每个单元格赋值
//初始化地址数据
$address_arr = [];
$address_arr = explode(',',$temp['address_address']);
//初始化商品数据
$product_name = $product_number = $product_attr = $product_num = $product_price = '';
$cl = ','.chr(10);
foreach ($temp['o_g_model'] as $v) {
$product_name .= $v['title'].$cl;
$product_number .= $v['attrid'].$cl;
$attr = [];
$attr = json_decode($v['attr_name'],true);
if ($attr) {
foreach ($attr as $vv) {
$product_attr .= $vv.' ';
}
$product_attr .= $cl;
}
$product_num .= $v['num'].$cl;
$product_price .= $v['user_price'].$cl;
}
switch ($j) {
case 0 :
//订单编号
$cellvalue = $temp['order_num'];
break;
case 1 :
//下单时间
$cellvalue = date('Y-m-d H:i:s',$temp['addtime']);
break;
case 2 :
//订单金额
$cellvalue = $temp['price_sum'];
break;
case 3 :
//订单状态
$cellvalue = lang('ORDER_STATUS')[$temp['status']];
break;
case 4 :
//支付方式
$cellvalue = lang('ORDER_PAY')[$temp['pay_type']];
break;
case 5 :
//付款时间
$cellvalue = '';
break;
case 6 :
//支付单号
$cellvalue = '';
break;
case 7 :
//配送方式
$cellvalue = lang('POST_TYPE')[$temp['post_type']];
break;
case 8 :
//收货人姓名
$cellvalue = $temp['address_name'];
break;
case 9 :
//联系电话
$cellvalue = $temp['address_phone'];
break;
case 10 :
//省
$cellvalue = $address_arr[0];
break;
case 11 :
//市
$cellvalue = $address_arr[1];
break;
case 12 :
//区
$cellvalue = $address_arr[2];
break;
case 13 :
//地址
$cellvalue = $address_arr[3];
break;
case 14 :
//运费
$cellvalue = $temp['p_price'];
break;
case 15 :
//商品名称
$cellvalue = $product_name;
break;
case 16 :
//商品货号
$cellvalue = $product_number;
break;
case 17 :
//商品规格
$cellvalue = $product_attr;
break;
case 18 :
//商品数量
$cellvalue = $product_num;
break;
case 19 :
//商品单价
$cellvalue = $product_price;
break;
case 20 :
//发货单号
$cellvalue = '已废弃';
break;
case 21 :
//卖家备注
$cellvalue = '';
break;
case 22 :
//实付金额
$cellvalue = $temp['pay_money'];
break;
case 23 :
//微信支付宝金额
$cellvalue = $temp['pay_money'];
break;
case 24 :
//余额支付
$cellvalue = $temp['static_money'];
break;
case 25 :
//优惠金额
$cellvalue = $temp['coupon'];
break;
case 26 :
//买家备注
$cellvalue = '';
break;
case 27 :
//物流公司
$cellvalue = '';
break;
case 28 :
//物流单号
$cellvalue = '';
break;
}
//赋值
$objPHPExcel->getActiveSheet()->setCellValue($letter[$j].$row, $cellvalue);
//设置字体大小
$objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getFont()->setSize(10);
//设置单元格内容水平居中
$objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//设置自动换行
if ((in_array($j,[15,16,17,18,19])) && "" != $cellvalue) {
$objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getAlignment()->setWrapText(true); // 自动换行
$objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); // 垂直方向上中间居中
}
}
// 设置行高
$objPHPExcel->getActiveSheet()->getRowDimension($row)->setRowHeight(21);
}
unset($res);
```
设置完之后,将生成的excel文件,通过浏览器下载到本地
```
//赋值结束,开始输出
$objPHPExcel->getActiveSheet()->setTitle($title);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$excel_filename.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
```
也可以在服务器上生成excel文件,然后发送到用户邮箱
```
$filename = __ROOT__.'/upload/files/'.$excel_filename;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save($filename);
```