VboExportXlsx.php in VBO export 8.3
File
src/Plugin/Action/VboExportXlsx.php
View source
<?php
namespace Drupal\vbo_export\Plugin\Action;
use Drupal\Core\Session\AccountProxyInterface;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Symfony\Component\DependencyInjection\ContainerInterface;
class VboExportXlsx extends VboExportBase {
const EXTENSION = 'xlsx';
protected $currentUser;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->currentUser = $container
->get('current_user');
return $instance;
}
protected function generateOutput() {
$config = $this->configuration;
$header = $this->context['sandbox']['header'];
$rows = $this
->getCurrentRows();
$current_user = $this->currentUser;
if (!_vbo_export_library_exists(Spreadsheet::class)) {
$this
->messenger()
->addError('PhpSpreadsheet library not installed.');
return '';
}
$spreadsheet = new Spreadsheet();
$spreadsheet
->removeSheetByIndex(0);
$spreadsheet
->getProperties()
->setCreated($this->time
->getRequestTime())
->setCreator($current_user
->getDisplayName())
->setTitle('VBO Export - ' . date('d-m-Y H:i', $this->time
->getRequestTime()))
->setLastModifiedBy($current_user
->getDisplayName());
$worksheet = $spreadsheet
->createSheet();
$worksheet
->setTitle((string) t('Export'));
$col_index = 1;
foreach ($header as $label) {
if ($config['strip_tags']) {
$label = strip_tags($label);
}
$worksheet
->setCellValueExplicitByColumnAndRow($col_index++, 1, trim($label), DataType::TYPE_STRING);
}
foreach ($rows as $row_index => $row) {
$col_index = 1;
foreach ($row as $cell) {
if ($config['strip_tags']) {
$cell = strip_tags($cell);
}
$worksheet
->setCellValueExplicitByColumnAndRow($col_index++, $row_index + 2, trim($cell), DataType::TYPE_STRING);
}
unset($rows[$row_index]);
}
$spreadsheet
->getDefaultStyle()
->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_LEFT);
$last_column = $worksheet
->getHighestColumn();
$last_column_index = Coordinate::columnIndexFromString($last_column);
$first_row_range = 'A1:' . $last_column . '1';
$worksheet
->getStyle($first_row_range)
->getFont()
->setBold(TRUE);
$worksheet
->setAutoFilter($first_row_range);
$full_range = 'A1:' . $last_column . $worksheet
->getHighestRow();
$worksheet
->getStyle($full_range)
->getAlignment()
->setWrapText(TRUE)
->setVertical(Alignment::VERTICAL_TOP);
for ($column = 0; $column <= $last_column_index; $column++) {
$worksheet
->getColumnDimensionByColumn($column)
->setAutoSize(TRUE);
}
$min_column_width = 15;
$max_column_width = 85;
try {
$worksheet
->calculateColumnWidths();
} catch (Exception $e) {
}
for ($column = 0; $column <= $last_column_index; $column++) {
$width = $worksheet
->getColumnDimensionByColumn($column)
->getWidth();
if ($width < $min_column_width) {
$worksheet
->getColumnDimensionByColumn($column)
->setAutoSize(FALSE);
$worksheet
->getColumnDimensionByColumn($column)
->setWidth($min_column_width);
}
elseif ($width > $max_column_width) {
$worksheet
->getColumnDimensionByColumn($column)
->setAutoSize(FALSE);
$worksheet
->getColumnDimensionByColumn($column)
->setWidth($max_column_width);
}
}
$objWriter = new Xlsx($spreadsheet);
ob_start();
$objWriter
->save('php://output');
return ob_get_clean();
}
}