TP5使用phpoffice phpexcel包操作excel(導出)


安裝composer(window版本)
安裝composer(MAC版本)
安裝composer(Linux版本)

在PhpStorm配置
導出excel

1、使用composer安裝phpoffice/phpexcel
phpexcel資源

下載好之后,解壓,再\php-excel\vendor\phpoffice目錄下,不建議使用,因為這是通過composer下載的 ,對其他(比如:composer.json等,安裝包也有這個,一套的)的文件有所關聯

composer require phpoffice/phpexcel

2、公共代碼(我放到API位置了,各自根據自己代碼結構編寫)

    <?php
    /**
     * 操作excel
     * 導入、導出
     * Created by PhpStorm.
     * Date: 2019/5/13
     * Time: 16:12
     */
    namespace app\api\controller;
     
    use think\Controller;
    use think\Request;
     
    class Excel extends Controller
    {
        /**
         * 導出
         * @param string $fileName
         * @param array $headArr
         * @param array $data
         * @throws \PHPExcel_Exception
         * @throws \PHPExcel_Reader_Exception
         * @throws \PHPExcel_Writer_Exception
         */
        function excelExport($fileName = '', $headArr = [], $data = []) {
     
            $fileName .= "-" . date("YmdHi", Request::instance()->time()) . ".xls";
     
            $objPHPExcel = new \PHPExcel();
     
            $objPHPExcel->getProperties();
     
            $key = ord("A"); // 設置表頭
     
            foreach ($headArr as $v) {
     
                $colum = chr($key);
     
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
     
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
     
                $key += 1;
     
            }
     
            $column = 2;
     
            $objActSheet = $objPHPExcel->getActiveSheet();
     
            foreach ($data as $key => $rows) { // 行寫入
     
                $span = ord("A");
     
                foreach ($rows as $keyName => $value) { // 列寫入
     
                    $objActSheet->setCellValue(chr($span) . $column, $value);
     
                    $span++;
     
                }
     
                $column++;
     
            }
     
            $fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表
     
            $objPHPExcel->setActiveSheetIndex(0); // 設置活動單指數到第一個表,所以Excel打開這是第一個表
     
            header('Content-Type: application/vnd.ms-excel');
     
            header("Content-Disposition: attachment;filename=$fileName");
     
            header('Cache-Control: max-age=0');
     
            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
     
            $objWriter->save('php://output'); // 文件通過瀏覽器下載
     
            exit();
     
        }
    }

3、控制層,處理數據調用

    <?php
    /**
     * 會員設置控制器
     * Created by PhpStorm.
     * Date: 2019/4/25
     * Time: 9:45
     */
     
    namespace app\admin\controller;
     
    use app\api\controller\Excel;
    use app\common\controller\Manage;
    use think\Request;
     
    class MemberShip extends Manage
    {
     
        private $memberShipM;
     
        /**
         * 預加載model
         * Category constructor.
         * @param Request|null $request
         */
        public function __construct(Request $request = null)
        {
            parent::__construct($request);
            $this->memberShipM = new \app\common\model\MemberShip();
        }
     
     
     
        /**
         * 導出會員列表
         */
        public function exportMember()
        {
            $excel = new Excel();
            $name='會員';
            $header=['序號','會員名稱','價格','購買人數'];
            $newdata = [];
            $data=$this->memberShipM->getList();
            foreach ($data as $key=>$value){
                $newdata[$key]['id'] = $value['id'];
                $newdata[$key]['name'] = $value['name'];
                $newdata[$key]['price'] = $value['price'];
                $newdata[$key]['buy_num'] = $value['buy_num'];
            }
     
            $excel->excelExport($name,$header,$newdata);
        }
     
    }

5、導出成功
————————————————
版權聲明:本文為CSDN博主「SUPER_童」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_40354683/article/details/90177141


免責聲明!

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



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