"htmlcode">

composer require "phpoffice/phpexcel": "*"

如果没有安装conposer可以参考下面1.2步

1、引入PHPExcel

首先得要下载phpexcel地址:https://github.com/PHPOffice/PHPExcel/archive/1.8.1.zip

你可以直接在入口文件index.php中引入,也可以在你定义的controller类之前,只要是在你使用之前引入就可以

require dirname(dirname(__FILE__)).'/excel/PHPExcel.php';

或者在phpexcel类里修改相应的namespace也可。

2、按照下面的代码修改PHPExcel代码目录里的Autoloader.php文件,对比源文件改成:

public static function Register() {
  $functions = spl_autoload_functions();
  foreach ( $functions as $function)
    spl_autoload_unregister($function);
    $functions = array_merge(array(array('PHPExcel_Autoloader','Load')),$functions);
  foreach ( $functions as $function)
    $x = spl_autoload_register($function);
    return $x;
}

上面的函数中,注释掉的是原有的代码。

3、下面的代码是输出Excel,以及一些常用的属性设置,在controller中:

public function actionExport()
{
    $objectPHPExcel = new PHPExcel();
    $objectPHPExcel->setActiveSheetIndex(0);
  
    $page_size = 52;
    $model = new NewsSearch();
    $dataProvider = $model->search();
    $dataProvider->setPagination(false);
    $data = $dataProvider->getData();
    $count = $dataProvider->getTotalItemCount();
    $page_count = (int)($count/$page_size) +1;
    $current_page = 0;
    $n = 0;
    foreach ( $data as $product )
    {
      if ( $n % $page_size === 0 )
      {
        $current_page = $current_page +1;
  
        //报表头的输出
        $objectPHPExcel->getActiveSheet()->mergeCells('B1:G1');
        $objectPHPExcel->getActiveSheet()->setCellValue('B1','产品信息表');
  
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2','产品信息表');
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2','产品信息表');
        $objectPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getFont()->setSize(24);
        $objectPHPExcel->setActiveSheetIndex(0)->getStyle('B1')
          ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2','日期:'.date("Y年m月j日"));
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('G2','第'.$current_page.'/'.$page_count.'页');
        $objectPHPExcel->setActiveSheetIndex(0)->getStyle('G2')
          ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
          
        //表格头的输出
        $objectPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B3','编号');
        $objectPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(6.5);
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('C3','名称');
        $objectPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(17);
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('D3','生产厂家');
        $objectPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(22);
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('E3','单位');
        $objectPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('F3','单价');
        $objectPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('G3','在库数');
        $objectPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
          
        //设置居中
        $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')
          ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  
        //设置边框
        $objectPHPExcel->getActiveSheet()->getStyle('B3:G3' )
          ->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
        $objectPHPExcel->getActiveSheet()->getStyle('B3:G3' )
          ->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
        $objectPHPExcel->getActiveSheet()->getStyle('B3:G3' )
          ->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
        $objectPHPExcel->getActiveSheet()->getStyle('B3:G3' )
          ->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
        $objectPHPExcel->getActiveSheet()->getStyle('B3:G3' )
          ->getBorders()->getVertical()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  
        //设置颜色
        $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')->getFill()
          ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF66CCCC');
          
      }
      //明细的输出
      $objectPHPExcel->getActiveSheet()->setCellValue('B'.($n+4) ,$product->id);
      $objectPHPExcel->getActiveSheet()->setCellValue('C'.($n+4) ,$product->product_name);
      $objectPHPExcel->getActiveSheet()->setCellValue('D'.($n+4) ,$product->product_agent->name);
      $objectPHPExcel->getActiveSheet()->setCellValue('E'.($n+4) ,$product->unit);
      $objectPHPExcel->getActiveSheet()->setCellValue('F'.($n+4) ,$product->unit_price);
      $objectPHPExcel->getActiveSheet()->setCellValue('G'.($n+4) ,$product->library_count);
      //设置边框
      $currentRowNum = $n+4;
      $objectPHPExcel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum )
          ->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
      $objectPHPExcel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum )
          ->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
      $objectPHPExcel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum )
          ->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
      $objectPHPExcel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum )
          ->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
      $objectPHPExcel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum )
          ->getBorders()->getVertical()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
      $n = $n +1;  
    }
  
    //设置分页显示
    //$objectPHPExcel->getActiveSheet()->setBreak( 'I55' , PHPExcel_Worksheet::BREAK_ROW );
    //$objectPHPExcel->getActiveSheet()->setBreak( 'I10' , PHPExcel_Worksheet::BREAK_COLUMN );
    $objectPHPExcel->getActiveSheet()->getPageSetup()->setHorizontalCentered(true);
    $objectPHPExcel->getActiveSheet()->getPageSetup()->setVerticalCentered(false);
  
  
    ob_end_clean();
    ob_start();
  
    header('Content-Type : application/vnd.ms-excel');
    header('Content-Disposition:attachment;filename="'.'产品信息表-'.date("Y年m月j日").'.xls"');
    $objWriter= PHPExcel_IOFactory::createWriter($objectPHPExcel,'Excel5');
    $objWriter->save('php://output');

代码执行后,会直接生成Excel,并提示下载或打开。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?