Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions testbench/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
$data2 = array(
array('2003','01','343.12'),
array('2003','02','345.12'),
array('2003','02', ['Vencedor', 'SaoPaulo'], ['Perdedor', 'Flamengo']),
);
$writer = new XLSXWriter();
$writer->setAuthor('Some Author');
$writer->addStyle('SaoPaulo', 'FFFFFF', 'FF0000', true);
$writer->addStyle('Flamengo', 'FF0000', '000000', false);
$writer->writeSheet($data1,'Sheet1',$header);
$writer->writeSheet($data2,'Sheet2');
$writer->writeToFile('test.xlsx');
Expand Down
14 changes: 14 additions & 0 deletions testbench/xlsxwriter.class.Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ public function testWriteToFile()
$this->assertEmpty($r2);
}

public function testCustomStyle()
{
$filename = tempnam("/tmp", "xlsx_writer");
$file_writer = new XLSXWriter_BuffererWriter($filename);

$xlsx_writer = new _XLSXWriter_();
$xlsx_writer->addStyle('test', 'FFFFFF', 'FFFFFF');
$xlsx_writer->writeCell($file_writer, 0, 0, '0123', 'test');
$file_writer->close();
$cell_xml = file_get_contents($filename);
$this->assertEquals('<c r="A1" s="7" t="s"><v>0</v></c>', $cell_xml);
@unlink($filename);
}

private function stripCellsFromSheetXML($sheet_xml)
{
$output=array();
Expand Down
36 changes: 32 additions & 4 deletions xlsxwriter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class XLSXWriter

protected $current_sheet = '';

protected $fonts = [];
protected $fills = [];
protected $styles = [];

public function __construct()
{
if(!ini_get('date.timezone'))
Expand All @@ -31,6 +35,21 @@ public function __construct()

public function setAuthor($author='') { $this->author=$author; }

public function addStyle(string $name, string $font_color, string $background_color, bool $bold = false): void {
$this->fonts[] = [
$font_color,
$bold
];
$this->fills[] = [
$background_color
];
$this->styles[$name] = [
count($this->fonts) + 5,
count($this->fills) + 3,
7 + count($this->styles),
];
}

public function __destruct()
{
if (!empty($this->temp_files)) {
Expand Down Expand Up @@ -279,7 +298,7 @@ protected function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $col
'blackheader' => 6
);
$cell = self::xlsCell($row_number, $column_number);
$s = isset($styles[$cell_format]) ? $styles[$cell_format] : '0';
$s = $styles[$cell_format] ?? $this->styles[$cell_format][2] ?? '0';

if (!is_scalar($value) || $value==='') { //objects, array, empty
$file->write('<c r="'.$cell.'" s="'.$s.'"/>');
Expand Down Expand Up @@ -314,17 +333,23 @@ protected function writeStylesXML()
$file->write( '<numFmt formatCode="#,##0.00" numFmtId="168"/>');
$file->write( '<numFmt formatCode="0.0000" numFmtId="169"/>');
$file->write('</numFmts>');
$file->write('<fonts count="5">');
$file->write('<fonts count="'. (5 + count($this->fonts)) . '">');
$file->write( '<font><name val="Arial"/><charset val="1"/><family val="2"/><sz val="10"/></font>');
$file->write( '<font><name val="Arial"/><family val="0"/><sz val="10"/></font>');
$file->write( '<font><name val="Arial"/><family val="0"/><sz val="10"/></font>');
$file->write( '<font><name val="Arial"/><family val="0"/><sz val="10"/></font>');
$file->write( '<font><name val="Arial"/><b val="true"/><charset val="1"/><family val="2"/><sz val="10"/></font>');
foreach($this->fonts as [$color, $bold]) {
$file->write( '<font><name val="Arial"/><b val="'. ($bold ? 'true' : 'false') . '"/><charset val="1"/><family val="2"/><sz val="10"/><color rgb="'. $color . '"/></font>');
}
$file->write('</fonts>');
$file->write('<fills count="3">');
$file->write('<fills count="'. (3 + count($this->fills)) .'">');
$file->write(' <fill><patternFill patternType="none"/></fill>');
$file->write(' <fill><patternFill patternType="gray125"/></fill>');
$file->write(' <fill><patternFill patternType="solid"><fgColor theme="0" tint="-0.249977111117893"/><bgColor rgb="FF003300"/></patternFill></fill>');
foreach($this->fills as [$color]) {
$file->write(' <fill><patternFill patternType="solid"><fgColor rgb="'. $color . '"/></patternFill></fill>');
}
$file->write('</fills>');
$file->write('<borders count="1"><border diagonalDown="false" diagonalUp="false"><left/><right/><top/><bottom/><diagonal/></border></borders>');
$file->write( '<cellStyleXfs count="20">');
Expand Down Expand Up @@ -352,14 +377,17 @@ protected function writeStylesXML()
$file->write( '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="42"/>');
$file->write( '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="9"/>');
$file->write( '</cellStyleXfs>');
$file->write( '<cellXfs count="7">');
$file->write( '<cellXfs count="'. (7 + count($this->styles)) . '">');
$file->write( '<xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="164" xfId="0"/>');
$file->write( '<xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="165" xfId="0"/>');
$file->write( '<xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="166" xfId="0"/>');
$file->write( '<xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="167" xfId="0"/>');
$file->write( '<xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="168" xfId="0"/>');
$file->write( '<xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="169" xfId="0"/>');
$file->write( '<xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="2" fontId="4" numFmtId="164" xfId="0"/>');
foreach($this->styles as [$font_id, $fill_id, $_]) {
$file->write( '<xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="'. ($fill_id - 1) . '" fontId="'. ($font_id - 1) .'" numFmtId="164" xfId="0"/>');
}
$file->write( '</cellXfs>');
$file->write( '<cellStyles count="6">');
$file->write( '<cellStyle builtinId="0" customBuiltin="false" name="Normal" xfId="0"/>');
Expand Down