Skip to content

Instantly share code, notes, and snippets.

@madflow
Created August 6, 2016 14:05
Show Gist options
  • Save madflow/d134d476f396a7f0753f2bee327008b9 to your computer and use it in GitHub Desktop.
Save madflow/d134d476f396a7f0753f2bee327008b9 to your computer and use it in GitHub Desktop.
Test BG colors in spout
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Box\Spout\Writer\Style\StyleBuilder;
use Box\Spout\Writer\WriterFactory;
use Box\Spout\Common\Type;
use Box\Spout\Writer\Style\Color;
use Box\Spout\Reader\Wrapper\XMLReader;
$file = __DIR__ . '/bg.xlsx';
$writer = WriterFactory::create(Type::XLSX);
$writer->openToFile($file);
$styleRed = (new StyleBuilder())->setBackgroundColor(Color::RED)->build();
$styleGreen = (new StyleBuilder())->setBackgroundColor(Color::GREEN)->build();
$styleFont = (new StyleBuilder())->setFontBold()->setFontSize(16)->build();
$writer->addRow(['PLAIN']);
$writer->addRowWithStyle(['RED'], $styleRed);
$writer->addRowWithStyle(['GREEN'], $styleGreen);
$writer->addRow(['PLAIN']);
$writer->addRowWithStyle(['Font Styled'], $styleFont);
$writer->addRow(['PLAIN']);
$writer->addRow(['PLAIN']);
$writer->addRow(['PLAIN']);
$writer->addRow(['PLAIN']);
$writer->addRowWithStyle(['RED'], $styleRed);
$writer->addRowWithStyle(['GREEN'], $styleGreen);
$writer->addRowWithStyle(['RED'], $styleRed);
$writer->close();
$xmlReader = new XMLReader();
$path = $xmlReader->getRealPathURIForFileInZip($file, 'xl/styles.xml');
$xml = DOMDocument::load($path);
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
echo $xml->saveXML();
$xmlReader->close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment