Read Xlsx file in PhpSpreadsheet

前端 未结 4 2188
眼角桃花
眼角桃花 2020-12-15 10:43

I want to read an xlsx file that was created in Microsoft Excel, but when I run the following code...

$Source_File = \"test.xlsx\";
$Spreadsheet         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 11:33

    From my understanding, you are missing a piece. Why don't you first create a reader and then load the file.

    Try the following code. It can identify the extension and create the reader of that type accordingly.

    $inputFileName = "Text.xlsx";
    
    /**  Identify the type of $inputFileName  **/
    $inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($inputFileName);
    
    /**  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();
    

    Now you can simply run a foreach loop on the result array.

    foreach( $schdeules as $single_schedule )
    {               
        echo '
    '; foreach( $single_schedule as $single_item ) { echo '

    ' . $single_item . '

    '; } echo '
    '; }

    This is tested and working code.

提交回复
热议问题