You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To write the Excel file, we use the excellent [PHPExcel](https://github.com/PHPOffice/PHPExcel) package.
19
18
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
21
26
22
27
```php
23
28
<?php
@@ -33,16 +38,53 @@ $file = \Yii::createObject([
33
38
$file->send('user.xlsx');
34
39
```
35
40
36
-
## Installation
41
+
Find more examples below.
37
42
38
-
Install the package with [composer](http://getcomposer.org):
39
43
40
-
composer require codemix/yii2-excelexport *
44
+
## Configuration and Use
41
45
46
+
### ExcelFile
42
47
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
44
72
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
46
88
47
89
### ActiveQuery results
48
90
@@ -51,8 +93,7 @@ Instead of a full documentation we better show you some examples. You should be
51
93
$file = \Yii::createObject([
52
94
'class' => 'codemix\excelexport\ExcelFile',
53
95
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`
56
97
57
98
'sheets' => [
58
99
@@ -74,12 +115,6 @@ $file = \Yii::createObject([
74
115
'titles' => [
75
116
'D' => 'Team Name',
76
117
],
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.
83
118
],
84
119
85
120
],
@@ -95,8 +130,7 @@ $file = \Yii::createObject([
95
130
'class' => 'codemix\excelexport\ExcelFile',
96
131
'sheets' => [
97
132
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
0 commit comments