thinkphp中如何使用phpspreadsheet插件


thinkphp中如何使用phpspreadsheet插件

一、總結

一句話總結:多百度,百度什么都有

 

1、thinkphp中用composer安裝的插件的命名空間是什么?

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

 

2、插件的代碼實例哪里找?

插件自帶代碼實例

\vendor\phpoffice\phpspreadsheet\samples\里面有例程,仔細看下

 

3、在thinkphp中安裝好了插件,我們需要在入口文件中require包么?

不需要,thinkphp已經給我們做了

 

注意:不需要自己在入口文件中再require包了

第十八行代碼不需要

 1 <?php  2 // +----------------------------------------------------------------------  3 // | ThinkPHP [ WE CAN DO IT JUST THINK ]  4 // +----------------------------------------------------------------------  5 // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.  6 // +----------------------------------------------------------------------  7 // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )  8 // +----------------------------------------------------------------------  9 // | Author: liu21st <liu21st@gmail.com> 10 // +---------------------------------------------------------------------- 11 12 // [ 應用入口文件 ] 13 14 // 定義應用目錄 15 define('APP_PATH', __DIR__ . '/../application/'); 16 // 加載框架引導文件 17 require __DIR__ . '/../thinkphp/start.php'; 18 //require __DIR__ . '/../vendor/autoload.php';

 

4、我們要調用自己安裝的插件的類,use的格式是怎樣的?

其實use就是永遠和文件的命名空間是相對應的

直接看需要文件定義的命名空間:比如說下面第一個文件Spreadsheet的命名空間就是這個:namespace PhpOffice\PhpSpreadsheet;

 6 use PhpOffice\PhpSpreadsheet\Spreadsheet;  7 use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

 

 

二、TP5使用Composer安裝PhpSpreadsheet類庫實現導出Excel表並封裝

一、背景介紹:

PhpSpreadsheet是PHPExcel的下一個版本。它打破了兼容性,大大提高了代碼庫質量(命名空間,PSR合規性,最新PHP語言功能的使用等)。

由於所有努力都轉移到了PhpSpreadsheet,因此將不再維護PHPExcel。PHPExcel,補丁和新功能的所有貢獻都應該針對PhpSpreadsheet開發分支。

前提:TP5項目中已經安裝配置好Composer 管理工具包。

 

二、Composer 中文網 / Packagist 中國全量鏡像https://www.phpcomposer.com/ 打開安裝包列表搜索phpspreadsheet

資源鏈接:https://packagist.org/packages/phpoffice/phpspreadsheet 復制命令composer require phpoffice/phpspreadsheet,

在開發工具(比如:PHPSTORM)命令行(terminal)中執行;

 

三、創建Office類文件並封裝,在需要導出Excel表的地方引入即可:

<?php

namespace app\index\controller;


use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Office
{

    /**
     * 導出excel表
     * $data:要導出excel表的數據,接受一個二維數組
     * $name:excel表的表名
     * $head:excel表的表頭,接受一個一維數組
     * $key:$data中對應表頭的鍵的數組,接受一個一維數組
     * 備注:此函數缺點是,表頭(對應列數)不能超過26;
     *循環不夠靈活,一個單元格中不方便存放兩個數據庫字段的值
     */
    public function outdata($name='測試表', $data=[], $head=[], $keys=[])
    {
        $count = count($head);  //計算表頭數量

        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        for ($i = 65; $i < $count + 65; $i++) {     //數字轉字母從65開始,循環設置表頭:
            $sheet->setCellValue(strtoupper(chr($i)) . '1', $head[$i - 65]);
        }

        /*--------------開始從數據庫提取信息插入Excel表中------------------*/


        foreach ($data as $key => $item) {             //循環設置單元格:
            //$key+2,因為第一行是表頭,所以寫到表格時   從第二行開始寫

            for ($i = 65; $i < $count + 65; $i++) {     //數字轉字母從65開始:
                $sheet->setCellValue(strtoupper(chr($i)) . ($key + 2), $item[$keys[$i - 65]]);
                $spreadsheet->getActiveSheet()->getColumnDimension(strtoupper(chr($i)))->setWidth(20); //固定列寬
            }

        }

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $name . '.xlsx"');
        header('Cache-Control: max-age=0');
        $writer = new Xlsx($spreadsheet);
        $writer->save('php://output');

        //刪除清空:
        $spreadsheet->disconnectWorksheets();
        unset($spreadsheet);
        exit;
    }

