# SimpleXLSX class (Official)
[](https://packagist.org/packages/shuchkin/simplexlsx)
[](https://github.com/shuchkin/simplexlsx/blob/master/license.md) [](https://github.com/shuchkin/simplexlsx/stargazers) [](https://github.com/shuchkin/simplexlsx/network) [](https://github.com/shuchkin/simplexlsx/issues)
[](https://opencollective.com/simplexlsx)
Parse and retrieve data from Excel XLSx files. MS Excel 2007 workbooks PHP reader.
No addiditional extensions need (internal unzip + standart SimpleXML parser).
See also:
[SimpleXLS](https://github.com/shuchkin/simplexls) old format MS Excel 97 php reader.
[SimpleXLSXGen](https://github.com/shuchkin/simplexlsxgen) xlsx php writer.
*Hey, bro, please ★ the package for my motivation :) and [donate](https://opencollective.com/simplexlsx) for more motivation!*
**Sergey Shuchkin**
## Basic Usage
```php
use Shuchkin\SimpleXLSX;
if ( $xlsx = SimpleXLSX::parse('book.xlsx') ) {
print_r( $xlsx->rows() );
} else {
echo SimpleXLSX::parseError();
}
```
```
Array
(
[0] => Array
(
[0] => ISBN
[1] => title
[2] => author
[3] => publisher
[4] => ctry
)
[1] => Array
(
[0] => 618260307
[1] => The Hobbit
[2] => J. R. R. Tolkien
[3] => Houghton Mifflin
[4] => USA
)
)
```
## Installation
The recommended way to install this library is [through Composer](https://getcomposer.org).
[New to Composer?](https://getcomposer.org/doc/00-intro.md)
This will install the latest supported version:
```bash
$ composer require shuchkin/simplexlsx
```
or download PHP 5.5+ class [here](https://github.com/shuchkin/simplexlsx/blob/master/src/SimpleXLSX.php)
## Basic methods
```
…
```
## Examples
### XLSX to html table
```php
echo SimpleXLSX::parse('book.xlsx')->toHTML();
```
or
```php
if ( $xlsx = SimpleXLSX::parse('book.xlsx') ) {
echo '
';
foreach( $xlsx->rows() as $r ) {
echo '| '.implode(' | ', $r ).' |
';
}
echo '
';
} else {
echo SimpleXLSX::parseError();
}
```
or styled html table
```php
if ( $xlsx = SimpleXLSX::parse('book_styled.xlsx') ) {
echo $xlsx->toHTMLEx();
}
```
### XLSX read huge file, xlsx to csv
```php
if ( $xlsx = SimpleXLSX::parse( 'xlsx/books.xlsx' ) ) {
$f = fopen('book.csv', 'wb');
// fwrite($f, chr(0xEF) . chr(0xBB) . chr(0xBF)); // UTF-8 BOM
foreach ( $xlsx->readRows() as $r ) {
fputcsv($f, $r); // fputcsv($f, $r, ';', '"', "\\", "\r\n");
}
fclose($f);
} else {
echo SimpleXLSX::parseError();
}
```
### XLSX get sheet names and sheet indexes
```php
// Sheet numeration started 0
if ( $xlsx = SimpleXLSX::parse( 'xlsx/books.xlsx' ) ) {
print_r( $xlsx->sheetNames() );
print_r( $xlsx->sheetName( $xlsx->activeSheet ) );
}
```
```
Array
(
[0] => Sheet1
[1] => Sheet2
[2] => Sheet3
)
Sheet2
```
### Using rowsEx() to extract cell info
```php
$xlsx = SimpleXLSX::parse('book.xlsx');
print_r( $xlsx->rowsEx() );
```
```
…
```
| type | cell type |
| name | cell name (A1, B11) |
| value | cell value (1233, 1233.34, 2022-02-21 00:00:00, String) |
| href | internal and external links |
| f | formula |
| s | style index, use $xlsx->cellFormats[ $index ] to get style |
| css | generated cell CSS |
| r | row index |
| hidden | hidden row or column |
| width | width in custom units |
| height | height in points (pt, 1/72 in) |
| comment | Cell comment as plain text |
### Select Sheet
```php
$xlsx = SimpleXLSX::parse('book.xlsx');
// Sheet numeration started 0, we select second worksheet
foreach( $xlsx->rows(1) as $r ) {
// ...
}
```
### Get sheet by index
```php
$xlsx = SimpleXLSX::parse('book.xlsx');
echo 'Sheet Name 2 = '.$xlsx->sheetName(1);
```
### XLSX::parse remote data
```php
if ( $xlsx = SimpleXLSX::parse('https://www.example.com/example.xlsx' ) ) {
$dim = $xlsx->dimension(1); // don't trust dimension extracted from xml
$num_cols = $dim[0];
$num_rows = $dim[1];
echo $xlsx->sheetName(1).':'.$num_cols.'x'.$num_rows;
} else {
echo SimpleXLSX::parseError();
}
```
### XLSX::parse memory data
```php
// For instance $data is a data from database or cache
if ( $xlsx = SimpleXLSX::parseData( $data ) ) {
print_r( $xlsx->rows() );
} else {
echo SimpleXLSX::parseError();
}
```
### Get Cell (slow)
```php
echo $xlsx->getCell(0, 'B2'); // The Hobbit
```
### DateTime helpers
```
…
```
### Rows with header values as keys
```php
if ( $xlsx = SimpleXLSX::parse('books.xlsx')) {
// Produce array keys from the array values of 1st array element
$header_values = $rows = [];
foreach ( $xlsx->rows() as $k => $r ) {
if ( $k === 0 ) {
$header_values = $r;
continue;
}
$rows[] = array_combine( $header_values, $r );
}
print_r( $rows );
}
```
```
Array
(
[0] => Array
(
[ISBN] => 618260307
[title] => The Hobbit
[author] => J. R. R. Tolkien
[publisher] => Houghton Mifflin
[ctry] => USA
)
[1] => Array
(
[ISBN] => 908606664
[title] => Slinky Malinki
[author] => Lynley Dodd
[publisher] => Mallinson Rendel
[ctry] => NZ
)
)
```
### Debug
```php
use Shuchkin\SimpleXLSX;
ini_set('error_reporting', E_ALL );
ini_set('display_errors', 1 );
if ( $xlsx = SimpleXLSX::parseFile('books.xlsx', true ) ) {
echo $xlsx->toHTML();
} else {
echo SimpleXLSX::parseError();
}
```
### Classic OOP style
```php
use SimpleXLSX;
$xlsx = new SimpleXLSX('books.xlsx'); // try...catch
if ( $xlsx->success() ) {
foreach( $xlsx->rows() as $r ) {
// ...
}
} else {
echo 'xlsx error: '.$xlsx->error();
}
```
More examples [here](https://github.com/shuchkin/simplexlsx/tree/master/examples)
### Error Codes
SimpleXLSX::ParseErrno(), $xlsx->errno()
| code | message | comment |
| 1 | File not found | Where file? UFO? |
| 2 | Unknown archive format | ZIP? |
| 3 | XML-entry parser error | bad XML |
| 4 | XML-entry not found | bad ZIP archive |
| 5 | Entry not found | File not found in ZIP archive |
| 6 | Worksheet not found | Not exists |