【问题标题】:Yii2: PHPExcel error when process Excel contain 90k rowsYii2:处理 Excel 包含 90k 行时出现 PHPExcel 错误
【发布时间】:2017-06-02 11:58:24
【问题描述】:

我有一个上传 Excel 文件过程的应用程序。然后PHPExcel会处理上传的Excel文件。

这是流程:

  • 上传文件
  • 将文件保存到本地目录
  • 从本地目录获取文件
  • 使用 PHPExcel 读取文件

我已成功将 Excel 文件保存到本地目录,从 Excel 文件中读取并获取数据。

但是当我使用包含 90K 行 x 25 列的 Excel 文件时,该文件没有保存到本地目录中。然后我尝试print_r 数据并返回error=> 1

这是 print_r 结果

1. Excel 少于 8k 行并成功保存到本地目录:

2。具有 90K 行且未保存到本地目录的 Excel:

这是我在控制器中的代码

public function actionCreate() {
    $connection = \Yii::$app->db;
    $model = new MasterProduct();
    $userId = Yii::$app->user->id;

    $date = new DateTime('now', new \DateTimeZone('Asia/Bali'));
    $created_at = $date->format('Y:m:d H:i:s');

    $dirTrash = Yii::getAlias('@webroot/trash');
    $dirSubTrash = Yii::getAlias('@webroot/trash/trash_app');

//create directory in local if doesn't exist
    if (!is_dir($dirTrash)) {
        mkdir(Yii::getAlias('@webroot/trash'));
        if (!is_dir($dirSubTrash)) {
            mkdir(Yii::getAlias('@webroot/trash/trash_app'));
        }
    } else {
        if (!is_dir($dirSubTrash)) {
            mkdir(Yii::getAlias('@webroot/trash/trash_app'));
        }
    }

    if (Yii::$app->request->post()) {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $result = [];
        $fileMaster = UploadedFile::getInstancesByName('master_product');
        $middleName = substr(md5(microtime() * 100000), rand(0, 9), 5);

    //named the uploaded file, then save into local directory
        if ($fileMaster !== null) {
            $nameMaster = $userId . '_MP' . '_' . $middleName . '_' . date('Y-m-d') . '_' . $fileMaster[0]->getBaseName() . "." . $fileMaster[0]->getExtension();
            $pathMaster = Yii::getAlias('@webroot/trash/trash_app/') . $nameMaster;
            $fileMaster[0]->saveAs($pathMaster);
            
        } else {
            $error[] = "Choose the" . "<strong>" . " Excel " . "</strong>" . "file first.";
            $result = [
                'error' => $error
            ];
        }

    //this the code i've used to print_out the array in above images
        echo '<pre>';
        print_r($fileMaster);
        die();  

    //Access, and get the data from local directory
        $objPHPExcelMaster = new \PHPExcel();
        $fileNameMaster = Yii::getAlias('@webroot/trash/trash_app/') . $nameMaster;
        $inputFilesMaster = fopen(Yii::getAlias('@webroot/trash/trash_app/') . $nameMaster, "r");
        try {
            $inputFileTypeMaster = \PHPExcel_IOFactory::identify($fileNameMaster);
            $objReaderMaster = \PHPExcel_IOFactory::createReader($inputFileTypeMaster);
            $objPHPExcelMaster = $objReaderMaster->load($fileNameMaster);
        } catch (Exception $ex) {
            die('Error');
        }
        $sheetMaster = $objPHPExcelMaster->getSheet(0);
        $highestRowMaster = $sheetMaster->getHighestDataRow();
        $highestColumnMaster = $sheetMaster->getHighestDataColumn();
     
    //read the Excel file and set into array
        for ($row = 1; $row <= $highestRowMaster; ++$row) {
            $rowDataMaster = $sheetMaster->rangeToArray('A' . $row . ':' . $highestColumnMaster . $row, NULL, TRUE, NULL);
        }

        $tempDataMaster = mysql_escape_string(json_encode($rowDataMaster));

        $commandCheckExist = "SELECT COUNT(user_id) FROM `gm_json_master` WHERE id=$userId";
        $queryCheckExist = Yii::$app->db->createCommand($commandCheckExist)->execute();         
    
    //save data into db
        if ($queryCheckExist == 0) {
            $command_2 = "INSERT INTO json_master(json_m_product) VALUES('$tempDataMaster')";
            $query_2 = Yii::$app->db->createCommand($command_2)->execute();
        } else {
            $commandUpdate = "UPDATE json_master SET json_m_product='$tempDataMaster'";
            $queryUpdate = Yii::$app->db->createCommand($commandUpdate)->execute();
        }

        $result = [
            'url' => Url::to(['/gm/json-master-product/save'])
        ];
        return $result;
    } else {
        return $this->render('create', [
                    'model' => $model,
        ]);
    }
}

这是怎么发生的?以及如何使用 PHPExcel 读取 Excel 的大数据?

【问题讨论】:

    标签: php excel yii2 phpexcel


    【解决方案1】:

    错误代码 1 表示文件大小超过了服务器配置允许的最大文件大小。

    来自docs

    UPLOAD_ERR_INI_SIZE 值:1;上传的文件超过了 php.ini 中的 upload_max_filesize 指令。

    我会尝试提高该限制以及最大 POST 大小限制,看看是否有帮助。

    【讨论】:

    • 我遇到了另一个问题,我已经在 php 脚本中配置了 php 集,例如 set upload_max_filesize = 8M,我正在尝试上传包含 48910 行数据的 Excel 文件,但我得到了错误“已用尽 134217728 字节的允许内存大小(尝试分配 43 字节)”和“超过 30 秒的最大执行时间”所以我设置了 memory_limit=512Mmax_execution_time=240,但 PHPExcel 只能访问数据并设置到数组中,直到索引 38485。你知道为什么会这样吗?我试过增加 max_execution_time 和 memory_limit 但它没有解决它。谢谢
    猜你喜欢
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多