2017-03-13 02:33:02 +00:00
|
|
|
|
# Reading and writing to file
|
|
|
|
|
|
|
|
|
|
As you already know from the [architecture](./architecture.md#readers-and-writers),
|
|
|
|
|
reading and writing to a
|
|
|
|
|
persisted storage is not possible using the base PhpSpreadsheet classes.
|
|
|
|
|
For this purpose, PhpSpreadsheet provides readers and writers, which are
|
2017-12-30 10:44:32 +00:00
|
|
|
|
implementations of `\PhpOffice\PhpSpreadsheet\Reader\IReader` and
|
|
|
|
|
`\PhpOffice\PhpSpreadsheet\Writer\IWriter`.
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
|
|
|
|
## \PhpOffice\PhpSpreadsheet\IOFactory
|
|
|
|
|
|
|
|
|
|
The PhpSpreadsheet API offers multiple methods to create a
|
2017-12-30 10:44:32 +00:00
|
|
|
|
`\PhpOffice\PhpSpreadsheet\Reader\IReader` or
|
|
|
|
|
`\PhpOffice\PhpSpreadsheet\Writer\IWriter` instance:
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
2017-12-30 10:44:32 +00:00
|
|
|
|
Direct creation via `\PhpOffice\PhpSpreadsheet\IOFactory`. All examples
|
2017-03-13 02:33:02 +00:00
|
|
|
|
underneath demonstrate the direct creation method. Note that you can
|
2017-12-30 10:44:32 +00:00
|
|
|
|
also use the `\PhpOffice\PhpSpreadsheet\IOFactory` class to do this.
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
2017-12-30 10:44:32 +00:00
|
|
|
|
### Creating `\PhpOffice\PhpSpreadsheet\Reader\IReader` using `\PhpOffice\PhpSpreadsheet\IOFactory`
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
|
|
|
|
There are 2 methods for reading in a file into PhpSpreadsheet: using
|
|
|
|
|
automatic file type resolving or explicitly.
|
|
|
|
|
|
|
|
|
|
Automatic file type resolving checks the different
|
2017-12-30 10:44:32 +00:00
|
|
|
|
`\PhpOffice\PhpSpreadsheet\Reader\IReader` distributed with
|
2017-03-13 02:33:02 +00:00
|
|
|
|
PhpSpreadsheet. If one of them can load the specified file name, the
|
2017-12-30 10:44:32 +00:00
|
|
|
|
file is loaded using that `\PhpOffice\PhpSpreadsheet\Reader\IReader`.
|
2017-03-13 02:33:02 +00:00
|
|
|
|
Explicit mode requires you to specify which
|
2017-12-30 10:44:32 +00:00
|
|
|
|
`\PhpOffice\PhpSpreadsheet\Reader\IReader` should be used.
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
2017-12-30 10:44:32 +00:00
|
|
|
|
You can create a `\PhpOffice\PhpSpreadsheet\Reader\IReader` instance using
|
|
|
|
|
`\PhpOffice\PhpSpreadsheet\IOFactory` in automatic file type resolving
|
2017-03-13 02:33:02 +00:00
|
|
|
|
mode using the following code sample:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("05featuredemo.xlsx");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
A typical use of this feature is when you need to read files uploaded by
|
|
|
|
|
your users, and you don’t know whether they are uploading xls or xlsx
|
|
|
|
|
files.
|
|
|
|
|
|
|
|
|
|
If you need to set some properties on the reader, (e.g. to only read
|
|
|
|
|
data, see more about this later), then you may instead want to use this
|
|
|
|
|
variant:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile("05featuredemo.xlsx");
|
|
|
|
|
$reader->setReadDataOnly(true);
|
|
|
|
|
$reader->load("05featuredemo.xlsx");
|
|
|
|
|
```
|
|
|
|
|
|
2017-12-30 10:44:32 +00:00
|
|
|
|
You can create a `\PhpOffice\PhpSpreadsheet\Reader\IReader` instance using
|
|
|
|
|
`\PhpOffice\PhpSpreadsheet\IOFactory` in explicit mode using the following
|
2017-03-13 02:33:02 +00:00
|
|
|
|
code sample:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
|
|
|
|
|
$spreadsheet = $reader->load("05featuredemo.xlsx");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Note that automatic type resolving mode is slightly slower than explicit
|
|
|
|
|
mode.
|
|
|
|
|
|
2017-12-30 10:44:32 +00:00
|
|
|
|
### Creating `\PhpOffice\PhpSpreadsheet\Writer\IWriter` using `\PhpOffice\PhpSpreadsheet\IOFactory`
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
2017-12-30 10:44:32 +00:00
|
|
|
|
You can create a `\PhpOffice\PhpSpreadsheet\Writer\IWriter` instance using
|
|
|
|
|
`\PhpOffice\PhpSpreadsheet\IOFactory`:
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Xlsx");
|
|
|
|
|
$writer->save("05featuredemo.xlsx");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Excel 2007 (SpreadsheetML) file format
|
|
|
|
|
|
|
|
|
|
Xlsx file format is the main file format of PhpSpreadsheet. It allows
|
|
|
|
|
outputting the in-memory spreadsheet to a .xlsx file.
|
|
|
|
|
|
|
|
|
|
### \PhpOffice\PhpSpreadsheet\Reader\Xlsx
|
|
|
|
|
|
|
|
|
|
#### Reading a spreadsheet
|
|
|
|
|
|
|
|
|
|
You can read an .xlsx file using the following code:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
|
|
|
|
|
$spreadsheet = $reader->load("05featuredemo.xlsx");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Read data only
|
|
|
|
|
|
|
|
|
|
You can set the option setReadDataOnly on the reader, to instruct the
|
|
|
|
|
reader to ignore styling, data validation, … and just read cell data:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
|
|
|
|
|
$reader->setReadDataOnly(true);
|
|
|
|
|
$spreadsheet = $reader->load("05featuredemo.xlsx");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Read specific sheets only
|
|
|
|
|
|
|
|
|
|
You can set the option setLoadSheetsOnly on the reader, to instruct the
|
|
|
|
|
reader to only load the sheets with a given name:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
|
2018-02-25 12:16:04 +00:00
|
|
|
|
$reader->setLoadSheetsOnly(["Sheet 1", "My special sheet"]);
|
2017-03-13 02:33:02 +00:00
|
|
|
|
$spreadsheet = $reader->load("05featuredemo.xlsx");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Read specific cells only
|
|
|
|
|
|
|
|
|
|
You can set the option setReadFilter on the reader, to instruct the
|
|
|
|
|
reader to only load the cells which match a given rule. A read filter
|
|
|
|
|
can be any class which implements
|
2017-12-30 10:44:32 +00:00
|
|
|
|
`\PhpOffice\PhpSpreadsheet\Reader\IReadFilter`. By default, all cells are
|
|
|
|
|
read using the `\PhpOffice\PhpSpreadsheet\Reader\DefaultReadFilter`.
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
|
|
|
|
The following code will only read row 1 and rows 20 – 30 of any sheet in
|
|
|
|
|
the Excel file:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
class MyReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter {
|
|
|
|
|
|
|
|
|
|
public function readCell($column, $row, $worksheetName = '') {
|
|
|
|
|
// Read title row and rows 20 - 30
|
|
|
|
|
if ($row == 1 || ($row >= 20 && $row <= 30)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
|
|
|
|
|
$reader->setReadFilter( new MyReadFilter() );
|
|
|
|
|
$spreadsheet = $reader->load("06largescale.xlsx");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### \PhpOffice\PhpSpreadsheet\Writer\Xlsx
|
|
|
|
|
|
|
|
|
|
#### Writing a spreadsheet
|
|
|
|
|
|
|
|
|
|
You can write an .xlsx file using the following code:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
|
|
|
|
|
$writer->save("05featuredemo.xlsx");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Formula pre-calculation
|
|
|
|
|
|
|
|
|
|
By default, this writer pre-calculates all formulas in the spreadsheet.
|
|
|
|
|
This can be slow on large spreadsheets, and maybe even unwanted. You can
|
|
|
|
|
however disable formula pre-calculation:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
|
|
|
|
|
$writer->setPreCalculateFormulas(false);
|
|
|
|
|
$writer->save("05featuredemo.xlsx");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Office 2003 compatibility pack
|
|
|
|
|
|
|
|
|
|
Because of a bug in the Office2003 compatibility pack, there can be some
|
|
|
|
|
small issues when opening Xlsx spreadsheets (mostly related to formula
|
|
|
|
|
calculation). You can enable Office2003 compatibility with the following
|
|
|
|
|
code:
|
|
|
|
|
|
|
|
|
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
|
|
|
|
|
$writer->setOffice2003Compatibility(true);
|
|
|
|
|
$writer->save("05featuredemo.xlsx");
|
|
|
|
|
|
2019-08-07 15:28:44 +00:00
|
|
|
|
**Office2003 compatibility option should only be used when needed** because
|
|
|
|
|
it disables several Office2007 file format options, resulting in a
|
|
|
|
|
lower-featured Office2007 spreadsheet.
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
|
|
|
|
## Excel 5 (BIFF) file format
|
|
|
|
|
|
|
|
|
|
Xls file format is the old Excel file format, implemented in
|
|
|
|
|
PhpSpreadsheet to provide a uniform manner to create both .xlsx and .xls
|
|
|
|
|
files. It is basically a modified version of [PEAR
|
2018-10-23 11:26:25 +00:00
|
|
|
|
Spreadsheet\_Excel\_Writer](https://pear.php.net/package/Spreadsheet_Excel_Writer),
|
2017-03-13 02:33:02 +00:00
|
|
|
|
although it has been extended and has fewer limitations and more
|
|
|
|
|
features than the old PEAR library. This can read all BIFF versions that
|
|
|
|
|
use OLE2: BIFF5 (introduced with office 95) through BIFF8, but cannot
|
|
|
|
|
read earlier versions.
|
|
|
|
|
|
|
|
|
|
Xls file format will not be developed any further, it just provides an
|
|
|
|
|
additional file format for PhpSpreadsheet.
|
|
|
|
|
|
|
|
|
|
**Excel5 (BIFF) limitations** Please note that BIFF file format has some
|
|
|
|
|
limits regarding to styling cells and handling large spreadsheets via
|
|
|
|
|
PHP.
|
|
|
|
|
|
|
|
|
|
### \PhpOffice\PhpSpreadsheet\Reader\Xls
|
|
|
|
|
|
|
|
|
|
#### Reading a spreadsheet
|
|
|
|
|
|
|
|
|
|
You can read an .xls file using the following code:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
|
|
|
|
|
$spreadsheet = $reader->load("05featuredemo.xls");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Read data only
|
|
|
|
|
|
|
|
|
|
You can set the option setReadDataOnly on the reader, to instruct the
|
|
|
|
|
reader to ignore styling, data validation, … and just read cell data:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
|
|
|
|
|
$reader->setReadDataOnly(true);
|
|
|
|
|
$spreadsheet = $reader->load("05featuredemo.xls");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Read specific sheets only
|
|
|
|
|
|
|
|
|
|
You can set the option setLoadSheetsOnly on the reader, to instruct the
|
|
|
|
|
reader to only load the sheets with a given name:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
|
2018-02-25 12:16:04 +00:00
|
|
|
|
$reader->setLoadSheetsOnly(["Sheet 1", "My special sheet"]);
|
2017-03-13 02:33:02 +00:00
|
|
|
|
$spreadsheet = $reader->load("05featuredemo.xls");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Read specific cells only
|
|
|
|
|
|
|
|
|
|
You can set the option setReadFilter on the reader, to instruct the
|
|
|
|
|
reader to only load the cells which match a given rule. A read filter
|
|
|
|
|
can be any class which implements
|
2017-12-30 10:44:32 +00:00
|
|
|
|
`\PhpOffice\PhpSpreadsheet\Reader\IReadFilter`. By default, all cells are
|
|
|
|
|
read using the `\PhpOffice\PhpSpreadsheet\Reader\DefaultReadFilter`.
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
|
|
|
|
The following code will only read row 1 and rows 20 to 30 of any sheet
|
|
|
|
|
in the Excel file:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
class MyReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter {
|
|
|
|
|
|
|
|
|
|
public function readCell($column, $row, $worksheetName = '') {
|
|
|
|
|
// Read title row and rows 20 - 30
|
|
|
|
|
if ($row == 1 || ($row >= 20 && $row <= 30)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
|
|
|
|
|
$reader->setReadFilter( new MyReadFilter() );
|
|
|
|
|
$spreadsheet = $reader->load("06largescale.xls");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### \PhpOffice\PhpSpreadsheet\Writer\Xls
|
|
|
|
|
|
|
|
|
|
#### Writing a spreadsheet
|
|
|
|
|
|
|
|
|
|
You can write an .xls file using the following code:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($spreadsheet);
|
|
|
|
|
$writer->save("05featuredemo.xls");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Excel 2003 XML file format
|
|
|
|
|
|
|
|
|
|
Excel 2003 XML file format is a file format which can be used in older
|
|
|
|
|
versions of Microsoft Excel.
|
|
|
|
|
|
|
|
|
|
**Excel 2003 XML limitations** Please note that Excel 2003 XML format
|
|
|
|
|
has some limits regarding to styling cells and handling large
|
|
|
|
|
spreadsheets via PHP.
|
|
|
|
|
|
|
|
|
|
### \PhpOffice\PhpSpreadsheet\Reader\Xml
|
|
|
|
|
|
|
|
|
|
#### Reading a spreadsheet
|
|
|
|
|
|
|
|
|
|
You can read an Excel 2003 .xml file using the following code:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xml();
|
|
|
|
|
$spreadsheet = $reader->load("05featuredemo.xml");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Read specific cells only
|
|
|
|
|
|
|
|
|
|
You can set the option setReadFilter on the reader, to instruct the
|
|
|
|
|
reader to only load the cells which match a given rule. A read filter
|
|
|
|
|
can be any class which implements
|
2017-12-30 10:44:32 +00:00
|
|
|
|
`\PhpOffice\PhpSpreadsheet\Reader\IReadFilter`. By default, all cells are
|
|
|
|
|
read using the `\PhpOffice\PhpSpreadsheet\Reader\DefaultReadFilter`.
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
|
|
|
|
The following code will only read row 1 and rows 20 to 30 of any sheet
|
|
|
|
|
in the Excel file:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
class MyReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter {
|
|
|
|
|
|
|
|
|
|
public function readCell($column, $row, $worksheetName = '') {
|
|
|
|
|
// Read title row and rows 20 - 30
|
|
|
|
|
if ($row == 1 || ($row >= 20 && $row <= 30)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xml();
|
|
|
|
|
$reader->setReadFilter( new MyReadFilter() );
|
|
|
|
|
$spreadsheet = $reader->load("06largescale.xml");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Symbolic LinK (SYLK)
|
|
|
|
|
|
|
|
|
|
Symbolic Link (SYLK) is a Microsoft file format typically used to
|
|
|
|
|
exchange data between applications, specifically spreadsheets. SYLK
|
|
|
|
|
files conventionally have a .slk suffix. Composed of only displayable
|
|
|
|
|
ANSI characters, it can be easily created and processed by other
|
|
|
|
|
applications, such as databases.
|
|
|
|
|
|
|
|
|
|
**SYLK limitations** Please note that SYLK file format has some limits
|
|
|
|
|
regarding to styling cells and handling large spreadsheets via PHP.
|
|
|
|
|
|
|
|
|
|
### \PhpOffice\PhpSpreadsheet\Reader\Slk
|
|
|
|
|
|
|
|
|
|
#### Reading a spreadsheet
|
|
|
|
|
|
|
|
|
|
You can read an .slk file using the following code:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Slk();
|
|
|
|
|
$spreadsheet = $reader->load("05featuredemo.slk");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Read specific cells only
|
|
|
|
|
|
|
|
|
|
You can set the option setReadFilter on the reader, to instruct the
|
|
|
|
|
reader to only load the cells which match a given rule. A read filter
|
|
|
|
|
can be any class which implements
|
2017-12-30 10:44:32 +00:00
|
|
|
|
`\PhpOffice\PhpSpreadsheet\Reader\IReadFilter`. By default, all cells are
|
|
|
|
|
read using the `\PhpOffice\PhpSpreadsheet\Reader\DefaultReadFilter`.
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
|
|
|
|
The following code will only read row 1 and rows 20 to 30 of any sheet
|
|
|
|
|
in the SYLK file:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
class MyReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter {
|
|
|
|
|
|
|
|
|
|
public function readCell($column, $row, $worksheetName = '') {
|
|
|
|
|
// Read title row and rows 20 - 30
|
|
|
|
|
if ($row == 1 || ($row >= 20 && $row <= 30)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Slk();
|
|
|
|
|
$reader->setReadFilter( new MyReadFilter() );
|
|
|
|
|
$spreadsheet = $reader->load("06largescale.slk");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Open/Libre Office (.ods)
|
|
|
|
|
|
|
|
|
|
Open Office or Libre Office .ods files are the standard file format for
|
|
|
|
|
Open Office or Libre Office Calc files.
|
|
|
|
|
|
|
|
|
|
### \PhpOffice\PhpSpreadsheet\Reader\Ods
|
|
|
|
|
|
|
|
|
|
#### Reading a spreadsheet
|
|
|
|
|
|
|
|
|
|
You can read an .ods file using the following code:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Ods();
|
|
|
|
|
$spreadsheet = $reader->load("05featuredemo.ods");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Read specific cells only
|
|
|
|
|
|
|
|
|
|
You can set the option setReadFilter on the reader, to instruct the
|
|
|
|
|
reader to only load the cells which match a given rule. A read filter
|
|
|
|
|
can be any class which implements
|
2017-12-30 10:44:32 +00:00
|
|
|
|
`\PhpOffice\PhpSpreadsheet\Reader\IReadFilter`. By default, all cells are
|
|
|
|
|
read using the `\PhpOffice\PhpSpreadsheet\Reader\DefaultReadFilter`.
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
|
|
|
|
The following code will only read row 1 and rows 20 to 30 of any sheet
|
|
|
|
|
in the Calc file:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
class MyReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter {
|
|
|
|
|
|
|
|
|
|
public function readCell($column, $row, $worksheetName = '') {
|
|
|
|
|
// Read title row and rows 20 - 30
|
|
|
|
|
if ($row == 1 || ($row >= 20 && $row <= 30)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$reader = new PhpOffice\PhpSpreadsheet\Reader\Ods();
|
|
|
|
|
$reader->setReadFilter( new MyReadFilter() );
|
|
|
|
|
$spreadsheet = $reader->load("06largescale.ods");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## CSV (Comma Separated Values)
|
|
|
|
|
|
|
|
|
|
CSV (Comma Separated Values) are often used as an import/export file
|
|
|
|
|
format with other systems. PhpSpreadsheet allows reading and writing to
|
|
|
|
|
CSV files.
|
|
|
|
|
|
|
|
|
|
**CSV limitations** Please note that CSV file format has some limits
|
|
|
|
|
regarding to styling cells, number formatting, ...
|
|
|
|
|
|
|
|
|
|
### \PhpOffice\PhpSpreadsheet\Reader\Csv
|
|
|
|
|
|
|
|
|
|
#### Reading a CSV file
|
|
|
|
|
|
|
|
|
|
You can read a .csv file using the following code:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
|
|
|
|
|
$spreadsheet = $reader->load("sample.csv");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Setting CSV options
|
|
|
|
|
|
2017-04-17 16:51:53 +00:00
|
|
|
|
Often, CSV files are not really "comma separated", or use semicolon (`;`)
|
2017-03-13 02:33:02 +00:00
|
|
|
|
as a separator. You can instruct
|
2017-12-30 10:44:32 +00:00
|
|
|
|
`\PhpOffice\PhpSpreadsheet\Reader\Csv` some options before reading a CSV
|
2017-03-13 02:33:02 +00:00
|
|
|
|
file.
|
|
|
|
|
|
2017-04-17 16:51:53 +00:00
|
|
|
|
The separator will be auto-detected, so in most cases it should not be necessary
|
|
|
|
|
to specify it. But in cases where auto-detection does not fit the use-case, then
|
|
|
|
|
it can be set manually.
|
|
|
|
|
|
2017-12-30 10:44:32 +00:00
|
|
|
|
Note that `\PhpOffice\PhpSpreadsheet\Reader\Csv` by default assumes that
|
2017-03-13 02:33:02 +00:00
|
|
|
|
the loaded CSV file is UTF-8 encoded. If you are reading CSV files that
|
|
|
|
|
were created in Microsoft Office Excel the correct input encoding may
|
|
|
|
|
rather be Windows-1252 (CP1252). Always make sure that the input
|
|
|
|
|
encoding is set appropriately.
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
|
|
|
|
|
$reader->setInputEncoding('CP1252');
|
|
|
|
|
$reader->setDelimiter(';');
|
|
|
|
|
$reader->setEnclosure('');
|
|
|
|
|
$reader->setSheetIndex(0);
|
|
|
|
|
|
|
|
|
|
$spreadsheet = $reader->load("sample.csv");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Read a specific worksheet
|
|
|
|
|
|
|
|
|
|
CSV files can only contain one worksheet. Therefore, you can specify
|
|
|
|
|
which sheet to read from CSV:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$reader->setSheetIndex(0);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Read into existing spreadsheet
|
|
|
|
|
|
|
|
|
|
When working with CSV files, it might occur that you want to import CSV
|
|
|
|
|
data into an existing `Spreadsheet` object. The following code loads a
|
2017-03-13 05:57:37 +00:00
|
|
|
|
CSV file into an existing `$spreadsheet` containing some sheets, and
|
2017-03-13 02:33:02 +00:00
|
|
|
|
imports onto the 6th sheet:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
|
|
|
|
|
$reader->setDelimiter(';');
|
|
|
|
|
$reader->setEnclosure('');
|
|
|
|
|
$reader->setSheetIndex(5);
|
|
|
|
|
|
|
|
|
|
$reader->loadIntoExisting("05featuredemo.csv", $spreadsheet);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### \PhpOffice\PhpSpreadsheet\Writer\Csv
|
|
|
|
|
|
|
|
|
|
#### Writing a CSV file
|
|
|
|
|
|
|
|
|
|
You can write a .csv file using the following code:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
|
|
|
|
|
$writer->save("05featuredemo.csv");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Setting CSV options
|
|
|
|
|
|
2017-03-13 05:57:37 +00:00
|
|
|
|
Often, CSV files are not really "comma separated", or use semicolon (`;`)
|
2017-03-13 02:33:02 +00:00
|
|
|
|
as a separator. You can instruct
|
2017-12-30 10:44:32 +00:00
|
|
|
|
`\PhpOffice\PhpSpreadsheet\Writer\Csv` some options before writing a CSV
|
2017-03-13 02:33:02 +00:00
|
|
|
|
file:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
|
|
|
|
|
$writer->setDelimiter(';');
|
|
|
|
|
$writer->setEnclosure('');
|
|
|
|
|
$writer->setLineEnding("\r\n");
|
|
|
|
|
$writer->setSheetIndex(0);
|
|
|
|
|
|
|
|
|
|
$writer->save("05featuredemo.csv");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Write a specific worksheet
|
|
|
|
|
|
|
|
|
|
CSV files can only contain one worksheet. Therefore, you can specify
|
|
|
|
|
which sheet to write to CSV:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer->setSheetIndex(0);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Formula pre-calculation
|
|
|
|
|
|
|
|
|
|
By default, this writer pre-calculates all formulas in the spreadsheet.
|
|
|
|
|
This can be slow on large spreadsheets, and maybe even unwanted. You can
|
|
|
|
|
however disable formula pre-calculation:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
|
|
|
|
|
$writer->setPreCalculateFormulas(false);
|
|
|
|
|
$writer->save("05featuredemo.csv");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Writing UTF-8 CSV files
|
|
|
|
|
|
Improve Coverage for CSV (#1475)
I believe that both CSV Reader and Writer are 100% covered now.
There were some errors uncovered during development.
The reader specifically permits encodings other than UTF-8 to be used.
However, fgetcsv will not properly handle other encodings.
I tried replacing it with fgets/iconv/strgetcsv, but that could not
handle line breaks within a cell, even for UTF-8.
This is, I'm sure, a very rare use case.
I eventually handled it by using php://memory to hold the translated
file contents for non-UTF8. There were no tests for this situation,
and now there are (probably too many).
"Contiguous" read was not handle correctly. There is a file
in samples which uses it. It was designed to read a large sheet,
and split it into three. The first sheet was corrrect, but the
second and third were almost entirely empty. This has been corrected,
and the sample code was adapted into a formal test with assertions
to confirm that it works as designed.
I made a minor documentation change. Unlike HTML, where you never
need a BOM because you can declare the encoding in the file,
a CSV with non-ASCII characters must explicitly include a BOM
for Excel to handle it correctly. This was explained in the Reading CSV
section, but was glossed over in the Writing CSV section, which I
have updated.
2020-05-17 09:15:18 +00:00
|
|
|
|
CSV files are written in UTF-8. If they do not contain characters
|
|
|
|
|
outside the ASCII range, nothing else need be done.
|
|
|
|
|
However, if such characters are in the file,
|
|
|
|
|
it should explicitly include a BOM file header;
|
|
|
|
|
if it doesn't, Excel will not interpret those characters correctly.
|
|
|
|
|
This can be enabled by using the following code:
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
|
|
|
|
|
$writer->setUseBOM(true);
|
|
|
|
|
$writer->save("05featuredemo.csv");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Decimal and thousands separators
|
|
|
|
|
|
|
|
|
|
If the worksheet you are exporting contains numbers with decimal or
|
|
|
|
|
thousands separators then you should think about what characters you
|
|
|
|
|
want to use for those before doing the export.
|
|
|
|
|
|
|
|
|
|
By default PhpSpreadsheet looks up in the server's locale settings to
|
|
|
|
|
decide what characters to use. But to avoid problems it is recommended
|
|
|
|
|
to set the characters explicitly as shown below.
|
|
|
|
|
|
|
|
|
|
English users will want to use this before doing the export:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Shared\StringHelper::setDecimalSeparator('.');
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Shared\StringHelper::setThousandsSeparator(',');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
German users will want to use the opposite values.
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Shared\StringHelper::setDecimalSeparator(',');
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Shared\StringHelper::setThousandsSeparator('.');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Note that the above code sets decimal and thousand separators as global
|
|
|
|
|
options. This also affects how HTML and PDF is exported.
|
|
|
|
|
|
|
|
|
|
## HTML
|
|
|
|
|
|
|
|
|
|
PhpSpreadsheet allows you to read or write a spreadsheet as HTML format,
|
|
|
|
|
for quick representation of the data in it to anyone who does not have a
|
|
|
|
|
spreadsheet application on their PC, or loading files saved by other
|
|
|
|
|
scripts that simply create HTML markup and give it a .xls file
|
|
|
|
|
extension.
|
|
|
|
|
|
|
|
|
|
**HTML limitations** Please note that HTML file format has some limits
|
|
|
|
|
regarding to styling cells, number formatting, ...
|
|
|
|
|
|
|
|
|
|
### \PhpOffice\PhpSpreadsheet\Reader\Html
|
|
|
|
|
|
|
|
|
|
#### Reading a spreadsheet
|
|
|
|
|
|
|
|
|
|
You can read an .html or .htm file using the following code:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Html();
|
|
|
|
|
|
|
|
|
|
$spreadsheet = $reader->load("05featuredemo.html");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**HTML limitations** Please note that HTML reader is still experimental
|
|
|
|
|
and does not yet support merged cells or nested tables cleanly
|
|
|
|
|
|
|
|
|
|
### \PhpOffice\PhpSpreadsheet\Writer\Html
|
|
|
|
|
|
2017-12-30 10:44:32 +00:00
|
|
|
|
Please note that `\PhpOffice\PhpSpreadsheet\Writer\Html` only outputs the
|
2017-03-13 02:33:02 +00:00
|
|
|
|
first worksheet by default.
|
|
|
|
|
|
|
|
|
|
#### Writing a spreadsheet
|
|
|
|
|
|
|
|
|
|
You can write a .htm file using the following code:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet);
|
|
|
|
|
|
|
|
|
|
$writer->save("05featuredemo.htm");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Write all worksheets
|
|
|
|
|
|
|
|
|
|
HTML files can contain one or more worksheets. If you want to write all
|
|
|
|
|
sheets into a single HTML file, use the following code:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer->writeAllSheets();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Write a specific worksheet
|
|
|
|
|
|
|
|
|
|
HTML files can contain one or more worksheets. Therefore, you can
|
|
|
|
|
specify which sheet to write to HTML:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer->setSheetIndex(0);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Setting the images root of the HTML file
|
|
|
|
|
|
|
|
|
|
There might be situations where you want to explicitly set the included
|
2018-06-04 05:09:21 +00:00
|
|
|
|
images root. For example, instead of:
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
2018-06-04 05:09:21 +00:00
|
|
|
|
``` html
|
|
|
|
|
<img src="./images/logo.jpg">
|
|
|
|
|
```
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
2018-06-04 05:09:21 +00:00
|
|
|
|
You might want to see:
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
|
|
|
|
``` html
|
2018-06-04 05:09:21 +00:00
|
|
|
|
<img src="http://www.domain.com/images/logo.jpg">
|
2017-03-13 02:33:02 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
You can use the following code to achieve this result:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer->setImagesRoot('http://www.example.com');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Formula pre-calculation
|
|
|
|
|
|
|
|
|
|
By default, this writer pre-calculates all formulas in the spreadsheet.
|
|
|
|
|
This can be slow on large spreadsheets, and maybe even unwanted. You can
|
|
|
|
|
however disable formula pre-calculation:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet);
|
|
|
|
|
$writer->setPreCalculateFormulas(false);
|
|
|
|
|
|
|
|
|
|
$writer->save("05featuredemo.htm");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Embedding generated HTML in a web page
|
|
|
|
|
|
|
|
|
|
There might be a situation where you want to embed the generated HTML in
|
|
|
|
|
an existing website. \PhpOffice\PhpSpreadsheet\Writer\Html provides
|
|
|
|
|
support to generate only specific parts of the HTML code, which allows
|
|
|
|
|
you to use these parts in your website.
|
|
|
|
|
|
|
|
|
|
Supported methods:
|
|
|
|
|
|
2017-03-13 05:57:37 +00:00
|
|
|
|
- `generateHTMLHeader()`
|
|
|
|
|
- `generateStyles()`
|
|
|
|
|
- `generateSheetData()`
|
|
|
|
|
- `generateHTMLFooter()`
|
Improve HTML Writer (#1464)
There are a number of situations where HTML write was producing
HTML which could not be validated. These include:
- inconsistent use of backslash terminating META, IMG, and COL tags
- @page style tags in body rather than header. Aside from being
non-standard, HTML Reader treats those as spreadsheet data.
- <div style="page-break-before:always" />, a construct which is
usually better handled through css anyhow.
- no alt tag for images (drawings and charts)
Other problems:
- Windows file names not handled correctly for images
- Memory drawings not handled in extendRowsForChartsAndImages
- No handling of different values for showing gridlines
for screen and print
- Mpdf and Dompdf do not require the use of inline css.
Tcpdf remains a holdout in the use of this inferior approach.
- no need to chunk base64 encoding of embedded images
- support for colors in number format was buggy (html tags
run through htmlspecialchars)
Code has been refactored when practical to reduce the number of
very large functions.
Coverage is now 100% for the entire HTML Writer module,
from 75% lines and 39% methods beforehand.
All functions dealing only with charts
are bypassed for coverage because the version of Jpgraph available in
Composer is not suitable for PHP7. The code will, nevertheless,
run successfully, but with warning messages. I have confirmed that
the code is entirely covered, without warnings, when the current
version of Jpgraph is used in lieu of the one available in Composer.
I will be glad to revisit this when the Jpgraph problem is resolved.
Directory PhpSpreadsheetTests/Writer/Html was created to house
the new tests. It seemed logical to move HtmlCommentsTest to
the new directory from PhpSpreadsheetTests/Functional.
A function to generate all the HTML is useful, especially for testing,
but also in lieu of the multiple other generate* functions. I have
added and documented generateHTMLAll.
The documentation for the generate* functions (a) produces invalid html,
(b) produces html which cannot be handled correctly by HTML reader,
and (c) even if those were correct, does not actually affect
the display of the spreadsheet. The documentation has been replaced
by a valid, and more instructive, example.
The (undocumented) useEmbeddedCss property, and the functions
to test and set it are no longer needed. Rather than breaking
existing code by deleting them, I marked the functions deprecated.
This change borrows a change to LocaleFloatsTest from
pull request 1456, submitted a little over a week before this one.
## Improve NumberFormat Support
First phase of this change included correcting NumberFormat handling
in HTML Writer. Certain complex formats could not be handled without
changes to Style/NumberFormat, and I did not wish to combine those changes.
Once the original change had been pushed, I took this part of it back up.
HTML Writer can now handle conditions in formats like:
[Blue][>=3000.5]$#,##0.00;[Red][<0]$#,##0.00;$#,##0.00
In testing, I discovered several errors and omissions
in handling of some other formats.
These are now corrected, and tests added.
2020-05-18 03:43:18 +00:00
|
|
|
|
- `generateHTMLAll()`
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
|
|
|
|
Here's an example which retrieves all parts independently and merges
|
|
|
|
|
them into a resulting HTML page:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet);
|
Improve HTML Writer (#1464)
There are a number of situations where HTML write was producing
HTML which could not be validated. These include:
- inconsistent use of backslash terminating META, IMG, and COL tags
- @page style tags in body rather than header. Aside from being
non-standard, HTML Reader treats those as spreadsheet data.
- <div style="page-break-before:always" />, a construct which is
usually better handled through css anyhow.
- no alt tag for images (drawings and charts)
Other problems:
- Windows file names not handled correctly for images
- Memory drawings not handled in extendRowsForChartsAndImages
- No handling of different values for showing gridlines
for screen and print
- Mpdf and Dompdf do not require the use of inline css.
Tcpdf remains a holdout in the use of this inferior approach.
- no need to chunk base64 encoding of embedded images
- support for colors in number format was buggy (html tags
run through htmlspecialchars)
Code has been refactored when practical to reduce the number of
very large functions.
Coverage is now 100% for the entire HTML Writer module,
from 75% lines and 39% methods beforehand.
All functions dealing only with charts
are bypassed for coverage because the version of Jpgraph available in
Composer is not suitable for PHP7. The code will, nevertheless,
run successfully, but with warning messages. I have confirmed that
the code is entirely covered, without warnings, when the current
version of Jpgraph is used in lieu of the one available in Composer.
I will be glad to revisit this when the Jpgraph problem is resolved.
Directory PhpSpreadsheetTests/Writer/Html was created to house
the new tests. It seemed logical to move HtmlCommentsTest to
the new directory from PhpSpreadsheetTests/Functional.
A function to generate all the HTML is useful, especially for testing,
but also in lieu of the multiple other generate* functions. I have
added and documented generateHTMLAll.
The documentation for the generate* functions (a) produces invalid html,
(b) produces html which cannot be handled correctly by HTML reader,
and (c) even if those were correct, does not actually affect
the display of the spreadsheet. The documentation has been replaced
by a valid, and more instructive, example.
The (undocumented) useEmbeddedCss property, and the functions
to test and set it are no longer needed. Rather than breaking
existing code by deleting them, I marked the functions deprecated.
This change borrows a change to LocaleFloatsTest from
pull request 1456, submitted a little over a week before this one.
## Improve NumberFormat Support
First phase of this change included correcting NumberFormat handling
in HTML Writer. Certain complex formats could not be handled without
changes to Style/NumberFormat, and I did not wish to combine those changes.
Once the original change had been pushed, I took this part of it back up.
HTML Writer can now handle conditions in formats like:
[Blue][>=3000.5]$#,##0.00;[Red][<0]$#,##0.00;$#,##0.00
In testing, I discovered several errors and omissions
in handling of some other formats.
These are now corrected, and tests added.
2020-05-18 03:43:18 +00:00
|
|
|
|
$hdr = $writer->generateHTMLHeader();
|
|
|
|
|
$sty = $writer->generateStyles(false); // do not write <style> and </style>
|
|
|
|
|
$newstyle = <<<EOF
|
|
|
|
|
<style type='text/css'>
|
|
|
|
|
$sty
|
2017-03-13 02:33:02 +00:00
|
|
|
|
html {
|
Improve HTML Writer (#1464)
There are a number of situations where HTML write was producing
HTML which could not be validated. These include:
- inconsistent use of backslash terminating META, IMG, and COL tags
- @page style tags in body rather than header. Aside from being
non-standard, HTML Reader treats those as spreadsheet data.
- <div style="page-break-before:always" />, a construct which is
usually better handled through css anyhow.
- no alt tag for images (drawings and charts)
Other problems:
- Windows file names not handled correctly for images
- Memory drawings not handled in extendRowsForChartsAndImages
- No handling of different values for showing gridlines
for screen and print
- Mpdf and Dompdf do not require the use of inline css.
Tcpdf remains a holdout in the use of this inferior approach.
- no need to chunk base64 encoding of embedded images
- support for colors in number format was buggy (html tags
run through htmlspecialchars)
Code has been refactored when practical to reduce the number of
very large functions.
Coverage is now 100% for the entire HTML Writer module,
from 75% lines and 39% methods beforehand.
All functions dealing only with charts
are bypassed for coverage because the version of Jpgraph available in
Composer is not suitable for PHP7. The code will, nevertheless,
run successfully, but with warning messages. I have confirmed that
the code is entirely covered, without warnings, when the current
version of Jpgraph is used in lieu of the one available in Composer.
I will be glad to revisit this when the Jpgraph problem is resolved.
Directory PhpSpreadsheetTests/Writer/Html was created to house
the new tests. It seemed logical to move HtmlCommentsTest to
the new directory from PhpSpreadsheetTests/Functional.
A function to generate all the HTML is useful, especially for testing,
but also in lieu of the multiple other generate* functions. I have
added and documented generateHTMLAll.
The documentation for the generate* functions (a) produces invalid html,
(b) produces html which cannot be handled correctly by HTML reader,
and (c) even if those were correct, does not actually affect
the display of the spreadsheet. The documentation has been replaced
by a valid, and more instructive, example.
The (undocumented) useEmbeddedCss property, and the functions
to test and set it are no longer needed. Rather than breaking
existing code by deleting them, I marked the functions deprecated.
This change borrows a change to LocaleFloatsTest from
pull request 1456, submitted a little over a week before this one.
## Improve NumberFormat Support
First phase of this change included correcting NumberFormat handling
in HTML Writer. Certain complex formats could not be handled without
changes to Style/NumberFormat, and I did not wish to combine those changes.
Once the original change had been pushed, I took this part of it back up.
HTML Writer can now handle conditions in formats like:
[Blue][>=3000.5]$#,##0.00;[Red][<0]$#,##0.00;$#,##0.00
In testing, I discovered several errors and omissions
in handling of some other formats.
These are now corrected, and tests added.
2020-05-18 03:43:18 +00:00
|
|
|
|
background-color: yellow;
|
2017-03-13 02:33:02 +00:00
|
|
|
|
}
|
|
|
|
|
</style>
|
Improve HTML Writer (#1464)
There are a number of situations where HTML write was producing
HTML which could not be validated. These include:
- inconsistent use of backslash terminating META, IMG, and COL tags
- @page style tags in body rather than header. Aside from being
non-standard, HTML Reader treats those as spreadsheet data.
- <div style="page-break-before:always" />, a construct which is
usually better handled through css anyhow.
- no alt tag for images (drawings and charts)
Other problems:
- Windows file names not handled correctly for images
- Memory drawings not handled in extendRowsForChartsAndImages
- No handling of different values for showing gridlines
for screen and print
- Mpdf and Dompdf do not require the use of inline css.
Tcpdf remains a holdout in the use of this inferior approach.
- no need to chunk base64 encoding of embedded images
- support for colors in number format was buggy (html tags
run through htmlspecialchars)
Code has been refactored when practical to reduce the number of
very large functions.
Coverage is now 100% for the entire HTML Writer module,
from 75% lines and 39% methods beforehand.
All functions dealing only with charts
are bypassed for coverage because the version of Jpgraph available in
Composer is not suitable for PHP7. The code will, nevertheless,
run successfully, but with warning messages. I have confirmed that
the code is entirely covered, without warnings, when the current
version of Jpgraph is used in lieu of the one available in Composer.
I will be glad to revisit this when the Jpgraph problem is resolved.
Directory PhpSpreadsheetTests/Writer/Html was created to house
the new tests. It seemed logical to move HtmlCommentsTest to
the new directory from PhpSpreadsheetTests/Functional.
A function to generate all the HTML is useful, especially for testing,
but also in lieu of the multiple other generate* functions. I have
added and documented generateHTMLAll.
The documentation for the generate* functions (a) produces invalid html,
(b) produces html which cannot be handled correctly by HTML reader,
and (c) even if those were correct, does not actually affect
the display of the spreadsheet. The documentation has been replaced
by a valid, and more instructive, example.
The (undocumented) useEmbeddedCss property, and the functions
to test and set it are no longer needed. Rather than breaking
existing code by deleting them, I marked the functions deprecated.
This change borrows a change to LocaleFloatsTest from
pull request 1456, submitted a little over a week before this one.
## Improve NumberFormat Support
First phase of this change included correcting NumberFormat handling
in HTML Writer. Certain complex formats could not be handled without
changes to Style/NumberFormat, and I did not wish to combine those changes.
Once the original change had been pushed, I took this part of it back up.
HTML Writer can now handle conditions in formats like:
[Blue][>=3000.5]$#,##0.00;[Red][<0]$#,##0.00;$#,##0.00
In testing, I discovered several errors and omissions
in handling of some other formats.
These are now corrected, and tests added.
2020-05-18 03:43:18 +00:00
|
|
|
|
EOF;
|
|
|
|
|
echo preg_replace('@</head>@', "$newstyle\n</head>", $hdr);
|
2017-03-13 02:33:02 +00:00
|
|
|
|
echo $writer->generateSheetData();
|
|
|
|
|
echo $writer->generateHTMLFooter();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Writing UTF-8 HTML files
|
|
|
|
|
|
|
|
|
|
A HTML file can be marked as UTF-8 by writing a BOM file header. This
|
|
|
|
|
can be enabled by using the following code:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet);
|
|
|
|
|
$writer->setUseBOM(true);
|
|
|
|
|
|
|
|
|
|
$writer->save("05featuredemo.htm");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Decimal and thousands separators
|
|
|
|
|
|
2017-12-30 10:44:32 +00:00
|
|
|
|
See section `\PhpOffice\PhpSpreadsheet\Writer\Csv` how to control the
|
2017-03-13 02:33:02 +00:00
|
|
|
|
appearance of these.
|
|
|
|
|
|
|
|
|
|
## PDF
|
|
|
|
|
|
|
|
|
|
PhpSpreadsheet allows you to write a spreadsheet into PDF format, for
|
|
|
|
|
fast distribution of represented data.
|
|
|
|
|
|
|
|
|
|
**PDF limitations** Please note that PDF file format has some limits
|
|
|
|
|
regarding to styling cells, number formatting, ...
|
|
|
|
|
|
|
|
|
|
### \PhpOffice\PhpSpreadsheet\Writer\Pdf
|
|
|
|
|
|
|
|
|
|
PhpSpreadsheet’s PDF Writer is a wrapper for a 3rd-Party PDF Rendering
|
2017-10-21 16:54:14 +00:00
|
|
|
|
library such as TCPDF, mPDF or Dompdf. You must now install a PDF
|
2017-03-13 02:33:02 +00:00
|
|
|
|
rendering library yourself; but PhpSpreadsheet will work with a number
|
|
|
|
|
of different libraries.
|
|
|
|
|
|
|
|
|
|
Currently, the following libraries are supported:
|
|
|
|
|
|
2017-10-14 05:57:44 +00:00
|
|
|
|
Library | Downloadable from | PhpSpreadsheet writer
|
|
|
|
|
--------|-------------------------------------|----------------------
|
2017-10-21 16:54:14 +00:00
|
|
|
|
TCPDF | https://github.com/tecnickcom/tcpdf | Tcpdf
|
|
|
|
|
mPDF | https://github.com/mpdf/mpdf | Mpdf
|
|
|
|
|
Dompdf | https://github.com/dompdf/dompdf | Dompdf
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
|
|
|
|
The different libraries have different strengths and weaknesses. Some
|
|
|
|
|
generate better formatted output than others, some are faster or use
|
|
|
|
|
less memory than others, while some generate smaller .pdf files. It is
|
|
|
|
|
the developers choice which one they wish to use, appropriate to their
|
|
|
|
|
own circumstances.
|
|
|
|
|
|
2017-10-21 16:54:14 +00:00
|
|
|
|
You can instantiate a writer with its specific name, like so:
|
2017-03-13 02:33:02 +00:00
|
|
|
|
|
|
|
|
|
``` php
|
2017-10-21 16:54:14 +00:00
|
|
|
|
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Mpdf');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Or you can register which writer you are using with a more generic name,
|
|
|
|
|
so you don't need to remember which library you chose, only that you want
|
|
|
|
|
to write PDF files:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$class = \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf::class;
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\IOFactory::registerWriter('Pdf', $class);
|
|
|
|
|
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Pdf');
|
2017-10-14 05:57:44 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Or you can instantiate directly the writer of your choice like so:
|
|
|
|
|
|
|
|
|
|
``` php
|
2017-10-21 16:54:14 +00:00
|
|
|
|
$writer = \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
|
2017-03-13 02:33:02 +00:00
|
|
|
|
```
|
|
|
|
|
|
2017-10-30 15:14:34 +00:00
|
|
|
|
#### Custom implementation or configuration
|
|
|
|
|
|
|
|
|
|
If you need a custom implementation, or custom configuration, of a supported
|
|
|
|
|
PDF library. You can extends the PDF library, and the PDF writer like so:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
class My_Custom_TCPDF extends TCPDF
|
|
|
|
|
{
|
|
|
|
|
// ...
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class My_Custom_TCPDF_Writer extends \PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf
|
|
|
|
|
{
|
|
|
|
|
protected function createExternalWriterInstance($orientation, $unit, $paperSize)
|
|
|
|
|
{
|
|
|
|
|
$instance = new My_Custom_TCPDF($orientation, $unit, $paperSize);
|
2017-12-31 11:53:05 +00:00
|
|
|
|
|
2017-10-30 15:14:34 +00:00
|
|
|
|
// more configuration of $instance
|
2017-12-31 11:53:05 +00:00
|
|
|
|
|
2017-10-30 15:14:34 +00:00
|
|
|
|
return $instance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\IOFactory::registerWriter('Pdf', MY_TCPDF_WRITER::class);
|
|
|
|
|
```
|
|
|
|
|
|
2017-03-13 02:33:02 +00:00
|
|
|
|
#### Writing a spreadsheet
|
|
|
|
|
|
|
|
|
|
Once you have identified the Renderer that you wish to use for PDF
|
|
|
|
|
generation, you can write a .pdf file using the following code:
|
|
|
|
|
|
|
|
|
|
``` php
|
2017-10-21 16:54:14 +00:00
|
|
|
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
|
2017-03-13 02:33:02 +00:00
|
|
|
|
$writer->save("05featuredemo.pdf");
|
|
|
|
|
```
|
|
|
|
|
|
2017-10-14 05:57:44 +00:00
|
|
|
|
Please note that `\PhpOffice\PhpSpreadsheet\Writer\Pdf` only outputs the
|
2017-03-13 02:33:02 +00:00
|
|
|
|
first worksheet by default.
|
|
|
|
|
|
|
|
|
|
#### Write all worksheets
|
|
|
|
|
|
|
|
|
|
PDF files can contain one or more worksheets. If you want to write all
|
|
|
|
|
sheets into a single PDF file, use the following code:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer->writeAllSheets();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Write a specific worksheet
|
|
|
|
|
|
|
|
|
|
PDF files can contain one or more worksheets. Therefore, you can specify
|
|
|
|
|
which sheet to write to PDF:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$writer->setSheetIndex(0);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Formula pre-calculation
|
|
|
|
|
|
|
|
|
|
By default, this writer pre-calculates all formulas in the spreadsheet.
|
|
|
|
|
This can be slow on large spreadsheets, and maybe even unwanted. You can
|
|
|
|
|
however disable formula pre-calculation:
|
|
|
|
|
|
|
|
|
|
``` php
|
2017-10-21 16:54:14 +00:00
|
|
|
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
|
2017-03-13 02:33:02 +00:00
|
|
|
|
$writer->setPreCalculateFormulas(false);
|
|
|
|
|
|
|
|
|
|
$writer->save("05featuredemo.pdf");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Decimal and thousands separators
|
|
|
|
|
|
2017-12-30 10:44:32 +00:00
|
|
|
|
See section `\PhpOffice\PhpSpreadsheet\Writer\Csv` how to control the
|
2017-03-13 02:33:02 +00:00
|
|
|
|
appearance of these.
|
|
|
|
|
|
|
|
|
|
## Generating Excel files from templates (read, modify, write)
|
|
|
|
|
|
|
|
|
|
Readers and writers are the tools that allow you to generate Excel files
|
|
|
|
|
from templates. This requires less coding effort than generating the
|
|
|
|
|
Excel file from scratch, especially if your template has many styles,
|
|
|
|
|
page setup properties, headers etc.
|
|
|
|
|
|
|
|
|
|
Here is an example how to open a template file, fill in a couple of
|
|
|
|
|
fields and save it again:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('template.xlsx');
|
|
|
|
|
|
|
|
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
|
|
|
|
|
|
$worksheet->getCell('A1')->setValue('John');
|
|
|
|
|
$worksheet->getCell('A2')->setValue('Smith');
|
|
|
|
|
|
|
|
|
|
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
|
|
|
|
|
$writer->save('write.xls');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Notice that it is ok to load an xlsx file and generate an xls file.
|
2019-08-14 16:04:21 +00:00
|
|
|
|
|
|
|
|
|
## Generating Excel files from HTML content
|
|
|
|
|
|
|
|
|
|
If you are generating an Excel file from pre-rendered HTML content you can do so
|
|
|
|
|
automatically using the HTML Reader. This is most useful when you are generating
|
|
|
|
|
Excel files from web application content that would be downloaded/sent to a user.
|
|
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
$htmlString = '<table>
|
|
|
|
|
<tr>
|
|
|
|
|
<td>Hello World</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td>Hello<br />World</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td>Hello<br>World</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</table>';
|
|
|
|
|
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Html();
|
|
|
|
|
$spreadsheet = $reader->loadFromString($htmlString);
|
|
|
|
|
|
|
|
|
|
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
|
|
|
|
|
$writer->save('write.xls');
|
|
|
|
|
```
|
2019-10-17 19:04:18 +00:00
|
|
|
|
|
|
|
|
|
Suppose you have multiple worksheets you'd like created from html. This can be
|
|
|
|
|
accomplished as follows.
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
$firstHtmlString = '<table>
|
|
|
|
|
<tr>
|
|
|
|
|
<td>Hello World</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</table>';
|
|
|
|
|
$secondHtmlString = '<table>
|
|
|
|
|
<tr>
|
|
|
|
|
<td>Hello World</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</table>';
|
|
|
|
|
|
|
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Html();
|
|
|
|
|
$spreadsheet = $reader->loadFromString($firstHtmlString);
|
|
|
|
|
$reader->setSheetIndex(1);
|
|
|
|
|
$spreadhseet = $reader->loadFromString($secondHtmlString, $spreadsheet);
|
|
|
|
|
|
|
|
|
|
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
|
|
|
|
|
$writer->save('write.xls');
|
|
|
|
|
```
|