Answers for "phpspreadsheet excel to json"

PHP
2

phpspreadsheet excel to json

<?php

require(DIR_SYSTEM . 'library/export_import/vendor/autoload.php');

        $inputFileName = __DIR__ . '/1.xlsx';
        $inputFileType = 'Xlsx';

        /**  Create a new Reader of the type defined in $inputFileType  **/
        $reader = PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
        /**  Advise the Reader that we only want to load cell data  **/
        $reader->setReadDataOnly(true);
        /**  Load $inputFileName to a Spreadsheet Object  **/
        $spreadsheet = $reader->load($inputFileName);
        $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
        $worksheet = $spreadsheet->getSheet(0);//
        // Get the highest row and column numbers referenced in the worksheet
        $highestRow = $worksheet->getHighestRow(); // e.g. 10
        $highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
        $highestColumnIndex = PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);

        $data = array();
        for ($row = 1; $row <= $highestRow; $row++) {
            $riga = array();
            for ($col = 1; $col <= $highestColumnIndex; $col++) {
                $riga[] = $worksheet->getCellByColumnAndRow($col, $row)->getValue();
            }
            if (1 === $row) {
                // Header row. Save it in "$keys".
                $keys = $riga;
                continue;
            }
            $data[] = array_combine($keys, $riga);
        }

        header('Content-Type: application/json');

        print json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
Posted by: Guest on October-28-2021

Code answers related to "phpspreadsheet excel to json"

Browse Popular Code Answers by Language