Jump to content

read excel file using php 8


Recommended Posts

Dear Respected Sir/Madam

 

I am looking for excel file reader specifically .xlsx reader using php 8

 

I was using PHPExcel-develop but doesnot support php 8
also tried
PhpSpreadsheet-master
PHPExcel-1.8
spreadsheet-reader-master

with no success
can anyone help me
compatible excel reader (preferably .xlsx)

 

Vikas

Link to comment
Share on other sites

To read .xlsx files using PHP 8, you can use the PhpSpreadsheet library. It's the successor to PHPExcel and fully supports PHP 8. You can run this command "composer require phpoffice/phpspreadsheet" and Once you have PhpSpreadsheet installed, you can use it to read .xlsx files. Here's a basic example:

<?php
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\IOFactory;

try {
    $spreadsheet = IOFactory::load('path/to/your/file.xlsx');
    $sheet = $spreadsheet->getActiveSheet();

    // Get the highest row and column numbers referenced in the worksheet
    $highestRow = $sheet->getHighestRow(); // e.g. 10
    $highestColumn = $sheet->getHighestColumn(); // e.g 'F'

    // Iterate over each row in the worksheet in turn
    for ($row = 1; $row <= $highestRow; $row++) {
        $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
        print_r($rowData);
    }
} catch (Exception $e) {
    echo 'Error loading file: ', $e->getMessage();
}
?>

 

Link to comment
Share on other sites

3 hours ago, Olumide said:

To read .xlsx files using PHP 8, you can use the PhpSpreadsheet library. It's the successor to PHPExcel and fully supports PHP 8. You can run this command "composer require phpoffice/phpspreadsheet" and Once you have PhpSpreadsheet installed, you can use it to read .xlsx files. Here's a basic example:

<?php
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\IOFactory;

try {
    $spreadsheet = IOFactory::load('path/to/your/file.xlsx');
    $sheet = $spreadsheet->getActiveSheet();

    // Get the highest row and column numbers referenced in the worksheet
    $highestRow = $sheet->getHighestRow(); // e.g. 10
    $highestColumn = $sheet->getHighestColumn(); // e.g 'F'

    // Iterate over each row in the worksheet in turn
    for ($row = 1; $row <= $highestRow; $row++) {
        $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
        print_r($rowData);
    }
} catch (Exception $e) {
    echo 'Error loading file: ', $e->getMessage();
}
?>

 

highly appreciate your help

 

Link to comment
Share on other sites

On 6/28/2024 at 10:48 AM, Olumide said:

To read .xlsx files using PHP 8, you can use the PhpSpreadsheet library. It's the successor to PHPExcel and fully supports PHP 8. You can run this command "composer require phpoffice/phpspreadsheet" and Once you have PhpSpreadsheet installed, you can use it to read .xlsx files. Here's a basic example:

<?php
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\IOFactory;

try {
    $spreadsheet = IOFactory::load('path/to/your/file.xlsx');
    $sheet = $spreadsheet->getActiveSheet();

    // Get the highest row and column numbers referenced in the worksheet
    $highestRow = $sheet->getHighestRow(); // e.g. 10
    $highestColumn = $sheet->getHighestColumn(); // e.g 'F'

    // Iterate over each row in the worksheet in turn
    for ($row = 1; $row <= $highestRow; $row++) {
        $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
        print_r($rowData);
    }
} catch (Exception $e) {
    echo 'Error loading file: ', $e->getMessage();
}
?>

 

i  wish to read excel  using a php code that runs on  web server / website

1. My main issue is how to use composer on website before using this code can you please help me 

2. Which file i need to download from internet and how to run composer that will help me to run the code 

Link to comment
Share on other sites

On 7/2/2024 at 11:56 AM, VikasAthavale said:

i  wish to read excel  using a php code that runs on  web server / website

1. My main issue is how to use composer on website before using this code can you please help me 

2. Which file i need to download from internet and how to run composer that will help me to run the code 

You'll need to install Composer on your web server. Composer is a dependency manager for PHP that allows you to easily manage libraries and packages.

Go to the https://getcomposer.org/, download the Installer for Composer depending on your OS.

Open the downloaded Composer-Setup.exe (Windows OS as an example) file.

Follow the prompts in the installation wizard.

Make sure to select the option to add Composer to the system PATH during installation.

Verify Installation:

Open Command Prompt.

Type composer and press Enter.

You should see the Composer version and available commands, confirming that Composer is installed and accessible globally.

Open Command Prompt and navigate to your project directory: cd path\to\your\project\directory

Use Composer to install the PhpSpreadsheet library by creating a composer.json file and running composer install.

Include the Composer autoload file in your PHP script and use PhpSpreadsheet to read your Excel files.

Link to comment
Share on other sites

  • 2 weeks later...
Posted (edited)
On 7/5/2024 at 4:39 AM, Olumide said:

You'll need to install Composer on your web server. Composer is a dependency manager for PHP that allows you to easily manage libraries and packages.

Go to the https://getcomposer.org/, download the Installer for Composer depending on your OS.

Open the downloaded Composer-Setup.exe (Windows OS as an example) file.

Follow the prompts in the installation wizard.

