2013-05-13 12:59:53 +00:00
# PHPExcel Developer Documentation
2013-05-18 14:56:43 +00:00
## Reading and writing to file
2013-05-13 12:59:53 +00:00
As you already know from part REF _Ref191885438 \w \h 3.3 REF _Ref191885438 \h Readers and writers, reading and writing to a persisted storage is not possible using the base PHPExcel classes. For this purpose, PHPExcel provides readers and writers, which are implementations of PHPExcel_Writer_IReader and PHPExcel_Writer_IWriter.
2013-05-18 14:56:43 +00:00
### PHPExcel_IOFactory
2013-05-13 12:59:53 +00:00
The PHPExcel API offers multiple methods to create a PHPExcel_Writer_IReader or PHPExcel_Writer_IWriter instance:
2013-07-18 14:42:58 +00:00
Direct creation via PHPExcel_IOFactory. All examples underneath demonstrate the direct creation method. Note that you can also use the PHPExcel_IOFactory class to do this.
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
#### Creating PHPExcel_Reader_IReader using PHPExcel_IOFactory
2013-05-13 12:59:53 +00:00
There are 2 methods for reading in a file into PHPExcel: using automatic file type resolving or explicitly.
Automatic file type resolving checks the different PHPExcel_Reader_IReader distributed with PHPExcel. If one of them can load the specified file name, the file is loaded using that PHPExcel_Reader_IReader. Explicit mode requires you to specify which PHPExcel_Reader_IReader should be used.
You can create a PHPExcel_Reader_IReader instance using PHPExcel_IOFactory in automatic file type resolving mode using the following code sample:
2013-05-18 22:22:27 +00:00
```php
2013-05-13 12:59:53 +00:00
$objPHPExcel = PHPExcel_IOFactory::load("05featuredemo.xlsx");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
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:
2013-05-18 22:22:27 +00:00
```php
2013-05-13 12:59:53 +00:00
$objReader = PHPExcel_IOFactory::createReaderForFile("05featuredemo.xlsx");
$objReader->setReadDataOnly(true);
$objReader->load("05featuredemo.xlsx");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
You can create a PHPExcel_Reader_IReader instance using PHPExcel_IOFactory in explicit mode using the following code sample:
2013-05-18 22:22:27 +00:00
```php
2016-10-06 11:39:10 +00:00
$objReader = PHPExcel_IOFactory::createReader("Xlsx");
2013-05-13 12:59:53 +00:00
$objPHPExcel = $objReader->load("05featuredemo.xlsx");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
Note that automatic type resolving mode is slightly slower than explicit mode.
2013-05-18 14:56:43 +00:00
#### Creating PHPExcel_Writer_IWriter using PHPExcel_IOFactory
2013-05-13 12:59:53 +00:00
You can create a PHPExcel_Writer_Iwriter instance using PHPExcel_IOFactory:
2013-05-18 22:22:27 +00:00
```php
2016-10-06 11:39:10 +00:00
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Xlsx");
2013-05-13 12:59:53 +00:00
$objWriter->save("05featuredemo.xlsx");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
### Excel 2007 (SpreadsheetML) file format
2013-05-13 12:59:53 +00:00
2016-10-06 11:39:10 +00:00
Xlsx file format is the main file format of PHPExcel. It allows outputting the in-memory spreadsheet to a .xlsx file.
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
#### PHPExcel_Reader_Excel2007
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Reading a spreadsheet
2013-05-13 12:59:53 +00:00
You can read an .xlsx file using the following code:
2013-05-18 22:22:27 +00:00
```php
2013-05-13 12:59:53 +00:00
$objReader = new PHPExcel_Reader_Excel2007();
$objPHPExcel = $objReader->load("05featuredemo.xlsx");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Read data only
2013-05-13 12:59:53 +00:00
You can set the option setReadDataOnly on the reader, to instruct the reader to ignore styling, data validation, … and just read cell data:
2013-05-18 22:22:27 +00:00
```php
2013-05-13 12:59:53 +00:00
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("05featuredemo.xlsx");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Read specific sheets only
2013-05-13 12:59:53 +00:00
You can set the option setLoadSheetsOnly on the reader, to instruct the reader to only load the sheets with a given name:
2013-05-18 22:22:27 +00:00
```php
2013-05-13 12:59:53 +00:00
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setLoadSheetsOnly( array("Sheet 1", "My special sheet") );
$objPHPExcel = $objReader->load("05featuredemo.xlsx");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Read specific cells only
2013-05-13 12:59:53 +00:00
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 PHPExcel_Reader_IReadFilter. By default, all cells are read using the PHPExcel_Reader_DefaultReadFilter.
The following code will only read row 1 and rows 20 – 30 of any sheet in the Excel file:
2013-05-18 22:22:27 +00:00
```php
class MyReadFilter implements PHPExcel_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;
}
2013-05-13 12:59:53 +00:00
}
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setReadFilter( new MyReadFilter() );
$objPHPExcel = $objReader->load("06largescale.xlsx");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
#### PHPExcel_Writer_Excel2007
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Writing a spreadsheet
2013-05-13 12:59:53 +00:00
You can write an .xlsx file using the following code:
2013-05-18 22:22:27 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save("05featuredemo.xlsx");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Formula pre-calculation
2013-05-13 12:59:53 +00:00
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:
2013-05-18 22:22:27 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->setPreCalculateFormulas(false);
$objWriter->save("05featuredemo.xlsx");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Office 2003 compatibility pack
2013-05-13 12:59:53 +00:00
2016-10-06 11:39:10 +00:00
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:
2013-05-13 12:59:53 +00:00
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->setOffice2003Compatibility(true);
$objWriter->save("05featuredemo.xlsx");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
__Office2003 compatibility should only be used when needed__
Office2003 compatibility option should only be used when needed. This option disables several Office2007 file format options, resulting in a lower-featured Office2007 spreadsheet when this option is used.
2013-05-18 14:56:43 +00:00
### Excel 5 (BIFF) file format
2013-05-13 12:59:53 +00:00
2016-10-06 11:49:41 +00:00
Xls file format is the old Excel file format, implemented in PHPExcel to provide a uniform manner to create both .xlsx and .xls files. It is basically a modified version of [PEAR Spreadsheet_Excel_Writer][21], 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.
2013-05-13 12:59:53 +00:00
2016-10-06 11:49:41 +00:00
Xls file format will not be developed any further, it just provides an additional file format for PHPExcel.
2013-05-13 12:59:53 +00:00
__Excel5 (BIFF) limitations__
Please note that BIFF file format has some limits regarding to styling cells and handling large spreadsheets via PHP.
2013-05-18 14:56:43 +00:00
#### PHPExcel_Reader_Excel5
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Reading a spreadsheet
2013-05-13 12:59:53 +00:00
You can read an .xls file using the following code:
2013-05-18 22:22:27 +00:00
```php
2013-05-13 12:59:53 +00:00
$objReader = new PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader->load("05featuredemo.xls");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Read data only
2013-05-13 12:59:53 +00:00
You can set the option setReadDataOnly on the reader, to instruct the reader to ignore styling, data validation, … and just read cell data:
2013-05-18 22:22:27 +00:00
```php
2013-05-13 12:59:53 +00:00
$objReader = new PHPExcel_Reader_Excel5();
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("05featuredemo.xls");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Read specific sheets only
2013-05-13 12:59:53 +00:00
You can set the option setLoadSheetsOnly on the reader, to instruct the reader to only load the sheets with a given name:
2013-05-18 22:22:27 +00:00
```php
2013-05-13 12:59:53 +00:00
$objReader = new PHPExcel_Reader_Excel5();
$objReader->setLoadSheetsOnly( array("Sheet 1", "My special sheet") );
$objPHPExcel = $objReader->load("05featuredemo.xls");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Read specific cells only
2013-05-13 12:59:53 +00:00
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 PHPExcel_Reader_IReadFilter. By default, all cells are read using the PHPExcel_Reader_DefaultReadFilter.
2013-05-18 22:22:27 +00:00
The following code will only read row 1 and rows 20 to 30 of any sheet in the Excel file:
2013-05-13 12:59:53 +00:00
2013-05-18 22:22:27 +00:00
```php
class MyReadFilter implements PHPExcel_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;
}
2013-05-13 12:59:53 +00:00
}
$objReader = new PHPExcel_Reader_Excel5();
$objReader->setReadFilter( new MyReadFilter() );
$objPHPExcel = $objReader->load("06largescale.xls");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
#### PHPExcel_Writer_Excel5
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Writing a spreadsheet
2013-05-13 12:59:53 +00:00
You can write an .xls file using the following code:
2013-05-18 22:22:27 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save("05featuredemo.xls");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
### Excel 2003 XML file format
2013-05-13 12:59:53 +00:00
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.
2013-05-18 14:56:43 +00:00
#### PHPExcel_Reader_Excel2003XML
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Reading a spreadsheet
2013-05-13 12:59:53 +00:00
2013-05-18 22:22:27 +00:00
You can read an Excel 2003 .xml file using the following code:
2013-05-13 12:59:53 +00:00
2013-05-18 22:22:27 +00:00
```php
2013-05-13 12:59:53 +00:00
$objReader = new PHPExcel_Reader_Excel2003XML();
$objPHPExcel = $objReader->load("05featuredemo.xml");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Read specific cells only
2013-05-13 12:59:53 +00:00
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 PHPExcel_Reader_IReadFilter. By default, all cells are read using the PHPExcel_Reader_DefaultReadFilter.
2013-05-18 22:22:27 +00:00
The following code will only read row 1 and rows 20 to 30 of any sheet in the Excel file:
2013-05-13 12:59:53 +00:00
2013-05-18 22:22:27 +00:00
```php
class MyReadFilter implements PHPExcel_Reader_IReadFilter {
2013-05-13 12:59:53 +00:00
2013-05-18 22:22:27 +00:00
public function readCell($column, $row, $worksheetName = '') {
// Read title row and rows 20 - 30
if ($row == 1 || ($row >= 20 & & $row < = 30)) {
return true;
}
return false;
}
2013-05-13 12:59:53 +00:00
}
$objReader = new PHPExcel_Reader_Excel2003XML();
$objReader->setReadFilter( new MyReadFilter() );
$objPHPExcel = $objReader->load("06largescale.xml");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
### Symbolic LinK (SYLK)
2013-05-13 12:59:53 +00:00
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.
2013-05-18 14:56:43 +00:00
#### PHPExcel_Reader_SYLK
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Reading a spreadsheet
2013-05-13 12:59:53 +00:00
You can read an .slk file using the following code:
2013-05-18 22:22:27 +00:00
```php
2013-05-13 12:59:53 +00:00
$objReader = new PHPExcel_Reader_SYLK();
$objPHPExcel = $objReader->load("05featuredemo.slk");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Read specific cells only
2013-05-13 12:59:53 +00:00
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 PHPExcel_Reader_IReadFilter. By default, all cells are read using the PHPExcel_Reader_DefaultReadFilter.
2013-05-18 22:22:27 +00:00
The following code will only read row 1 and rows 20 to 30 of any sheet in the SYLK file:
2013-05-13 12:59:53 +00:00
2013-05-18 22:22:27 +00:00
```php
class MyReadFilter implements PHPExcel_Reader_IReadFilter {
2013-05-13 12:59:53 +00:00
2013-05-18 22:22:27 +00:00
public function readCell($column, $row, $worksheetName = '') {
// Read title row and rows 20 - 30
if ($row == 1 || ($row >= 20 & & $row < = 30)) {
return true;
}
return false;
}
2013-05-13 12:59:53 +00:00
}
$objReader = new PHPExcel_Reader_SYLK();
$objReader->setReadFilter( new MyReadFilter() );
$objPHPExcel = $objReader->load("06largescale.slk");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
### Open/Libre Office (.ods)
2013-05-13 12:59:53 +00:00
2013-05-18 22:22:27 +00:00
Open Office or Libre Office .ods files are the standard file format for Open Office or Libre Office Calc files.
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
#### PHPExcel_Reader_OOCalc
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Reading a spreadsheet
2013-05-13 12:59:53 +00:00
You can read an .ods file using the following code:
2013-05-18 22:22:27 +00:00
```php
2013-05-13 12:59:53 +00:00
$objReader = new PHPExcel_Reader_OOCalc();
$objPHPExcel = $objReader->load("05featuredemo.ods");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Read specific cells only
2013-05-13 12:59:53 +00:00
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 PHPExcel_Reader_IReadFilter. By default, all cells are read using the PHPExcel_Reader_DefaultReadFilter.
2013-05-18 22:22:27 +00:00
The following code will only read row 1 and rows 20 to 30 of any sheet in the Calc file:
2013-05-13 12:59:53 +00:00
2013-05-18 22:22:27 +00:00
```php
class MyReadFilter implements PHPExcel_Reader_IReadFilter {
2013-05-13 12:59:53 +00:00
2013-05-18 22:22:27 +00:00
public function readCell($column, $row, $worksheetName = '') {
// Read title row and rows 20 - 30
if ($row == 1 || ($row >= 20 & & $row < = 30)) {
return true;
}
return false;
}
2013-05-13 12:59:53 +00:00
}
$objReader = new PHPExcel_Reader_OOcalc();
$objReader->setReadFilter( new MyReadFilter() );
$objPHPExcel = $objReader->load("06largescale.ods");
2013-05-18 22:22:27 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
### CSV (Comma Separated Values)
2013-05-13 12:59:53 +00:00
CSV (Comma Separated Values) are often used as an import/export file format with other systems. PHPExcel allows reading and writing to CSV files.
__CSV limitations__
2013-05-18 22:22:27 +00:00
Please note that CSV file format has some limits regarding to styling cells, number formatting, ...
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
#### PHPExcel_Reader_CSV
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Reading a CSV file
2013-05-13 12:59:53 +00:00
You can read a .csv file using the following code:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objReader = new PHPExcel_Reader_CSV();
$objPHPExcel = $objReader->load("sample.csv");
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Setting CSV options
2013-05-13 12:59:53 +00:00
Often, CSV files are not really “comma separated”, or use semicolon (;) as a separator. You can instruct PHPExcel_Reader_CSV some options before reading a CSV file.
Note that PHPExcel_Reader_CSV by default assumes that 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.
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objReader = new PHPExcel_Reader_CSV();
$objReader->setInputEncoding('CP1252');
$objReader->setDelimiter(';');
$objReader->setEnclosure('');
$objReader->setLineEnding("\r\n");
$objReader->setSheetIndex(0);
$objPHPExcel = $objReader->load("sample.csv");
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Read a specific worksheet
2013-05-13 12:59:53 +00:00
CSV files can only contain one worksheet. Therefore, you can specify which sheet to read from CSV:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objReader->setSheetIndex(0);
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Read into existing spreadsheet
2013-05-13 12:59:53 +00:00
When working with CSV files, it might occur that you want to import CSV data into an existing PHPExcel object. The following code loads a CSV file into an existing $objPHPExcel containing some sheets, and imports onto the 6th sheet:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objReader = new PHPExcel_Reader_CSV();
$objReader->setDelimiter(';');
$objReader->setEnclosure('');
$objReader->setLineEnding("\r\n");
$objReader->setSheetIndex(5);
2013-05-18 14:56:43 +00:00
2013-05-13 12:59:53 +00:00
$objReader->loadIntoExisting("05featuredemo.csv", $objPHPExcel);
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
#### PHPExcel_Writer_CSV
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Writing a CSV file
2013-05-13 12:59:53 +00:00
You can write a .csv file using the following code:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
$objWriter->save("05featuredemo.csv");
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Setting CSV options
2013-05-13 12:59:53 +00:00
Often, CSV files are not really “comma separated”, or use semicolon (;) as a separator. You can instruct PHPExcel_Writer_CSV some options before writing a CSV file:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
$objWriter->setDelimiter(';');
$objWriter->setEnclosure('');
$objWriter->setLineEnding("\r\n");
$objWriter->setSheetIndex(0);
$objWriter->save("05featuredemo.csv");
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Write a specific worksheet
2013-05-13 12:59:53 +00:00
CSV files can only contain one worksheet. Therefore, you can specify which sheet to write to CSV:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter->setSheetIndex(0);
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Formula pre-calculation
2013-05-13 12:59:53 +00:00
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:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
$objWriter->setPreCalculateFormulas(false);
$objWriter->save("05featuredemo.csv");
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Writing UTF-8 CSV files
2013-05-13 12:59:53 +00:00
A CSV file can be marked as UTF-8 by writing a BOM file header. This can be enabled by using the following code:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
$objWriter->setUseBOM(true);
$objWriter->save("05featuredemo.csv");
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Decimal and thousands separators
2013-05-13 12:59:53 +00:00
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.
2013-05-18 14:56:43 +00:00
By default PHPExcel 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.
2013-05-13 12:59:53 +00:00
English users will want to use this before doing the export:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
PHPExcel_Shared_String::setDecimalSeparator('.');
PHPExcel_Shared_String::setThousandsSeparator(',');
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
German users will want to use the opposite values.
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
PHPExcel_Shared_String::setDecimalSeparator(',');
PHPExcel_Shared_String::setThousandsSeparator('.');
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
Note that the above code sets decimal and thousand separators as global options. This also affects how HTML and PDF is exported.
2013-05-18 14:56:43 +00:00
### HTML
2013-05-13 12:59:53 +00:00
PHPExcel 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__
2013-05-18 14:56:43 +00:00
Please note that HTML file format has some limits regarding to styling cells, number formatting, ...
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
#### PHPExcel_Reader_HTML
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Reading a spreadsheet
2013-05-13 12:59:53 +00:00
You can read an .html or .htm file using the following code:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objReader = new PHPExcel_Reader_HTML();
$objPHPExcel = $objReader->load("05featuredemo.html");
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
__HTML limitations__
Please note that HTML reader is still experimental and does not yet support merged cells or nested tables cleanly
2013-05-18 14:56:43 +00:00
#### PHPExcel_Writer_HTML
2013-05-13 12:59:53 +00:00
Please note that PHPExcel_Writer_HTML only outputs the first worksheet by default.
2013-05-18 14:56:43 +00:00
##### Writing a spreadsheet
2013-05-13 12:59:53 +00:00
You can write a .htm file using the following code:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter = new PHPExcel_Writer_HTML($objPHPExcel);
$objWriter->save("05featuredemo.htm");
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Write all worksheets
2013-05-13 12:59:53 +00:00
HTML files can contain one or more worksheets. If you want to write all sheets into a single HTML file, use the following code:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter->writeAllSheets();
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Write a specific worksheet
2013-05-13 12:59:53 +00:00
HTML files can contain one or more worksheets. Therefore, you can specify which sheet to write to HTML:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter->setSheetIndex(0);
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Setting the images root of the HTML file
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
There might be situations where you want to explicitly set the included images root. For example, one might want to see
```html
< img style = "position: relative; left: 0px; top: 0px; width: 140px; height: 78px;" src = "http://www.domain.com/*images/logo.jpg" border = "0" >
```
instead of
```html
< img style = "position: relative; left: 0px; top: 0px; width: 140px; height: 78px;" src = "./images/logo.jpg" border = "0" > .
```
2013-05-13 12:59:53 +00:00
You can use the following code to achieve this result:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter->setImagesRoot('http://www.example.com');
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Formula pre-calculation
2013-05-13 12:59:53 +00:00
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:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter = new PHPExcel_Writer_HTML($objPHPExcel);
$objWriter->setPreCalculateFormulas(false);
$objWriter->save("05featuredemo.htm");
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Embedding generated HTML in a web page
2013-05-13 12:59:53 +00:00
There might be a situation where you want to embed the generated HTML in an existing website. PHPExcel_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:
2013-05-18 14:56:43 +00:00
- generateHTMLHeader()
- generateStyles()
- generateSheetData()
- generateHTMLFooter()
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
Here's an example which retrieves all parts independently and merges them into a resulting HTML page:
```php
2013-05-13 12:59:53 +00:00
< ?php
$objWriter = new PHPExcel_Writer_HTML($objPHPExcel);
echo $objWriter->generateHTMLHeader();
?>
< style >
<!--
html {
2013-05-18 14:56:43 +00:00
font-family: Times New Roman;
font-size: 9pt;
background-color: white;
2013-05-13 12:59:53 +00:00
}
< ?php
echo $objWriter->generateStyles(false); // do not write < style > and < / style >
?>
-->
< / style >
< ?php
echo $objWriter->generateSheetData();
echo $objWriter->generateHTMLFooter();
?>
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Writing UTF-8 HTML files
2013-05-13 12:59:53 +00:00
A HTML file can be marked as UTF-8 by writing a BOM file header. This can be enabled by using the following code:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter = new PHPExcel_Writer_HTML($objPHPExcel);
$objWriter->setUseBOM(true);
$objWriter->save("05featuredemo.htm");
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Decimal and thousands separators
2013-05-13 12:59:53 +00:00
See section PHPExcel_Writer_CSV how to control the appearance of these.
2013-05-18 14:56:43 +00:00
### PDF
2013-05-13 12:59:53 +00:00
PHPExcel allows you to write a spreadsheet into PDF format, for fast distribution of represented data.
__PDF limitations__
2013-05-18 14:56:43 +00:00
Please note that PDF file format has some limits regarding to styling cells, number formatting, ...
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
#### PHPExcel_Writer_PDF
2013-05-13 12:59:53 +00:00
PHPExcel’ s PDF Writer is a wrapper for a 3rd-Party PDF Rendering library such as tcPDF, mPDF or DomPDF. Prior to version 1.7.8 of PHPExcel, the tcPDF library was bundled with PHPExcel; but from version 1.7.8 this was removed. Instead, you must now install a PDF Rendering library yourself; but PHPExcel will work with a number of different libraries.
Currently, the following libraries are supported:
2013-05-18 14:56:43 +00:00
Library | Version used for testing | Downloadable from | PHPExcel Internal Constant
--------|--------------------------|----------------------------------|----------------------------
tcPDF | 5.9 | http://www.tcpdf.org/ | PDF_RENDERER_TCPDF
mPDF | 5.4 | http://www.mpdf1.com/mpdf/ | PDF_RENDERER_MPDF
domPDF | 0.6.0 beta 3 | http://code.google.com/p/dompdf/ | PDF_RENDERER_DOMPDF
2013-05-13 12:59:53 +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.
Before instantiating a Writer to generate PDF output, you will need to indicate which Rendering library you are using, and where it is located.
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
$rendererLibrary = 'mPDF5.4';
$rendererLibraryPath = dirname(__FILE__).'/../../../libraries/PDF/' . $rendererLibrary;
if (!PHPExcel_Settings::setPdfRenderer(
2013-05-18 14:56:43 +00:00
$rendererName,
$rendererLibraryPath
)) {
die(
'Please set the $rendererName and $rendererLibraryPath values' .
PHP_EOL .
' as appropriate for your directory structure'
);
2013-05-13 12:59:53 +00:00
}
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Writing a spreadsheet
2013-05-13 12:59:53 +00:00
Once you have identified the Renderer that you wish to use for PDF generation, you can write a .pdf file using the following code:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter = new PHPExcel_Writer_PDF($objPHPExcel);
$objWriter->save("05featuredemo.pdf");
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
Please note that PHPExcel_Writer_PDF only outputs the first worksheet by default.
2013-05-18 14:56:43 +00:00
##### Write all worksheets
2013-05-13 12:59:53 +00:00
PDF files can contain one or more worksheets. If you want to write all sheets into a single PDF file, use the following code:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter->writeAllSheets();
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Write a specific worksheet
2013-05-13 12:59:53 +00:00
PDF files can contain one or more worksheets. Therefore, you can specify which sheet to write to PDF:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter->setSheetIndex(0);
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Formula pre-calculation
2013-05-13 12:59:53 +00:00
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:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objWriter = new PHPExcel_Writer_PDF($objPHPExcel);
$objWriter->setPreCalculateFormulas(false);
$objWriter->save("05featuredemo.pdf");
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
2013-05-18 14:56:43 +00:00
##### Decimal and thousands separators
2013-05-13 12:59:53 +00:00
See section PHPExcel_Writer_CSV how to control the appearance of these.
2013-05-18 14:56:43 +00:00
### Generating Excel files from templates (read, modify, write)
2013-05-13 12:59:53 +00:00
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:
2013-05-18 14:56:43 +00:00
```php
2013-05-13 12:59:53 +00:00
$objPHPexcel = PHPExcel_IOFactory::load('template.xlsx');
$objWorksheet = $objPHPexcel->getActiveSheet();
$objWorksheet->getCell('A1')->setValue('John');
$objWorksheet->getCell('A2')->setValue('Smith');
2016-10-06 11:49:41 +00:00
$objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel, 'Xls');
2013-05-13 12:59:53 +00:00
$objWriter->save('write.xls');
2013-05-18 14:56:43 +00:00
```
2013-05-13 12:59:53 +00:00
Notice that it is ok to load an xlsx file and generate an xls file.
[21]: http://pear.php.net/package/Spreadsheet_Excel_Writer
[22]: http://www.codeplex.com/PHPExcel/Wiki/View.aspx?title=Credits& referringTitle=Home