定制 atwx/silverstripe-excel-export 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

atwx/silverstripe-excel-export

Composer 安装命令:

composer require atwx/silverstripe-excel-export

包简介

Silverstripe module offering DataFormatters to export DataObjects in Excel format.

README 文档

README

This Silverstripe module makes it easy to export a set of Silverstripe DataObjects to:

  • Excel 2007 (XLSX)
  • Excel 5 (XLS)
  • CSV

This module is built by extending the standard SilverStripe DataFormatter.

Requirements

Installation

Install the module through composer:

composer require atwx/silverstripe-excel-export

Exporting your DataObjects

There's 3 ways you can export your data to a spread sheet.

Programmatically by calling the DataFormatter directly

3 DataFormatters are provided:

  • ExcelDataFormatter for XLSX
  • OldExcelDataFormatter for XLS
  • CsvDataFormatter for CSV

You can manually instantiate them to convert a list of DataObjects or a single DataObject.

$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.

You can also retrieve the underlying PHPExcel object and export your DataObject set to whatever format supported by PHPExcel.

// 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();

Add the GridFieldExcelExportButton to a GridField

The GridFieldExcelExportButton allows your CMS users to easily export the data from a GridField to a spreadsheet.

$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.

Unlike the SilverStripe GridFieldExportButton, the GridFieldExcelExportButton will export all the fields of the provided DataObjects ... not just the summary fields.

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')).

GridFieldExcelExportAction and GridFieldExcelExportButton can be used in conjunction if you want to give both options to your users.

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.

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.

$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.

This getExcelExportFields() method should return an array of fields following the same format used by DataObject::inheritedDatabaseFields():

return [
    'ID' => 'Int',
    'Name' => 'Varchar',
    'Address' => 'Text'
];

You may also reference relationships in this array or dynamic properties:

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.

This behavior can be overriden for specific instances of ExcelDataFormatter by calling the setCustomFields() method.

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).

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.

Change the default for all ExcelDataFormatter

In you YML config, you can use the following syntax to change the default headers.

ExcelDataFormatter:
  UseLabelsAsHeaders: true

Override the default for a specific instance

You may change the default behavior for a specific instance.

$formatter->setUseLabelsAsHeaders(true);

Thanks to

Thanks to Firebrand who originally developed this module. This version adds compatibility for silverstripe 6.

atwx/silverstripe-excel-export 适用场景与选型建议

atwx/silverstripe-excel-export 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 atwx/silverstripe-excel-export 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 atwx/silverstripe-excel-export 我们能提供哪些服务?
定制开发 / 二次开发

基于 atwx/silverstripe-excel-export 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-24