調用示例:

 
$excel = new Office();

//設置表頭:
 $head = ['訂單編號', '商品總數', '收貨人', '聯系電話', '收貨地址'];

//數據中對應的字段,用於讀取相應數據:
$keys = ['order_sn', 'num', 'consignee', 'phone', 'detail'];            

$excel->outdata('訂單表', $orders, $head, $keys);


 參考:TP5使用Composer安裝PhpSpreadsheet類庫實現導出Excel表並封裝 - 逍逍逍遙虎的博客 - CSDN博客
https://blog.csdn.net/qq_41962562/article/details/82315608

 

 

三、TP5,用composer加載了phpSpreadSheet后怎么用呢?

\vendor\phpoffice\phpspreadsheet\samples\里面有例程,仔細看下

 

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

 

 1 use PhpOffice\PhpSpreadsheet\IOFactory;
 2 $path = ROOT_PATH . "public/" . $filename;
 3 setlocale(LC_ALL, 'zh_CN');  //csv中文亂碼
 4 $inputFileType = IOFactory::identify($path);
 5 $excelReader = IOFactory::createReader($inputFileType);
 6 if ($inputFileType == 'Csv') {   //csv文件讀取設置
 7   $excelReader->setInputEncoding('GBK');
 8   $excelReader->setDelimiter(',');
 9 }
10 $phpexcel = $excelReader->load($path);
11 $activeSheet = $phpexcel->getActiveSheet();
12 $sheet = $activeSheet->toArray();

 

上面一個是讀取,寫出的代碼就不貼了,找下phpexcel的改下命名空間就差不多了,
 
1 use PhpOffice\PhpSpreadsheet\Spreadsheet;
2 use PhpOffice\PhpSpreadsheet\IOFactory;

 

四、自己測試實例(親測通過)

比如我們現在在thinkphp5中要用phpspreadsheet插件

 

第一步:找到插件官網的composer安裝方法

Installation

Use composer to install PhpSpreadsheet into your project:

composer require phpoffice/phpspreadsheet 

Note: If you want the unreleased, unstable development version use phpoffice/phpspreadsheet:dev-develop instead.

 

第二步:在我們tp5系統的更目錄下打開cmd命令行,運行這句話(要先在電腦上面裝composer)

 

第三步:直接use包運行實例,不需要引(require)包(thinkphp已經幫我們做了,再引會造成一些方法重復定義)

這樣直接就用了

 1 <?php
 2 namespace app\admin\controller\test\phpspreadsheet;
 3 
 4 use app\admin\controller\Base;
 5 
 6 use PhpOffice\PhpSpreadsheet\Spreadsheet;
 7 use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
 8 
 9 class Index extends Base
10 {
11     public function index()
12     {
13         $spreadsheet = new Spreadsheet();
14         $sheet = $spreadsheet->getActiveSheet();
15         $sheet->setCellValue('A1', 'Hello World !');
16 
17         $writer = new Xlsx($spreadsheet);
18         $writer->save('d://hello world.xlsx');
19         //dump('2222222222');die;
20         //return view();
21     }
22 }

 

注意:不需要自己在入口文件中再require包了

第十八行代碼不需要

 1 <?php
 2 // +----------------------------------------------------------------------
 3 // | ThinkPHP [ WE CAN DO IT JUST THINK ]
 4 // +----------------------------------------------------------------------
 5 // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
 6 // +----------------------------------------------------------------------
 7 // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 8 // +----------------------------------------------------------------------
 9 // | Author: liu21st <liu21st@gmail.com>
10 // +----------------------------------------------------------------------
11 
12 // [ 應用入口文件 ]
13 
14 // 定義應用目錄
15 define('APP_PATH', __DIR__ . '/../application/');
16 // 加載框架引導文件
17 require __DIR__ . '/../thinkphp/start.php';
18 //require __DIR__ . '/../vendor/autoload.php';

 

 

 

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM