首页  >  文章  >  后端开发  >  thinkPHP+phpexcel实现excel报表输出功能实例详解

thinkPHP+phpexcel实现excel报表输出功能实例详解

怪我咯
怪我咯原创
2017-06-16 10:50:081758浏览

这篇文章主要介绍了thinkPHP+phpexcel实现excel报表输出功能,结合具体实例形式分析了thinkPHP整合PHPExcel实现针对Excel文件相关操作技巧,需要的朋友可以参考下

本文实例讲述了thinkPHP+phpexcel实现excel报表输出功能。分享给大家供大家参考,具体如下:

准备工作:

1.下载phpexcel1.7.6类包;

2.解压至TP框架的ThinkPHP\Vendor目录下,改类包文件夹名为PHPExcel176,目录结构如下图;

编写代码(以一个订单汇总数据为例):

  1. 创建数据库及表;
  2. 创建tp项目,配置项目的数据库连接,这些基本的就不说了;
  3. 在项目的Lib\Action下创建一个新的类文件ExportStatisticsAction.class.php,然后在  index方法中实现excel导出;
  4. 导出方法的步骤:
    ①查询数据
    ②导入phpexcel类库
    ③创建excel对象并设置excel对象的属性
    ④设置excel的行列样式(字体、高宽、颜色、边框、合并等)
    ⑤绘制报表表头
    ⑥将查询数据写入excel
    ⑦设置excel的sheet的名称
    ⑧设置excel报表打开后初始的sheet
    ⑨设置输出的excel的头参数及文件名
    ⑩调用创建excel的方法生成excel文件

代码如下:

<?php
/**
 * Created by lonm.shi.
 * Date: 2012-02-09
 * Time: 下午4:54
 * To change this template use File | Settings | File Templates.
 */
