PHP code example of alexandrainst / php-xlsx-fast-editor
1. Go to this page and download the library: Download alexandrainst/php-xlsx-fast-editor library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alexandrainst / php-xlsx-fast-editor example snippets
use alexandrainst\XlsxFastEditor\XlsxFastEditor;
use alexandrainst\XlsxFastEditor\XlsxFastEditorException;
try {
$xlsxFastEditor = new XlsxFastEditor('test.xlsx');
// Workbook / worksheet methods
$nbWorksheets = $xlsxFastEditor->getWorksheetCount();
$worksheetName = $xlsxFastEditor->getWorksheetName(1);
$worksheetId1 = $xlsxFastEditor->getWorksheetNumber('Sheet1');
// If you want to force Excel to recalculate formulas on next load:
$xlsxFastEditor->setFullCalcOnLoad($worksheetId1, true);
$fullCalcOnLoad = $xlsxFastEditor->getFullCalcOnLoad($worksheetId1);
// Direct read/write access
$fx = $xlsxFastEditor->readFormula($worksheetId1, 'A1');
$f = $xlsxFastEditor->readFloat($worksheetId1, 'B2');
$i = $xlsxFastEditor->readInt($worksheetId1, 'C3');
$s = $xlsxFastEditor->readString($worksheetId1, 'D4');
$h = $xlsxFastEditor->readHyperlink($worksheetId1, 'B4');
$d = $xlsxFastEditor->readDateTime($worksheetId1, 'F4');
$xlsxFastEditor->deleteRow($worksheetId1, 5);
$xlsxFastEditor->writeFormula($worksheetId1, 'A1', '=B2*3');
$xlsxFastEditor->writeFloat($worksheetId1, 'B2', 3.14);
$xlsxFastEditor->writeInt($worksheetId1, 'C3', 13);
$xlsxFastEditor->writeString($worksheetId1, 'D4', 'Hello');
$xlsxFastEditor->writeHyperlink($worksheetId1, 'B4', 'https://example.net/'); // Only for cells with an existing hyperlink
// Read as array
$table = $xlsxFastEditor->readArray($worksheetId1);
$s = $table['B'][2];
$table = $xlsxFastEditor->readArrayWithHeaders($worksheetId1);
$s = $table['columnName'][2];
// Regex search & replace operating globally on all the worksheets:
$xlsxFastEditor->textReplace('/Hello/i', 'World');
// Navigation methods for existing rows
$row = $xlsxFastEditor->getFirstRow($worksheetId1);
$row = $xlsxFastEditor->getRow($worksheetId1, 2);
$row = $row->getPreviousRow();
$row = $row->getNextRow();
$row = $xlsxFastEditor->getLastRow($worksheetId1);
$xlsxFastEditor->getHighestColumnName($worksheetId1);
// Methods for rows
$rowNumber = $row->number();
$cell = $row->getCellOrNull('D2');
$cell = $row->getCellOrNull('D');
// Navigation methods for existing cells
$cell = $row->getFirstCell();
$cell = $cell->getPreviousCell();
$cell = $cell->getNextCell();
$cell = $row->getLastCell();
// Methods for cells
$cellName = $cell->name();
$columnName = $cell->column();
$fx = $cell->readFormula();
$f = $cell->readFloat();
$i = $cell->readInt();
$s = $cell->readString();
$h = $cell->readHyperlink();
$d = $cell->readDateTime();
$cell->writeFormula('=B2*3');
$cell->writeFloat(3.14);
$cell->writeInt(13);
$cell->writeString('Hello');
$cell->writeHyperlink('https://example.net/'); // Only for cells with an existing hyperlink
// Iterators for existing rows and cells
foreach ($xlsxFastEditor->rowsIterator($worksheetId1) as $row) {
foreach ($row->cellsIterator() as $cell) {
// $cell->...
}
}
$xlsxFastEditor->save();
// If you do not want to save, call `close()` instead:
// $xlsxFastEditor->close();
} catch (XlsxFastEditorException $xlsxe) {
die($xlsxe->getMessage());
}
sh
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.