使用phpoffice/phpspreadsheet,導入導出數據


1、composer安裝:

composer require phpoffice/phpspreadsheet

 

2、導出類Export,放在服務層:

<?php

namespace app\backend\service;

use \PhpOffice\PhpSpreadsheet\Spreadsheet;
use \PhpOffice\PhpSpreadsheet\IOFactory;//use \PHPExcel_Style_NumberFormat;    //設置列的格式==>>設置文本格式

class Export
{
    public function exportAdmin($startTime, $endTime)
    {
        $adminList = db('admin')
            ->where('create_time', 'between', [$startTime, $endTime])
            ->order(['admin_id' => 'desc'])
            ->select();
        //return $adminList;

        $newExcel = new Spreadsheet();  //創建一個新的excel文檔
        $objSheet = $newExcel->getActiveSheet();  //獲取當前操作sheet的對象
        $objSheet->setTitle('管理員表');  //設置當前sheet的標題

        //設置寬度為true,不然太窄了
        $newExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
        $newExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $newExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $newExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);

        //設置第一欄的標題
        $objSheet->setCellValue('A1', 'id')
            ->setCellValue('B1', '用戶名')
            ->setCellValue('C1', '密碼')
            ->setCellValue('D1', '時間');

        //第二行起,每一行的值,setCellValueExplicit是用來導出文本格式的。
        //->setCellValueExplicit('C' . $k, $val['admin_password']PHPExcel_Cell_DataType::TYPE_STRING),可以用來導出數字不變格式
        foreach ($adminList as $k => $val) {
            $k = $k + 2;
            $objSheet->setCellValue('A' . $k, $val['admin_id'])
                ->setCellValue('B' . $k, $val['admin_username'])
                ->setCellValue('C' . $k, $val['admin_password'])
                ->setCellValue('D' . $k, date('Y-m-d H:i:s', $val['create_time']));
        }

        $this->downloadExcel($newExcel, '管理員表', 'Xls');
    }

    //公共文件,用來傳入xls並下載
    function downloadExcel($newExcel, $filename, $format)
    {
        // $format只能為 Xlsx 或 Xls
        if ($format == 'Xlsx') {
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        } elseif ($format == 'Xls') {
            header('Content-Type: application/vnd.ms-excel');
        }

        header("Content-Disposition: attachment;filename="
            . $filename . date('Y-m-d') . '.' . strtolower($format));
        header('Cache-Control: max-age=0');
        $objWriter = IOFactory::createWriter($newExcel, $format);

        $objWriter->save('php://output');

        //通過php保存在本地的時候需要用到
        //$objWriter->save($dir.'/demo.xlsx');

        //以下為需要用到IE時候設置
        // If you're serving to IE 9, then the following may be needed
        //header('Cache-Control: max-age=1');
        // If you're serving to IE over SSL, then the following may be needed
        //header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        //header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        //header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        //header('Pragma: public'); // HTTP/1.0
        exit;
    }
}

 

 

3、導入類Import,放在服務層:

<?php

namespace app\backend\service;

use think\Controller;
use \PhpOffice\PhpSpreadsheet\IOFactory;

class Import extends Controller
{
    public function importAdmin()
    {
        //獲取表格的大小,限制上傳表格的大小5M
        $file_size = $_FILES['myfile']['size'];
        if ($file_size > 5 * 1024 * 1024) {
            $this->error('文件大小不能超過5M');
            exit();
        }

        //限制上傳表格類型
        $fileExtendName = substr(strrchr($_FILES['myfile']["name"], '.'), 1);
        //application/vnd.ms-excel  為xls文件類型
        if ($fileExtendName != 'xls') {
            $this->error('必須為excel表格,且必須為xls格式!');
            exit();
        }

        if (is_uploaded_file($_FILES['myfile']['tmp_name'])) {
            // 有Xls和Xlsx格式兩種
            $objReader = IOFactory::createReader('Xls');

            $filename = $_FILES['myfile']['tmp_name'];
            $objPHPExcel = $objReader->load($filename);  //$filename可以是上傳的表格,或者是指定的表格
            $sheet = $objPHPExcel->getSheet(0);   //excel中的第一張sheet
            $highestRow = $sheet->getHighestRow();       // 取得總行數
            // $highestColumn = $sheet->getHighestColumn();   // 取得總列數

            //定義$usersExits,循環表格的時候,找出已存在的用戶。
            $usersExits = [];
            //循環讀取excel表格,整合成數組。如果是不指定key的二維,就用$data[i][j]表示。
            for ($j = 2; $j <= $highestRow; $j++) {
                $data[$j - 2] = [
                    'admin_username' => $objPHPExcel->getActiveSheet()->getCell("A" . $j)->getValue(),
                    'admin_password' => $objPHPExcel->getActiveSheet()->getCell("B" . $j)->getValue(),
                    'create_time' => time()
                ];
                //看下用戶名是否存在。將存在的用戶名保存在數組里。
                $userExist = db('admin')->where('admin_username', $data[$j - 2]['admin_username'])->find();
                if ($userExist) {
                    array_push($usersExits, $data[$j - 2]['admin_username']);
                }
            }
            //halt($usersExits);

            //如果有已存在的用戶名,就不插入數據庫了。
            if ($usersExits != []) {
                //把數組變成字符串,向前端輸出。
                $c = implode(" / ", $usersExits);
                $this->error('Excel中以下用戶名已存在:' . $c, "/backend/admin/create", '', 20);
                exit();
            }

            //halt($data);
            //插入數據庫
            $res = db('admin')->insertAll($data);
            if ($res) {
                $this->success('上傳成功!', '/backend/admin', '', 1);
            }
        }
    }
}

 

——為什么需要繼承系統的Controller,因為用到了$this->seccess()之類的。

 


免責聲明!

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



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