Yii2框架中使用PHPExcel导出Excel文件的示例

前端之家收集整理的这篇文章主要介绍了Yii2框架中使用PHPExcel导出Excel文件的示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近在研究PHP的Yii框架,很喜欢,碰到导出Excel的问题,研究了一下,就有了下面的方法

最简单的利用composer安装

PHPoffice/PHPexcel": "*"

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

1、引入PHPExcel

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

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

PHP;"> require dirname(dirname(__FILE__)).'/excel/PHPExcel.PHP';

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

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

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中:

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;

    //报表头的<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->mergeCells('B1:G1');
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->setCellValue('B1','产品信息表');

    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->setCellValue('B2','产品信息表');
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->setCellValue('B2','产品信息表');
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->getStyle('B1')->getFont()->setSize(24);
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->getStyle('B1')
      ->getAlignment()->setHorizontal(<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Alignment::HORIZONTAL_CENTER);

    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->setCellValue('B2','日期:'.date("Y年m月j日"));
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->setCellValue('G2','第'.$current_page.'/'.$page_count.'页');
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->getStyle('G2')
      ->getAlignment()->setHorizontal(<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Alignment::HORIZONTAL_RIGHT);

    //表格头的<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->setCellValue('B3','编号');
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getColumnDimension('B')->setWidth(6.5);
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->setCellValue('C3','<a href="https://www.jb51.cc/tag/mingcheng/" target="_blank" class="keywords">名称</a>');
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getColumnDimension('C')->setWidth(17);
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->setCellValue('D3','生产厂家');
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getColumnDimension('D')->setWidth(22);
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->setCellValue('E3','单位');
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->setCellValue('F3','单价');
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0)->setCellValue('G3','在库数');
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getColumnDimension('G')->setWidth(15);

    //设置居中
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('B3:G3')
      ->getAlignment()->setHorizontal(<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Alignment::HORIZONTAL_CENTER);

    //设置边框
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('B3:G3' )
      ->getBorders()->getTop()->setBorderStyle(<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Border::BORDER_THIN);
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('B3:G3' )
      ->getBorders()->getLeft()->setBorderStyle(<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Border::BORDER_THIN);
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('B3:G3' )
      ->getBorders()->getRight()->setBorderStyle(<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Border::BORDER_THIN);
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('B3:G3' )
      ->getBorders()->getBottom()->setBorderStyle(<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Border::BORDER_THIN);
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('B3:G3' )
      ->getBorders()->getVertical()->setBorderStyle(<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Border::BORDER_THIN);

    //设置颜色
    $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('B3:G3')->getFill()
      ->setFillType(<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF66CCCC');

  }
  //明细的<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>
  $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->setCellValue('B'.($n+4),$product->id);
  $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->setCellValue('C'.($n+4),$product->product_name);
  $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->setCellValue('D'.($n+4),$product->product_agent->name);
  $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->setCellValue('E'.($n+4),$product->unit);
  $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->setCellValue('F'.($n+4),$product->unit_price);
  $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->setCellValue('G'.($n+4),$product->library_count);
  //设置边框
  $currentRowNum = $n+4;
  $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum )
      ->getBorders()->getTop()->setBorderStyle(<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Border::BORDER_THIN);
  $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum )
      ->getBorders()->getLeft()->setBorderStyle(<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Border::BORDER_THIN);
  $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum )
      ->getBorders()->getRight()->setBorderStyle(<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Border::BORDER_THIN);
  $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum )
      ->getBorders()->getBottom()->setBorderStyle(<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Border::BORDER_THIN);
  $object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum )
      ->getBorders()->getVertical()->setBorderStyle(<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Border::BORDER_THIN);
  $n = $n +1;  
}

//设置<a href="https://www.jb51.cc/tag/fenyexianshi/" target="_blank" class="keywords">分页显示</a>
//$object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->setBreak( 'I55',<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Worksheet::BREAK_ROW );
//$object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->setBreak( 'I10',<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Worksheet::BREAK_COLUMN );
$object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getPageSetup()->setHorizontalCentered(true);
$object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->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= <a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_IOFactory::createWriter($object<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel,'Excel5');
$objWriter->save('<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>://output');

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

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

原文链接:https://www.f2er.com/php/17006.html

猜你在找的PHP相关文章