phpExcel

1   include 'PHPExcel.php'

  include 'PHPExcel/Writer/Excel2007.php';

2   实例化phpexcel对象:$objPHPExcel = new PHPExcel();

  $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

3 设置表格的参数

  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-Disposition:attachment;filename="resume.xls"');//设置名字
  header("Content-Transfer-Encoding:binary");

4 设置表格sheet页

  $objPHPExcel->setActiveSheetIndex(0);
  $objPHPExcel->getActiveSheet()->setTitle('Simple');

5 设置第一行标题

  $objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
  $objPHPExcel->getActiveSheet()->setCellValue('B1', 'name');
  $objPHPExcel->getActiveSheet()->setCellValue('C1', 'TEL');
  $objPHPExcel->getActiveSheet()->setCellValue('D1', '提交时间');
  $objPHPExcel->getActiveSheet()->setCellValue('E1', '来源');

  宽度

  $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
  $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(12);
  $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
  $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(12);
  $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
  $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(12);
  $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
  $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
  $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
  $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(12);

  颜色

  $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
  $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FF808080');
  $objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
  $objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->getStartColor()->setARGB('FF808080');
  $objPHPExcel->getActiveSheet()->getStyle('C1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
  $objPHPExcel->getActiveSheet()->getStyle('C1')->getFill()->getStartColor()->setARGB('FF808080');
  $objPHPExcel->getActiveSheet()->getStyle('D1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
  $objPHPExcel->getActiveSheet()->getStyle('D1')->getFill()->getStartColor()->setARGB('FF808080');
  $objPHPExcel->getActiveSheet()->getStyle('E1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
  $objPHPExcel->getActiveSheet()->getStyle('E1')->getFill()->getStartColor()->setARGB('FF808080');

循环插入

  $num = 2;
  foreach($data as $u){
    $objPHPExcel->getActiveSheet()->setCellValue('A'.$num, $u['id']);
    $objPHPExcel->getActiveSheet()->setCellValue('B'.$num, $u['name']);
    $objPHPExcel->getActiveSheet()->setCellValue('C'.$num, $u['phone']);
    $objPHPExcel->getActiveSheet()->setCellValue('D'.$num, date('y-m-d H:i:s',$u['create_time']));
    $objPHPExcel->getActiveSheet()->setCellValue('E'.$num, $type);
    $num++;
  }

导出
$objWriter->save('php://output');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值