PHP | Spreadsheet

Introduction: PHPSpreasheet is a library written in PHP which helps to read from and write to different types of spreadsheet file formats with the help of given set of classes. The various format which support spreadsheet are Excel(.xlsx), Open Document Format(.ods),SpreasheetML(.xml), CSV and many more.

Advantages:

  • Easy and effective comparisons.
  • Powerful analysis of large amounts of data.

Usability:

  • Agendas
  • Budgets
  • Calendars
  • Cards
  • Charts and Diagrams
  • Financial Tools (Loan calculators etc.)
  • Flyers
  • Forms
  • Inventories
  • Invoices
  • Lists and to-do checklists
  • Planners
  • Plans and proposals
  • Reports
  • Schedules
  • Timesheets

Requirements: The following software is developed using PHPSpreadsheet:

  • PHP version 5.6 or newer
  • PHP extension php_zip enabled
  • PHP extension php_xml enabled
  • PHP extension php_gd2 enabled

Installation: The PHPSpreadsheet can be install with the help of Composer



On Terminal: The following command run on terminal to install PHPSpreadsheet:

composer require phpoffice/phpspreadsheet

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// require_once('vendor/autoload.php');
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  
// Creates New Spreadsheet
$spreadsheet = new Spreadsheet();
  
// Retrieve the current active worksheet
$sheet = $spreadsheet->getActiveSheet();
  
// Set the value of cell A1
$sheet->setCellValue('A1', 'GeeksForGeeks!');
  
// Sets the value of cell B1
$sheet->setCellValue('B1', 'A Computer Science Portal For Geeks');
   
// Write an .xlsx file 
$writer = new Xlsx($spreadsheet);
  
// Save .xlsx file to the current directory
$writer->save('gfg.xlsx');
?>

chevron_right


Output:
example 1

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// require_once('path/vendor/autoload.php');
// Load an .xlsx file
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('gfg.xlsx');
   
// Store data from the activeSheet to the varibale
// in the form of Array
   
$data = array(1,$spreadsheet->getActiveSheet()
            ->toArray(null,true,true,true));
  
// Display the sheet content
var_dump($data);
?>

chevron_right


Output:

array(2) { 
    [0]=> int(1) 
    [1]=> array(1) { 
        [1]=> array(2) {
            ["A"]=> string(14) "GeeksForGeeks!" 
            ["B"]=> string(35) "A Computer Science Portal For Geeks"
        } 
    }
}

Reference: https://phpspreadsheet.readthedocs.io/en/develop/



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.