Skip to content

Commit e1d1ec1

Browse files
committed
First version
1 parent 7b800c0 commit e1d1ec1

21 files changed

+1156
-2
lines changed

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# http://EditorConfig.org
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
trim_trailing_whitespace = true
8+
9+
[*.md]
10+
indent_size = 4
11+
indent_style = tab
12+
13+
[*.php]
14+
indent_style = tab

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor/

.markdownlint.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"default": true,
3+
"blanks-around-fences": false,
4+
"blanks-around-lists": false,
5+
"first-line-heading": false,
6+
"line-length": false,
7+
"no-hard-tabs": false,
8+
"no-inline-html": {
9+
"allowed_elements": ["br", "img", "kbd"]
10+
},
11+
"no-multiple-blanks": {
12+
"maximum": 2
13+
},
14+
"ul-indent": false,
15+
"ul-style": {
16+
"style": "consistent"
17+
}
18+
}

.markdownlintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.git/
2+
vendor/

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
vendor/bin/phpstan:
2+
composer install --prefer-dist --no-progress
3+
4+
composer-test: vendor/bin/phpstan
5+
composer run-script test
6+
7+
composer-fix:
8+
composer run-script fix
9+
10+
.PHONY: test
11+
test: composer-test
12+
13+
.PHONY: fix
14+
fix: composer-fix

README.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,58 @@
1-
# php-xslx-fast-editor
2-
PHP library to make basic and fast read-write operations on existing Excel workbooks
1+
# php-xlsx-fast-editor
2+
3+
PHP library to make basic and fast read-write operations on existing Excel workbooks.
4+
5+
It handles XLSX/XLSM documents (Microsoft Excel 2007+, Office Open XML Workbook) using fast and simple low-level ZIP & XML manipulations,
6+
without requiring any library dependency.
7+
8+
## Rationale
9+
10+
If you need advanced manipulation of Excel documents such as working with formulas and styles,
11+
check the [PhpSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet) library
12+
(previously [PHPExcel](https://github.com/PHPOffice/PHPExcel/)),
13+
but for simply reading & writing basic values from existing Excel workbooks, `PhpSpreadsheet` is over an order of magnitude too slow.
14+
15+
There are also libraries to create new Excel documents from scratch, or for just reading some values, but not any obvious one for editing.
16+
17+
`php-xlsx-fast-editor` addresses the need of quickly reading and writing & editing existing Excel documents.
18+
19+
## Examples
20+
21+
```php
22+
// Use composer or load manually:
23+
require 'vendor/alexandrainst/XlsxFastEditor/autoload.php';
24+
use alexandrainst\XlsxFastEditor\XlsxFastEditor;
25+
26+
$xlsxFastEditor = new XlsxFastEditor('test.xlsx');
27+
28+
$worksheetId1 = $xlsxFastEditor->getWorksheetNumber('Sheet1');
29+
$worksheetId2 = $xlsxFastEditor->getWorksheetNumber('Sheet2');
30+
31+
echo $xlsxFastEditor->readFloat($worksheetId1, 'B2'), "\n";
32+
echo $xlsxFastEditor->readInt($worksheetId1, 'C3'), "\n";
33+
echo $xlsxFastEditor->readString($worksheetId2, 'D4'), "\n";
34+
35+
// If you want to force Excel to recalculate formulas on next load:
36+
$xlsxFastEditor->setFullCalcOnLoad($worksheetId2, true);
37+
38+
$xlsxFastEditor->writeFloat($worksheetId1, 'B2', 3.14);
39+
$xlsxFastEditor->writeInt($worksheetId1, 'C3', 13);
40+
$xlsxFastEditor->writeString($worksheetId2, 'D4', 'Hello');
41+
42+
// Regex search & replace operating globally on all the worksheets:
43+
$xlsxFastEditor->textReplace('/Hello/i', 'World');
44+
45+
$xlsxFastEditor->save();
46+
// If you do not want to save, call `close()` instead:
47+
// $xlsxFastEditor->close();
48+
```
49+
50+
## Requirements
51+
52+
PHP 7.4+ with ZIP and XML extensions.
53+
Check [`composer.json`](./composer.json) for details.
54+
55+
## Credits
56+
57+
Originally written by [Alexandre Alapetite](https://github.com/Alkarex) for the [Alexandra Institute](https://alexandra.dk/), 2023.
58+
License [GNU AGPL](https://gnu.org/licenses/agpl.html).

autoload.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* Modified from https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
5+
*/
6+
7+
spl_autoload_register(function (string $class): void {
8+
9+
// project-specific namespace prefix
10+
$prefix = 'alexandrainst\\XlsxFastEditor\\';
11+
12+
// base directory for the namespace prefix
13+
$base_dir = __DIR__ . '/src/';
14+
15+
// does the class use the namespace prefix?
16+
$len = strlen($prefix);
17+
if (strncmp($prefix, $class, $len) !== 0) {
18+
// no, move to the next registered autoloader
19+
return;
20+
}
21+
22+
$relative_class = substr($class, $len);
23+
$file = $base_dir . str_replace('\\', DIRECTORY_SEPARATOR, $relative_class) . '.php';
24+
25+
if (file_exists($file)) {
26+
require $file;
27+
}
28+
});

composer.json

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"name": "alexandrainst/php-xlsx-fast-editor",
3+
"version": "1.0.0",
4+
"description": "PHP library to make basic and fast read-write operations on existing Excel workbooks",
5+
"keywords": [
6+
"PHP",
7+
"OpenXML",
8+
"Excel",
9+
"xlsx",
10+
"xlsm",
11+
"spreadsheet",
12+
"workbook",
13+
"editor"
14+
],
15+
"homepage": "https://github.com/alexandrainst/php-xlsx-fast-editor",
16+
"type": "library",
17+
"license": "AGPL",
18+
"authors": [
19+
{
20+
"name": "Alexandre Alapetite",
21+
"homepage": "https://github.com/Alkarex",
22+
"role": "Developer"
23+
}
24+
],
25+
"require": {
26+
"php": ">=7.4",
27+
"ext-ctype": "*",
28+
"ext-dom": "*",
29+
"ext-xml": "*",
30+
"ext-zip": "*"
31+
},
32+
"require-dev": {
33+
"php": ">=8",
34+
"ext-simplexml": "*",
35+
"ext-tokenizer": "*",
36+
"ext-xmlwriter": "*",
37+
"phpstan/phpstan": "^1.10",
38+
"phpstan/phpstan-strict-rules": "^1.5",
39+
"squizlabs/php_codesniffer": "^3.7"
40+
},
41+
"scripts": {
42+
"php-lint": "find . -type d -name 'vendor' -prune -o -name '*.php' -print0 | xargs -0 -n1 -P4 php -l 1>/dev/null",
43+
"phpcs": "phpcs . -s",
44+
"phpcbf": "phpcbf . -p -s",
45+
"phpstan": "phpstan analyse --memory-limit 512M .",
46+
"asserts": "php -d zend.assertions tests/test.php",
47+
"test": [
48+
"@php-lint",
49+
"@phpcs",
50+
"@phpstan",
51+
"@asserts"
52+
],
53+
"fix": [
54+
"@phpcbf"
55+
]
56+
},
57+
"autoload": {
58+
"psr-4": {
59+
"alexandrainst\\XlsxFastEditor\\": "src/PhpSpreadsheet"
60+
}
61+
},
62+
"config": {
63+
"allow-plugins": {
64+
"phpstan/extension-installer": false
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)