Skip to content

Commit 0c180db

Browse files
committed
Update README
1 parent a3f4f1e commit 0c180db

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

README.md

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ Yii2 Excel Export
1414
* Create excel files with multiple sheets
1515
* Format cells and values
1616

17-
> **Note:** To write the Excel file, we use the excellent
18-
> [PHPExcel](https://github.com/PHPOffice/PHPExcel) package.
17+
To write the Excel file, we use the excellent [PHPExcel](https://github.com/PHPOffice/PHPExcel) package.
1918

20-
Here's a quick example to get you started:
19+
## Installation
20+
21+
Install the package with [composer](http://getcomposer.org):
22+
23+
composer require codemix/yii2-excelexport *
24+
25+
## Quickstart example
2126

2227
```php
2328
<?php
@@ -33,16 +38,53 @@ $file = \Yii::createObject([
3338
$file->send('user.xlsx');
3439
```
3540

36-
## Installation
41+
Find more examples below.
3742

38-
Install the package with [composer](http://getcomposer.org):
3943

40-
composer require codemix/yii2-excelexport *
44+
## Configuration and Use
4145

46+
### ExcelFile
4247

43-
## Examples
48+
Property | Description
49+
---------|-------------
50+
`writer` | The file format as supported by PHPOffice. The default is ` '\PHPExcel_Writer_Excel2007'`
51+
`sheets` | An array of sheet configurations (see below). The keys are used as sheet names.
52+
53+
Methods | Description
54+
---------|-------------
55+
`saveAs($name)` | Saves the excel file under `$name`
56+
`send($name=null,$inline=false)` | Sends the excel file to the browser. If `$name` is empty, the file is streamed for inline display, otherwhise a download dialog will open, unless `$inline` is `true` which will force inline display even if a filename is supplied.
57+
`getWorkbook()` | Returns the `PHPExcel` workbook instance
58+
`getTmpFile()` | Returns the `mikehaertl\tmp\File` instance of the temporary file
59+
60+
### ExcelSheet
61+
62+
Property | Description
63+
---------|-------------
64+
`data` | An array of data rows that should be used as sheet content
65+
`titles` (optional) | An array of column titles
66+
`types` (optional) | An array of types for specific columns as supported by PHPOffice, e.g. `\PHPExcel_Cell_DataType::TYPE_STRING`, indexed either by column name (e.g. `H`) or 0-based column index.
67+
`formats` (optional) | An array of format strings for specific columns as supported by Excel, e.g. `#,##0.00`, indexed either by column name (e.g. `H`) or 0-based column index.
68+
`formatters` (optional) | An array of value formatters for specific columns. Each must be a valid PHP callable whith the signature `function formatter($value, $row, $data)` where `$value` is the cell value to format, `$row` is the 0-based row index and `$data` is the current row data from the `data` configuration. The callbacks must be indexed either by column name (e.g. `H`) or by the 0-based column index.
69+
`callbacks` (optional) | An array of callbacks for specific columns that should be called after rendering a cell, e.g. to apply some styling. Each must be a valid PHP callable with the signature `function callback($cell, $col, $row)` where `$cell` is the current `PHPExcel_Cell` object and `$col` and `$row` are the 0-based column and row indices respectively.
70+
71+
### ActiveExcelSheet
4472

45-
Instead of a full documentation we better show you some examples. You should be able to take it from there.
73+
The class extends from `ExcelSheet` but differs in the following properties:
74+
75+
Property | Description
76+
---------|-------------
77+
`query` | The `ActiveQuery` for the row data (the `data` property will be ignored).
78+
`data` | The read-only property that returns the batched query result.
79+
`attributes` (optional) | The attributes to use as columns. Related attributes can be specifed in dot notation as usual, e.g. `team.name`. If not set, the `attributes()` from the corresponding `ActiveRecord` class will be used.
80+
`titles` (optional) | The column titles, indexed by column name (e.g. `H`) or 0-based column index. If a column is not listed here, the respective attribute label will be used. If set to `false` no title row will be rendered.
81+
`formats` (optional) | As in `ExcelSheet` but for `date`, `datetime` and `decimal` DB columns, the respective formats will be automatically set by default, according to the respective date format properties (see below) and the decimal precision.
82+
`formatters` (optional) | As in `ExcelSheet` but for `date` and `datetime` columns the value will be autoconverted to the correct excel time format with `\PHPExcel_Shared_Date::PHPToExcel()` by default.
83+
`dateFormat` | The excel format to use for `date` DB types. Default is `dd/mm/yyyy`.
84+
`dateTimeFormat` | The excel format to use for `datetime` DB types. Default is `dd/mm/yyyy hh:mm:ss`.
85+
`batchSize` | The query batchsize to use. Default is `100`.
86+
87+
## Examples
4688

4789
### ActiveQuery results
4890

@@ -51,8 +93,7 @@ Instead of a full documentation we better show you some examples. You should be
5193
$file = \Yii::createObject([
5294
'class' => 'codemix\excelexport\ExcelFile',
5395

54-
// Default writer is `\PHPExcel_Writer_Excel2007`. You can use any other writer available from PHPOffice.
55-
//'writer' => '\PHPExcel_Writer_Excel5',
96+
'writer' => '\PHPExcel_Writer_Excel5', // Override default of `\PHPExcel_Writer_Excel2007`
5697

5798
'sheets' => [
5899

@@ -74,12 +115,6 @@ $file = \Yii::createObject([
74115
'titles' => [
75116
'D' => 'Team Name',
76117
],
77-
78-
// 'formats' and 'formatters' are autogenerated for `ActiveExcelSheet`.
79-
// For example if `created_at` is a `DATETIME` column in the DB, the
80-
// column will be formatted as a date. You could still override formats
81-
// here though, for example if you save a timestamp as INT. See the
82-
// raw data example below.
83118
],
84119

85120
],
@@ -95,8 +130,7 @@ $file = \Yii::createObject([
95130
'class' => 'codemix\excelexport\ExcelFile',
96131
'sheets' => [
97132

98-
// Array keys are used as Sheet names in the final excel file
99-
'Result per Country' => [
133+
'Result per Country' => [ // Name of the excel sheet
100134
'data' => [
101135
['fr', 'France', 1.234, '2014-02-03 12:13:14'],
102136
['de', 'Germany', 2.345, '2014-02-05 19:18:39'],
@@ -111,14 +145,12 @@ $file = \Yii::createObject([
111145
'Created At',
112146
],
113147

114-
// Excel format strings
115148
'formats' => [
149+
// Either column name or 0-based column index can be used
116150
'C' => '#,##0.00',
117-
// Instead of column names, the 0-based column index can be also used
118151
3 => 'dd/mm/yyyy hh:mm:ss',
119152
],
120153

121-
// You'll usually only need this for date columns
122154
'formatters' => [
123155
// Dates and datetimes must be converted to Excel format
124156
3 => function ($value, $row, $data) {

0 commit comments

Comments
 (0)