XlsxExporter.php in Webform XLSX export 8
File
src/Plugin/WebformExporter/XlsxExporter.php
View source
<?php
namespace Drupal\webform_xlsx_export\Plugin\WebformExporter;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Drupal\webform\Plugin\WebformExporter\TabularBaseWebformExporter;
use Drupal\webform\WebformSubmissionInterface;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class XlsxExporter extends TabularBaseWebformExporter {
private $xls;
public function getFileExtension() {
return 'xlsx';
}
public function createExport() {
$this->xls = new Spreadsheet();
}
public function openExport() {
$this->xls = IOFactory::load($this
->getExportFilePath());
}
public function closeExport() {
IOFactory::createWriter($this->xls, "Xlsx")
->save($this
->getExportFilePath());
}
public function writeHeader() {
$sheet = $this->xls
->getActiveSheet();
foreach ($this
->buildHeader() as $column => $header) {
$sheet
->setCellValueByColumnAndRow($column + 1, 1, $header);
}
$sheet
->getStyle('A1:' . $sheet
->getHighestColumn() . '1')
->getFont()
->setBold(TRUE);
}
public function writeSubmission(WebformSubmissionInterface $webform_submission) {
$sheet = $this->xls
->getActiveSheet();
$row = $sheet
->getHighestRow();
foreach ($this
->buildRecord($webform_submission) as $column => $record) {
$sheet
->setCellValueByColumnAndRow($column + 1, $row + 1, $record);
}
}
}