Hướng dẫn class phpexcel not found yii2 - class phpexcel không tìm thấy yii2

Tôi muốn đọc bản ghi excel trong yii2, vì điều đó tôi tìm kiếm một phần mở rộng và cuối cùng tôi đã nhận được phần mở rộng sau

Tiện ích mở rộng: //www.yiiframework.com/extension/yii2-phpexcel/#add-comment

Sau khi cài đặt thành công thông qua trình soạn thảo, tôi đặt mã

namespace app\controllers;
use app\models\TempCdr;
use app\models\User;
use yii\web\UploadedFile;
use yii;



class BatchController extends \yii\web\Controller
{

    public  $user_model;
    public  $allowed_file_extension = array['xls','xlsx'];

    public function init[] {
        $fileName = 'assets/CdrTmp/cpyCdr.xls';
        $data = \moonland\phpexcel\Excel::import[$fileName];
    }


 }

Trong bộ điều khiển chỉ mục của tôi, nhưng nó không tìm thấy lớp lỗi 'phpExcel_iofactory'.

Cấu trúc thư mục của tôi sau khi cài đặt tiện ích mở rộng

moonlandsoft
 Excel.php
 composer.json

phpoffice
 phpexcel
   Documentation
   Examples
   src

Cập nhật JSON

 "phpoffice/phpexcel": "^1.8",
 "moonlandsoft/yii2-phpexcel": "*"

Tôi có thể biết vấn đề về điều đó là gì, cảm ơn trước cho ý tưởng và đề xuất của bạn.

  • Hướng dẫn
  • API
  • Wiki
  • Diễn đàn
  • Cộng đồng
    • Trò chuyện trực tiếp
    • Phần mở rộng
    • Tài nguyên
    • Các thành viên
    • Hội trường danh vọng
    • danh hiệu
  • Hơn
    • Học
    • Sách
    • Tài nguyên
    • Các thành viên
    • Hội trường danh vọng
    • danh hiệu
    • Hơn
    • Học
    • Sách
    • Phát triển, xây dựng
    • Tải xuống yii
    • Báo cáo một vấn đề
    • Báo cáo vấn đề bảo mật
    • Đóng góp cho yii
    • Về

  • Nhà
  • / /
  • Alexgx
  • / /
  • yii2-phpexcel
  • / /
  • PhpExcel.php
  • / /

Alexgx

Tệp này là một phần của gói AlexGX/YII2-PhpExcel. Vui lòng truy cập trang tải xuống của chúng tôi để tải xuống gói nhà soạn nhạc này và để giải quyết vấn đề 'phpexcel'-flavel' không


查找了网上的资料,说是没有composer.json与composer.lock这两个文件导致的,但是我项目中也是有的:

请教了大佬,说是好像是自动加载类没这个类啥的,但是具体问题我还不知道什么原因,希望有大佬知道的指点一二。
目前我的解决办法是使用require_once引入PHPExcel.php文件

require_once[CORE_PATH  . '/vendor/phpoffice/phpexcel/Classes/PHPExcel.php'];

Sau khi giới thiệu, bạn có thể truy cập nó bình thường.

Xuất dữ liệu hoàn chỉnh Mã nguồn:

 /**
     * 导出
     * @param  array    $titles         标题,一维数组,可传map或单纯标题
     * @param  array    $dataArray      数据,二维数组,可传map或单纯数据
     * @param  string   $filename       文件名,要带后缀
     * @param  string   $bigTitle       居中加粗的大标题,默认为空
     * @param  array    $extra          扩展数据
     * @return file
     */
    public static function export[array $titles, $dataArray, $filename, $bigTitle='', $extra=[]]
    {
        require_once[CORE_PATH  . '/vendor/phpoffice/phpexcel/Classes/PHPExcel.php'];
        set_time_limit[0];
        ini_set['memory_limit', '512M'];

        // 后缀
        $suffix = substr[$filename, strrpos[$filename, '.']];
        empty[$titles] && die['标题数组不能为空!'];
        empty[$dataArray] && die['数据数组不能为空!'];
        !in_array[$suffix, ['.xls', '.xlsx']] && die['文件名格式错误!'];

        $cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized;
        $cacheSettings = array['memoryCacheSize ' => '512MB'];
        \PHPExcel_Settings::setCacheStorageMethod[$cacheMethod, $cacheSettings];

        $oExcel = new \PHPExcel[];
        $oExcel->setActiveSheetIndex[0];
        $sheet = $oExcel->getActiveSheet[];

        // 设置列数据格式
        if [!empty[self::$styleFormat]] {
            $fields = array_keys[$titles];
            foreach [self::$styleFormat as $field => $formatCode] {
                $offset = array_search[$field, $fields];
                $col = chr[65+$offset];
                $sheet->getStyle[$col]->getNumberFormat[]->setFormatCode[$formatCode];
            }
        }

        // 行索引
        $rowIndex = $bigTitle!=''? 2:1;

        $chr = [
            'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
            'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ'
        ];

        // 设置大标题
        if [$bigTitle != ''] {
            $sheet->mergeCells['A1:'. $chr[count[$titles]-1] .'1'];
            $sheet->getStyle['A1']->applyFromArray[[
                'font' => ['bold'=>true],
                'alignment' => ['horizontal'=>\PHPExcel_Style_Alignment::HORIZONTAL_CENTER]
            ]];
            $sheet->setCellValue['A1', $bigTitle];
        }

        // 设置标题 A1 B1 C1 ....
        $colIndex = 0;
        $fieldsMap = [];
        foreach [$titles as $key => $title] {
            $fieldsMap[] = $key;
            $sheet->setCellValue[$chr[$colIndex] . $rowIndex, $title];
            $colIndex++;
        }

        // 设置内容 A1 B1 C1 ....   A2 B2 C2 ....
        $rowIndex++;
        foreach [$dataArray as $key => $value]
        {
            foreach [$fieldsMap as $colIndex => $field] {
                if [strrpos[$field, '|'] !== false] {
                    $temp1 = explode['|', $field];
                    $pos = strrpos[$temp1[1], '.'];
                    $pos === false && $pos = strlen[$temp1[1]];
                    $temp2 = [];
                    $temp2[0] = substr[$temp1[1], 0, $pos];
                    $temp2[1] = substr[$temp1[1], $pos+1];
                    $val = $value[$temp1[0]];
                    //$val = self::$temp2[0][$extra, $temp2[1], $val];
                    $val = call_user_func_array[array['\common\helpers\ExcelHelper',$temp2[0]],array[$extra, $temp2[1], $val, $value]];
                } else {
                    $val = $field? $value[$field] : $value;
                }
                $sheet->setCellValue[$chr[$colIndex].$rowIndex, $val];
            }
            $rowIndex++;
        }

        header["Content-Type: application/force-download"];
        header["Content-Type: application/octet-stream"];
        header["Content-Type: application/download"];
        if [$suffix == '.xlsx'] {
            header['Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
        } else {
            header['Content-Type: application/vnd.ms-excel'];
        }
        header['Content-Disposition: attachment;filename="'. $filename .'"'];
        header["Content-Transfer-Encoding: binary"];
        header["Pragma: no-cache"];
        $oWriter = \PHPExcel_IOFactory::createWriter[$oExcel, 'Excel2007'];
        $oWriter->save['php://output'];
        $oExcel->disconnectWorksheets[];
        exit;
    }

Mã nguồn xuất chi tiết cụ thể có thể được xem bởi một bài viết khác: //blog.csdn.net/QQ_41187577/article/details/112346208
//blog.csdn.net/qq_41187577/article/details/112346208

Bài Viết Liên Quan

Chủ Đề