Make sure to select the option to add Composer to the system PATH during installation.

Verify Installation:

Open Command Prompt.

Type composer and press Enter.

You should see the Composer version and available commands, confirming that Composer is installed and accessible globally.

Open Command Prompt and navigate to your project directory: cd path\to\your\project\directory

Use Composer to install the PhpSpreadsheet library by creating a composer.json file and running composer install.

Include the Composer autoload file in your PHP script and use PhpSpreadsheet to read your Excel files.

for those who struggle to use composer

On web server composer is already installed. open cpanel -> terminal for command prompt 

type command

>composer -v 

will show you the details of composer installed

https://packagist.org/ helps you the search the packages to be installed 

and on top the command to install using composer (to be used after opening terminal from cpanel

example

https://packagist.org/packages/phpmailer/phpmailer

on top of the page   command line to be used is shown

i had spent lot of time in finding this and wish to save your time incase you are not aware

 

 

 

Edited by VikasAthavale
forgot to mention https://packagist.org/ that was great help to me
Link to comment
Share on other sites

  • 1 month later...
On 7/15/2024 at 9:00 AM, VikasAthavale said:

for those who struggle to use composer

On web server composer is already installed. open cpanel -> terminal for command prompt 

type command

>composer -v 

will show you the details of composer installed

https://packagist.org/ helps you the search the packages to be installed 

and on top the command to install using composer (to be used after opening terminal from cpanel

example

https://packagist.org/packages/phpmailer/phpmailer

on top of the page   command line to be used is shown

i had spent lot of time in finding this and wish to save your time incase you are not aware

 

 

 

 

Link to comment
Share on other sites

Dear All,

I am uploading an excel with 2 columns

Yes i can read excel file. sample attached.

Code 

<?php
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\IOFactory;

try {
    $spreadsheet = IOFactory::load('uom.xlsx');
    $sheet = $spreadsheet->getActiveSheet();

    // Get the highest row and column numbers referenced in the worksheet
    $highestRow = $sheet->getHighestRow(); // e.g. 10
    $highestColumn = $sheet->getHighestColumn(); // e.g 'F'
  
    
    // Iterate over each row in the worksheet in turn
    for ($row = 1; $row <= $highestRow; $row++) {
        $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
        print_r($rowData)."<br>";
    }
} catch (Exception $e) {
    echo 'Error loading file: ', $e->getMessage();
}
?>

I am getting output 

Array ( [0] => Array ( [0] => Measure [1] => UPS Code ) )
 
Array ( [0] => Array ( [0] => Bag [1] => BG ) )
 
Array ( [0] => Array ( [0] => Barrel [1] => BA ) )
 
Array ( [0] => Array ( [0] => Bolt [1] => BT ) )


 

this is result of    

  print_r($rowData)."<br>"

so i tried 

echo "0=".$rowData[0]" 1=".$rowData[1]

to get individual cell

I get (could not get indivudual cell values.

pls help/ guide to get individual  cell values

Output after above change

echo "0=".$rowData[0]" 1=".$rowData[1];

to get individual cell.


 

Array ( [0] => Array ( [0] => Measure [1] => UPS Code ))
Separate Array *** rowData[0] =Array rowData[1] << rowData
Array ( [0] => Array ( [0] => Bag [1] => BG ) )

Separate Array *** rowData[0] =Array rowData[1] << rowData
Array ( [0] => Array ( [0] => Barrel [1] => BA ) )

Separate Array *** rowData[0] =Array rowData[1] << rowData
Array ( [0] => Array ( [0] => Bolt [1] => BT ) )

Separate Array *** rowData[0] =Array rowData[1] << rowData
Array ( [0] => Array ( [0] => Box [1] => BOX ) )

Separate Array *** rowData[0] =Array rowData[1] << rowData
Array ( [0] => Array ( [0] => Bunch [1] => BH ) )

Separate Array *** rowData[0] =Array rowData[1] << rowData
Array ( [0] => Array ( [0] => Bundle [1] => BE ) )


 

 

uom screenshot.png

Link to comment
Share on other sites

Please use code tags for your code and output.

I did some editing of your post to reflect this.

 

Your issue is here:

    // Iterate over each row in the worksheet in turn
    for ($row = 1; $row <= $highestRow; $row++) {
        $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
        print_r($rowData)."<br>";
    }

You are retrieving one row at a time, into an array variable, you then overwrite on the next iteration of the loop.  If you want to make one array with all the rows you have retrieved you would do that like so:

 

    // Iterate over each row in the worksheet in turn
    $rowData = array();
    for ($row = 1; $row <= $highestRow; $row++) {
        $rowData[] = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
    }

 

$rowData will be a multidimensional array, where each element is an array of 2 elements (representing the 2 columns (A & B) in the spreadsheet.

Because the first row contains a header, to access the 2nd row You could do something like this:

echo "Measure: {$rowData[1][0]}  UPS Code: {$rowData[1][1]}";

 

If you wanted to output all the values:

foreach($rowData as $row) {

   echo "{$row[0]}: {$row[1]} <br>";

}

Hopefully, you should now see ways to output the data in whatever markup format you want.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.