PHP | Spreadsheet
Last Updated :
07 Mar, 2024
Introduction: PHPSpreadsheet is a library written in PHP which helps to read from and write to different types of spreadsheet file formats with the help of a given set of classes. The various format which support spreadsheet are Excel(.xlsx), Open Document Format(.ods),SpreadsheetML(.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 installed with the help of Composer.
On Terminal: The following command runs on the terminal to install PHPSpreadsheet:
composer require phpoffice/phpspreadsheet
Example 1:
PHP
<?php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet ->getActiveSheet();
$sheet ->setCellValue( 'A1' , 'GeeksForGeeks!' );
$sheet ->setCellValue( 'B1' , 'A Computer Science Portal For Geeks' );
$writer = new Xlsx( $spreadsheet );
$writer ->save( 'gfg.xlsx' );
?>
|
Output:
Example 2:
PHP
<?php
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load( 'gfg.xlsx' );
$data = array (1, $spreadsheet ->getActiveSheet()
->toArray(null,true,true,true));
var_dump( $data );
?>
|
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"
}
}
}
Share your thoughts in the comments
Please Login to comment...