class ExportStatisticsAction extends Action {
  public function index(){
    $model= D("OrdersView");
    $OrdersData= $model->select(); //查询数据得到$OrdersData二维数组
    vendor("PHPExcel176.PHPExcel");
    // Create new PHPExcel object
    $objPHPExcel = new PHPExcel();
    // Set properties
    $objPHPExcel->getProperties()->setCreator("ctos")
      ->setLastModifiedBy("ctos")
      ->setTitle("Office 2007 XLSX Test Document")
      ->setSubject("Office 2007 XLSX Test Document")
      ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
      ->setKeywords("office 2007 openxml php")
      ->setCategory("Test result file");
    //set width
    $objPHPExcel->getActiveSheet()->getColumnDimension(&#39;A&#39;)->setWidth(8);
    $objPHPExcel->getActiveSheet()->getColumnDimension(&#39;B&#39;)->setWidth(10);
    $objPHPExcel->getActiveSheet()->getColumnDimension(&#39;C&#39;)->setWidth(25);
    $objPHPExcel->getActiveSheet()->getColumnDimension(&#39;D&#39;)->setWidth(12);
    $objPHPExcel->getActiveSheet()->getColumnDimension(&#39;E&#39;)->setWidth(50);
    $objPHPExcel->getActiveSheet()->getColumnDimension(&#39;F&#39;)->setWidth(10);
    $objPHPExcel->getActiveSheet()->getColumnDimension(&#39;G&#39;)->setWidth(12);
    $objPHPExcel->getActiveSheet()->getColumnDimension(&#39;H&#39;)->setWidth(12);
    $objPHPExcel->getActiveSheet()->getColumnDimension(&#39;I&#39;)->setWidth(12);
    $objPHPExcel->getActiveSheet()->getColumnDimension(&#39;J&#39;)->setWidth(30);
    //设置行高度
    $objPHPExcel->getActiveSheet()->getRowDimension(&#39;1&#39;)->setRowHeight(22);
    $objPHPExcel->getActiveSheet()->getRowDimension(&#39;2&#39;)->setRowHeight(20);
    //set font size bold
    $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
    $objPHPExcel->getActiveSheet()->getStyle(&#39;A2:J2&#39;)->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle(&#39;A2:J2&#39;)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle(&#39;A2:J2&#39;)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
    //设置水平居中
    $objPHPExcel->getActiveSheet()->getStyle(&#39;A1&#39;)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
    $objPHPExcel->getActiveSheet()->getStyle(&#39;A&#39;)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle(&#39;B&#39;)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle(&#39;D&#39;)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle(&#39;F&#39;)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle(&#39;G&#39;)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle(&#39;H&#39;)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle(&#39;I&#39;)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    //合并cell
    $objPHPExcel->getActiveSheet()->mergeCells(&#39;A1:J1&#39;);
    // set table header content
    $objPHPExcel->setActiveSheetIndex(0)
      ->setCellValue(&#39;A1&#39;, &#39;订单数据汇总 时间:&#39;.date(&#39;Y-m-d H:i:s&#39;))
      ->setCellValue(&#39;A2&#39;, &#39;订单ID&#39;)
      ->setCellValue(&#39;B2&#39;, &#39;下单人&#39;)
      ->setCellValue(&#39;C2&#39;, &#39;客户名称&#39;)
      ->setCellValue(&#39;D2&#39;, &#39;下单时间&#39;)
      ->setCellValue(&#39;E2&#39;, &#39;需求机型&#39;)
      ->setCellValue(&#39;F2&#39;, &#39;需求数量&#39;)
      ->setCellValue(&#39;G2&#39;, &#39;需求交期&#39;)
      ->setCellValue(&#39;H2&#39;, &#39;确认BOM料号&#39;)
      ->setCellValue(&#39;I2&#39;, &#39;PMC确认交期&#39;)
      ->setCellValue(&#39;J2&#39;, &#39;PMC交货备注&#39;);
    // Miscellaneous glyphs, UTF-8
    for($i=0;$i<count($OrdersData)-1;$i++){
      $objPHPExcel->getActiveSheet(0)->setCellValue(&#39;A&#39;.($i+3), $OrdersData[$i][&#39;id&#39;]);
      $objPHPExcel->getActiveSheet(0)->setCellValue(&#39;B&#39;.($i+3), $OrdersData[$i][&#39;realname&#39;]);
      $objPHPExcel->getActiveSheet(0)->setCellValue(&#39;C&#39;.($i+3), $OrdersData[$i][&#39;customer_name&#39;]);
      $objPHPExcel->getActiveSheet(0)->setCellValue(&#39;D&#39;.($i+3), toDate($OrdersData[$i][&#39;create_time&#39;])); //这里调用了common.php的时间戳转换函数
      $objPHPExcel->getActiveSheet(0)->setCellValue(&#39;E&#39;.($i+3), $OrdersData[$i][&#39;require_product&#39;]);
      $objPHPExcel->getActiveSheet(0)->setCellValue(&#39;F&#39;.($i+3), $OrdersData[$i][&#39;require_count&#39;]);
      $objPHPExcel->getActiveSheet(0)->setCellValue(&#39;G&#39;.($i+3), $OrdersData[$i][&#39;require_time&#39;]);
      $objPHPExcel->getActiveSheet(0)->setCellValue(&#39;H&#39;.($i+3), $OrdersData[$i][&#39;product_bom_encoding&#39;]);
      $objPHPExcel->getActiveSheet(0)->setCellValue(&#39;I&#39;.($i+3), $OrdersData[$i][&#39;delivery_time&#39;]);
      $objPHPExcel->getActiveSheet(0)->setCellValue(&#39;J&#39;.($i+3), $OrdersData[$i][&#39;delivery_memo&#39;]);
      $objPHPExcel->getActiveSheet()->getStyle(&#39;A&#39;.($i+3).&#39;:J&#39;.($i+3))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
      $objPHPExcel->getActiveSheet()->getStyle(&#39;A&#39;.($i+3).&#39;:J&#39;.($i+3))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
      $objPHPExcel->getActiveSheet()->getRowDimension($i+3)->setRowHeight(16);
    }
    // sheet命名
    $objPHPExcel->getActiveSheet()->setTitle(&#39;订单汇总表&#39;);
    // Set active sheet index to the first sheet, so Excel opens this as the first sheet
    $objPHPExcel->setActiveSheetIndex(0);
    // excel头参数
    header(&#39;Content-Type: application/vnd.ms-excel&#39;);
    header(&#39;Content-Disposition: attachment;filename="订单汇总表(&#39;.date(&#39;Ymd-His&#39;).&#39;).xls"&#39;); //日期为文件名后缀
    header(&#39;Cache-Control: max-age=0&#39;);
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, &#39;Excel5&#39;); //excel5为xls格式,excel2007为xlsx格式
    $objWriter->save(&#39;php://output&#39;);
  }
}

5.调用导出方法直接  http://项目/index.php/ExportStatistics/index,项目中调用直接__APP__/ExportStatistics/index,生成的报表是下载方式来保存。phpexcel1.7.6没有发现什么编码问题,速度也很快,注意导出的方法中不能有任何页面输出信息或调试信息,否则导出的excel会提示格式不对。效果如下:

导出报表

以上是thinkPHP+phpexcel实现excel报表输出功能实例详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn