Silverstripe Excel Export module
This Silverstripe module makes it easy to export a set of Silverstripe DataObjects to:
* Excel 2007 (XLSX)
* Excel 5 (XLS)
* CSV, (*1)
This module is built by extending the standard SilverStripe DataFormatter., (*2)
Requirements
Suggestions
Installation
Install the module through composer:, (*3)
composer require firebrandhq/silverstripe-excel-export
Exporting your DataObjects
There's 3 ways you can export your data to a spread sheet., (*4)
3 DataFormatters are provided:
* ExcelDataFormatter for XLSX
* OldExcelDataFormatter for XLS
* CsvDataFormatter for CSV, (*5)
You can manually instantiate them to convert a list of DataObjects or a single DataObject., (*6)
$formatter = new ExcelDataFormatter();
// Will return an Excel Spreadsheet as a string for a single user
$filedata = $formatter->convertDataObject($user);
// Will return an Excel Spreadsheet as a string for a list of user
$filedata = $formatter->convertDataObjectSet(Member::get());
convertDataObjectSet()
and convertDataObject()
will automatically set the Content-Type HTTP header to an appropriate Mime Type., (*7)
You can also retrieve the underlying PHPExcel object and export your DataObject set to whatever format supported by PHPExcel., (*8)
// Get your Data
$formatter = new ExcelDataFormatter();
$excel = $formatter->getPhpExcelObject(SiteTree::get());
// Set up a writer
$writer = PHPExcel_IOFactory::createWriter($excel, 'HTML');
// Save the file somewhere on the server
$writer->save('/tmp/sitetree_list.html');
// Output the results back to the browser
$writer->save('php://output');
// Output the file to a variable
ob_start();
$writer->save('php://output');
$fileData = ob_get_clean();
The GridFieldExcelExportButton
allows your CMS users to easily export the data from a GridField to a spreadsheet., (*9)
$rowEntryConfig = GridFieldConfig_RecordEditor::create();
$rowEntryConfig->addComponent(new GridFieldExcelExportButton());
$rowEntryDataGridField = new GridField(
"ContentRow",
"Content Row Entry",
$this->ContentRow(),
$rowEntryConfig
);
$fields->addFieldToTab('Root.Main', $rowEntryDataGridField);
The above code snippet will display a split button allowing the user to export the GridField list to the format of their choice., (*10)
Unlike the SilverStripe GridFieldExportButton, the GridFieldExcelExportButton
will export all the fields of the provided DataObjects ... not just the summary fields., (*11)
You can also use the GridFieldExcelExportAction
component. This button is added to each row and allows you to export individual records one at a time. Out of the box, GridFieldExcelExportAction
will export to xlsx, but you can get it to export to xls or _csv_ (e.g.: new GridFieldExcelExportAction('csv')
)., (*12)
GridFieldExcelExportAction
and GridFieldExcelExportButton
can be used in conjunction if you want to give both options to your users., (*13)
Call via the SilverStripe RestfulServer Module
The SilverStripe RestfulServer Module allows you to turn any SilverStripe website into a RESTFul Server., (*14)
If you use the SilverStripe RestfulServer Module in conjunction with the Silverstripe Excel Export module, you'll be able to dynamically export any DataObject set just by entering the right URL in your browser., (*15)
Access control
Obviously, you don't want everyone to be able to download any data off your website. The SilverStripe RestfulServer Module will only return results for DataObject with the $api_access
property set., (*16)
private static $api_access = true;
Additionally, access to individual DataObjects is controlled by the canView
function., (*17)
Configuration the SilverStripe RestfulServer Module , (*18)
Getting to the data
Exporting your data is just as easy as entering a URL.
* Get a list of all Pages in Excel 2007: http://localhost/api/v1/Page.xlsx
* Get a list of all Pages in Excel 5: http://localhost/api/v1/Page.xls
* Get a list of all Pages in CSV: http://localhost/api/v1/Page.csv
* Limit the list to 10 results: http://localhost/api/v1/Page.csv?limit=10
* Return a single record: http://localhost/api/v1/Page/37.xlsx
* Drill down into relationships: http://localhost/api/v1/Tag/127/Articles.xlsx, (*19)
SilverStripe RestfulServer Module Supported operations, (*20)
Customising the output
There's 2 ways you can control the output:
* Choose which fields to output ;
* Choose to use field label instead of fields names in the headers., (*21)
Choose which fields to output
Because the ExcelDataFormatter
extends DataFormatter, you can use methods like setCustomFields()
, setCustomAddFields()
or setRemoveFields()
to control what fields will be present in the spread sheet., (*22)
$formatter = new ExcelDataFormatter();
// This formatter instead of returning every field of a DataObject, will only return 3 fields.
$formatter->setCustomFields(['ID', 'Title', 'LastEdited']);
// If youe DataObject has dynamic properties, you can reference them using setCustomAddFields().
$formatter->setCustomAddFields(['ChildrenCount']);
Defining a default column set
You can customise the default column set that will be return for a specific DataObject class by defining a getExcelExportFields()
method on your DataOject class., (*23)
This getExcelExportFields()
method should return an array of fields following the same format used by DataObject::inheritedDatabaseFields()
:, (*24)
return [
'ID' => 'Int',
'Name' => 'Varchar',
'Address' => 'Text'
];
You may also reference relationships in this array or dynamic properties:, (*25)
return [
'Owner.Name' => 'Varchar',
'Category.Title' => 'Varchar',
'ChildrenCount' => 'Int',
];
This will also allow you to control the order the fields appear in the Spread Sheet. Note that ID will always be the first field and cannot be removed., (*26)
This behavior can be overriden for specific instances of ExcelDataFormatter
by calling the setCustomFields()
method., (*27)
Use field labels or field names as column headers
Out of the box, the actual field names will be used as column header. (e.g.: FirstName
rather than First Name
)., (*28)
You can customise this behavior and use the Field Labels as define on your DataObject class instead. When generating the header row, ExcelDataFormatter
will call the fieldLabel()
method on your Data Object to decide what string to use in each header., (*29)
In you YML config, you can use the following syntax to change the default headers., (*30)
ExcelDataFormatter:
UseLabelsAsHeaders: true
Override the default for a specific instance
You may change the default behavior for a specific instance., (*31)
$formatter->setUseLabelsAsHeaders(true);