简体   繁体   English

无法在 Yii2 中加载 Excel 文件

[英]Unable to load excel file in Yii2

I am using PHP Spreadsheet to read an excel file我正在使用PHP 电子表格读取 excel 文件

$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文件“/usaid/Files/Installation_ Report.xlsx”不存在。F:\\xampp\\htdocs\\usaid\\vendor\\phpoffice\\phpspreadsheet\\src\\PhpSpreadsheet\\Shared\\File.php

The path is路径是

F:\xampp\htdocs\usaid\Files

Update 1更新 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. InvalidArgumentException 文件“usaid/Files/Installation_Report.xlsx”不存在。

Note: Tried even with $inputFileName = Yii::$app->basePath.'\\Files\\Installation_Report.xlsx';注意:即使使用$inputFileName = Yii::$app->basePath.'\\Files\\Installation_Report.xlsx';试过$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 '/'.使用常量 DIRECTORY_SEPARATOR 而不是“/”。
  • the result should be : "F:\\xampp\\htdocs\\usaid\\Files\\Installation_Report.xlsx"结果应该是:“F:\\xampp\\htdocs\\usaid\\Files\\Installation_Report.xlsx”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM