Yii2에서 Excel 파일을로드 할 수 없습니다.

Nov 19 2020

Excel 파일을 읽기 위해 PHP 스프레드 시트 를 사용 하고 있습니다.

$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();
}

하지만 아래 오류가 발생합니다.

"/ usaid / Files / Installation_ Report.xlsx"파일이 없습니다. F : \ xampp \ htdocs \ usaid \ vendor \ phpoffice \ phpspreadsheet \ src \ PhpSpreadsheet \ Shared \ File.php

경로는

F:\xampp\htdocs\usaid\Files

업데이트 1

이 답변에 따라 코드를 변경하려고했습니다.

 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();
    }


}

이제 나는 얻고있다

InvalidArgumentException 파일 "usaid / Files / Installation_Report.xlsx"가 존재하지 않습니다.

참고 :$inputFileName = Yii::$app->basePath.'\Files\Installation_Report.xlsx'; 동일한 결과로 시도했습니다 .

답변

1 Nicoweb Nov 28 2020 at 01:02
realpath(Yii::$app->basePath).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'Files'.DIRECTORY_SEPARATOR.'Installation_Report.xlsx';
  • '/'대신 상수 DIRECTORY_SEPARATOR를 사용하십시오.
  • 결과는 다음과 같아야합니다. "F : \ xampp \ htdocs \ usaid \ Files \ Installation_Report.xlsx"
UrmatZhenaliev Nov 22 2020 at 04:00
  1. Linux 및 Windows 디렉터리 구분 기호를 엉망으로 만들지 마십시오. Windows \는를 사용하고 Linux는/
  2. 엑셀 파일이있는 디렉토리 dir에서 터미널에서 명령을 실행하여 파일이 실제로 존재하는지 확인하십시오. Windows 파일 탐색기는 확장자없이 파일 이름을 표시 할 수 있습니다. 따라서 파일 이름이 다음과 같은지 확인하십시오.Installation_Report.xlsx.xlsx
  3. 사용해보십시오 realpath(Yii::$app->basePath) . '/Files/Installation_Report.xlsx';
  4. 탐색기에서 바로 파일 이름을 복사하여 응용 프로그램에 붙여 넣으십시오. 라틴 알파벳 기호 / 키릴 문자 / 다른 알파벳에 충돌이있을 수 있습니다.