首頁  >  文章  >  後端開發  >  thinkPHP+phpexcel實作excel報表輸出功能實例詳解

thinkPHP+phpexcel實作excel報表輸出功能實例詳解

怪我咯
怪我咯原創
2017-06-16 10:50:081750瀏覽

這篇文章主要介紹了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報表開啟後初始的sheet    ⑨。     ⑩呼叫建立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