无法在Yii2中加载Excel文件 [英] Unable to load excel file in Yii2

查看:60
本文介绍了无法在Yii2中加载Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

InvalidArgumentException文件"usaid/Files/Installation_Report.xlsx"不存在.

注意:即使在 $ inputFileName = Yii :: $ app-> basePath的情况下也尝试过.'\ Files \ Installation_Report.xlsx'; 但结果相同

解决方案

  realpath(Yii :: $ app-> basePath).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'文件'.DIRECTORY_SEPARATOR.'Installation_Report.xlsx'; 

  • 使用常量DIRECTORY_SEPARATOR代替"/".
  • 结果应为:"F:\ xampp \ htdocs \ usaid \ Files \ Installation_Report.xlsx"

I am using PHP Spreadsheet to read an excel file

$inputFileName = '/usaid/Files/Installation_Report.xlsx';
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();

try
{
    /** Load $inputFileName to a Spreadsheet Object  **/
    $spreadsheet = $reader->load($inputFileName);

    print_r($spreadsheet);
    exit();
} catch (\Exception $exception) {
    print_r($exception->getMessage() . $exception->getFile());
    exit();
}

But I am getting below error

File "/usaid/Files/Installation_ Report.xlsx" does not exist.F:\xampp\htdocs\usaid\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Shared\File.php

The path is

F:\xampp\htdocs\usaid\Files

Update 1

As per this answer I have tried to change the code

 public function actionRun()
{

    $inputFileName = 'usaid/Files/Installation_Report.xlsx';

    /**  Identify the type of $inputFileName  **/
    $inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($inputFileName);

    try
    {

        /**  Create a new Reader of the type that has been identified  **/
        $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);

        /**  Load $inputFileName to a Spreadsheet Object  **/
        $spreadsheet = $reader->load($inputFileName);

        /**  Convert Spreadsheet Object to an Array for ease of use  **/
        $schdeules = $spreadsheet->getActiveSheet()->toArray();
        //$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);

        var_dump($schdeules);
        die();
    }
    catch (\Exception $exception)
    {
        print_r($exception->getMessage() . $exception->getFile());
        exit();
    }


}

Now I am getting

InvalidArgumentException File "usaid/Files/Installation_Report.xlsx" does not exist.

Note: Tried even with $inputFileName = Yii::$app->basePath.'\Files\Installation_Report.xlsx'; but same result

解决方案

realpath(Yii::$app->basePath).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'Files'.DIRECTORY_SEPARATOR.'Installation_Report.xlsx';

  • use the constant DIRECTORY_SEPARATOR instead of '/'.
  • the result should be : "F:\xampp\htdocs\usaid\Files\Installation_Report.xlsx"

这篇关于无法在Yii2中加载Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