Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created October 18, 2023 09:47
Show Gist options
  • Save code-boxx/ddc607c520cf471a201128b7a1d0a1a8 to your computer and use it in GitHub Desktop.
Save code-boxx/ddc607c520cf471a201128b7a1d0a1a8 to your computer and use it in GitHub Desktop.
PHP Add Rows To Excel File

PHP ADD NEW ROWS TO EXCEL FILE

https://code-boxx.com/php-add-rows-excel/

NOTES

  1. GIST does not allow Excel files. Convert x-dummy.csv to x-dummy.xlsx on your own, or use your own dummy Excel file.
  2. Install Composer - https://getcomposer.org/
  3. Install PHPSpreadsheet.
    • Open the terminal.
    • Navigate to your project folder - cd MY/PROJECT/FOLDER
    • Run composer require phpoffice/phpspreadsheet

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
// (A) LOAD PHPSPREADSHEET
require "vendor/autoload.php";
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// (B) OPEN WORKSHEET
$spreadsheet = PhpOffice\PhpSpreadsheet\IOFactory::load("x-dummy.xlsx");
$worksheet = $spreadsheet->getActiveSheet();
// (C) APPEND ROW
$row = $worksheet->getHighestRow()+1;
$worksheet->setCellValue("A$row", "NEW");
$worksheet->setCellValue("B$row", "ROW");
// (D) ALTERNATIVELY
$data = ["ANOTHER", "ROW"];
$row++;
$char = 65; // "A"
foreach ($data as $d) {
$worksheet->setCellValue(chr($char) . $row, $d);
$char++;
}
// (E) OUTPUT
$writer = new Xlsx($spreadsheet);
$writer->save("demoA.xlsx");
echo "OK!";
<?php
// (A) LOAD PHPSPREADSHEET
require "vendor/autoload.php";
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// (B) OPEN WORKSHEET
$spreadsheet = PhpOffice\PhpSpreadsheet\IOFactory::load("x-dummy.xlsx");
$worksheet = $spreadsheet->getActiveSheet();
// (C) PREPEND NEW ROW
$worksheet->insertNewRowBefore(1);
$worksheet->setCellValue("A1", "NEW");
$worksheet->setCellValue("B1", "ROW");
// (D) OUTPUT
$writer = new Xlsx($spreadsheet);
$writer->save("demoB.xlsx");
echo "OK!";
<?php
// (A) LOAD PHPSPREADSHEET
require "vendor/autoload.php";
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// (B) OPEN WORKSHEET
$spreadsheet = PhpOffice\PhpSpreadsheet\IOFactory::load("x-dummy.xlsx");
$worksheet = $spreadsheet->getActiveSheet();
// (C) INSERT AT ROW 3
$at = 3;
$worksheet->insertNewRowBefore($at);
$worksheet->setCellValue("A$at", "NEW");
$worksheet->setCellValue("B$at", "ROW");
// (D) SEARCH
$at = null;
foreach ($worksheet->getRowIterator() as $row) {
foreach ($row->getCellIterator() as $cell) {
if ($cell->getValue() == "Jon Doe") { $at = $cell->getRow(); break; }
}
}
if ($at != null) {
$worksheet->insertNewRowBefore($at);
$worksheet->setCellValue("A$at", "ANOTHER");
$worksheet->setCellValue("B$at", "ROW");
}
// (E) OUTPUT
$writer = new Xlsx($spreadsheet);
$writer->save("demoC.xlsx");
echo "OK!";
Joa Doe joa@doe.com
Job Doe job@doe.com
Joe Doe joe@doe.com
Joi Doe joi@doe.com
Jon Doe jon@doe.com
Joy Doe joy@doe.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment