1
0
mirror of https://github.com/shuchkin/simplexlsxgen.git synced 2023-08-10 21:12:59 +03:00

7 Commits

Author SHA1 Message Date
b63c66f8e9 1.2.13 2022-06-01 12:58:01 +06:00
1b5701a07f 1.2.13 2022-06-01 12:54:28 +06:00
199e5691e7 1.2.12 2022-05-17 12:10:24 +06:00
c54b314328 1.2.12 2022-05-17 12:06:30 +06:00
6db113f753 1.2.11 2022-05-17 12:03:35 +06:00
ff3f016358 1.2.11 2022-04-30 14:08:40 +06:00
bcd3586731 1.2.10 2022-04-30 14:04:13 +06:00
4 changed files with 56 additions and 17 deletions

View File

@ -1,9 +1,19 @@
# Changelog # Changelog
# 1.2.10 (2022-04-24) ## 1.2.13 (2022-06-01)
* Added colors `<style color="#FFFF00" bgcolor="#00FF00">Yellow text on blue background</style>`, thx [mrjemson](https://github.com/mrjemson) * setColWidth(num_col_started_1, size_in_chars) - set column width
# 1.1.12 (2022-03-15) ## 1.2.12 (2022-05-17)
* Vertical align (tags top,middle,bottom) `<bottom>12345</bottom>`
## 1.2.11 (2022-05-01)
* Row height `<style height="50">Custom row height 50</style>`
## 1.2.10 (2022-04-24)
* Added colors `<style color="#FFFF00" bgcolor="#00FF00">Yellow text on blue background</style>`, thx [mrjemson](https://github.com/mrjemson)
## 1.1.12 (2022-03-15)
* Added `$xlsx->mergeCells('A1:C1')` * Added `$xlsx->mergeCells('A1:C1')`
## 1.1.11 (2022-02-05) ## 1.1.11 (2022-02-05)

View File

@ -6,7 +6,7 @@ Export data to Excel XLSX file. PHP XLSX generator. No external tools and librar
- XLS reader [here](https://github.com/shuchkin/simplexls) - XLS reader [here](https://github.com/shuchkin/simplexls)
- CSV reader/writer [here](https://github.com/shuchkin/simplecsv) - CSV reader/writer [here](https://github.com/shuchkin/simplecsv)
**Sergey Shuchkin** <sergey.shuchkin@gmail.com> 2020-2021<br/> **Sergey Shuchkin** <sergey.shuchkin@gmail.com> 2020-2022<br/>
*Hey, bro, please ★ the package for my motivation :) and [donate](https://opencollective.com/simplexlsx) for more motivation!* *Hey, bro, please ★ the package for my motivation :) and [donate](https://opencollective.com/simplexlsx) for more motivation!*
@ -70,12 +70,17 @@ $data = [
['Center', '<center>12345.67</center>'], ['Center', '<center>12345.67</center>'],
['Right', '<right>Right Text</right>'], ['Right', '<right>Right Text</right>'],
['Center + Bold', '<center><b>Name</b></center>'], ['Center + Bold', '<center><b>Name</b></center>'],
['<center>MERGE CELLS</center>', null] ['Row height', '<style height="50">Row Height = 50</style>'],
['Top', '<style height="50"><top>Top</top></style>'],
['Middle + Center', '<style height="50"><middle><center>Middle + Center</center></middle></style>'],
['Bottom + Right', '<style height="50"><bottom><right>Bottom + Right</right></bottom></style>'],
['<center>MERGE CELLS MERGE CELLS MERGE CELLS MERGE CELLS MERGE CELLS</center>', null],
]; ];
SimpleXLSXGen::fromArray( $data ) SimpleXLSXGen::fromArray( $data )
->setDefaultFont( 'Courier New' ) ->setDefaultFont( 'Courier New' )
->setDefaultFontSize( 14 ) ->setDefaultFontSize( 14 )
->mergeCells('A16:B16') ->setColWidth(1, 35)
->mergeCells('A20:B20')
->saveAs('styles_and_tags.xlsx'); ->saveAs('styles_and_tags.xlsx');
``` ```
![XLSX screenshot](styles.png) ![XLSX screenshot](styles.png)

View File

@ -46,12 +46,15 @@ class SimpleXLSXGen {
const A_DEFAULT = 0; const A_DEFAULT = 0;
const A_LEFT = 1; const A_LEFT = 1;
const A_RIGHT = 2; const A_RIGHT = 2;
const A_CENTER = 3; const A_CENTER = 4;
const A_TOP = 8;
const A_MIDDLE = 16;
const A_BOTTOM = 32;
public function __construct() { public function __construct() {
$this->curSheet = -1; $this->curSheet = -1;
$this->defaultFont = 'Calibri'; $this->defaultFont = 'Calibri';
$this->sheets = [ ['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [], 'mergecells' => [] ] ]; $this->sheets = [ ['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [], 'mergecells' => [], 'colwidth' => [] ] ];
$this->SI = []; // sharedStrings index $this->SI = []; // sharedStrings index
$this->SI_KEYS = []; // & keys $this->SI_KEYS = []; // & keys
$this->XF = [ // styles $this->XF = [ // styles
@ -153,7 +156,7 @@ class SimpleXLSXGen {
} }
} }
$this->sheets[$this->curSheet] = ['name' => $name, 'hyperlinks' => [], 'mergecells' => []]; $this->sheets[$this->curSheet] = ['name' => $name, 'hyperlinks' => [], 'mergecells' => [], 'colwidth' => []];
if ( isset( $rows[0] ) && is_array($rows[0]) ) { if ( isset( $rows[0] ) && is_array($rows[0]) ) {
$this->sheets[$this->curSheet]['rows'] = $rows; $this->sheets[$this->curSheet]['rows'] = $rows;
@ -346,15 +349,18 @@ class SimpleXLSXGen {
.( $xf[3] & self::FL_COLOR ? '><fgColor rgb="'.$xf[5].'"/><bgColor indexed="64"/></patternFill>' : ' />') .( $xf[3] & self::FL_COLOR ? '><fgColor rgb="'.$xf[5].'"/><bgColor indexed="64"/></patternFill>' : ' />')
.'</fill>'; .'</fill>';
} }
$align = ($xf[1] === self::A_LEFT ? ' applyAlignment="1"><alignment horizontal="left"/>' : '') $align = ($xf[1] & self::A_LEFT ? ' horizontal="left"' : '')
.($xf[1] === self::A_RIGHT ? ' applyAlignment="1"><alignment horizontal="right"/>' : '') .($xf[1] & self::A_RIGHT ? ' horizontal="right"' : '')
.($xf[1] === self::A_CENTER ? ' applyAlignment="1"><alignment horizontal="center"/>' : ''); .($xf[1] & self::A_CENTER ? ' horizontal="center"' : '')
.($xf[1] & self::A_TOP ? ' vertical="top"' : '')
.($xf[1] & self::A_MIDDLE ? ' vertical="center"' : '')
.($xf[1] & self::A_BOTTOM ? ' vertical="bottom"' : '');
$XF[] = '<xf numFmtId="'.$xf[0].'" fontId="'.$F_ID.'" fillId="'.$FL_ID.'" borderId="0" xfId="0"' $XF[] = '<xf numFmtId="'.$xf[0].'" fontId="'.$F_ID.'" fillId="'.$FL_ID.'" borderId="0" xfId="0"'
.($xf[0] > 0 ? ' applyNumberFormat="1"' : '') .($xf[0] > 0 ? ' applyNumberFormat="1"' : '')
.($F_ID > 0 ? ' applyFont="1"' : '') .($F_ID > 0 ? ' applyFont="1"' : '')
.($FL_ID > 0 ? ' applyFill="1"' : '') .($FL_ID > 0 ? ' applyFill="1"' : '')
.($align ? $align . '</xf>' : '/>'); .($align ? ' applyAlignment="1"><alignment'.$align . '/></xf>' : '/>');
} }
// wrap collections // wrap collections
@ -481,8 +487,9 @@ class SimpleXLSXGen {
$COL = []; $COL = [];
foreach( $this->sheets[$idx]['rows'] as $r ) { foreach( $this->sheets[$idx]['rows'] as $r ) {
$CUR_ROW++; $CUR_ROW++;
$row = '<row r="'.$CUR_ROW.'">'; $row = '';
$CUR_COL = 0; $CUR_COL = 0;
$RH = 0; // row height
foreach( $r as $v ) { foreach( $r as $v ) {
$CUR_COL++; $CUR_COL++;
if ( !isset($COL[ $CUR_COL ])) { if ( !isset($COL[ $CUR_COL ])) {
@ -528,6 +535,9 @@ class SimpleXLSXGen {
$FL += self::FL_COLOR; $FL += self::FL_COLOR;
$B = strlen($m2[1]) === 8 ? $m2[1] : ('FF' . ltrim($m2[1],'#')); $B = strlen($m2[1]) === 8 ? $m2[1] : ('FF' . ltrim($m2[1],'#'));
} }
if ( preg_match('/ height="([^"]+)"/', $m[1], $m2) ) {
$RH = $m2[1];
}
} }
if ( strpos( $v, '<left>' ) !== false ) { if ( strpos( $v, '<left>' ) !== false ) {
$A += self::A_LEFT; $A += self::A_LEFT;
@ -538,6 +548,15 @@ class SimpleXLSXGen {
if ( strpos( $v, '<right>' ) !== false ) { if ( strpos( $v, '<right>' ) !== false ) {
$A += self::A_RIGHT; $A += self::A_RIGHT;
} }
if ( strpos( $v, '<top>' ) !== false ) {
$A += self::A_TOP;
}
if ( strpos( $v, '<middle>' ) !== false ) {
$A += self::A_MIDDLE;
}
if ( strpos( $v, '<bottom>' ) !== false ) {
$A += self::A_BOTTOM;
}
if ( preg_match( '/<a href="(https?:\/\/[^"]+)">(.*?)<\/a>/i', $v, $m ) ) { if ( preg_match( '/<a href="(https?:\/\/[^"]+)">(.*?)<\/a>/i', $v, $m ) ) {
$h = explode( '#', $m[1] ); $h = explode( '#', $m[1] );
$this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => $h[0], 'L' => isset( $h[1] ) ? $h[1] : '']; $this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => $h[0], 'L' => isset( $h[1] ) ? $h[1] : ''];
@ -582,7 +601,7 @@ class SimpleXLSXGen {
$cv = $this->date2excel( $m[3], $m[2], $m[1], $m[4], $m[5], $m[6] ); $cv = $this->date2excel( $m[3], $m[2], $m[1], $m[4], $m[5], $m[6] );
$N = self::N_DATETIME; // [22] m/d/yy h:mm $N = self::N_DATETIME; // [22] m/d/yy h:mm
} elseif ( preg_match( '/^[0-9+-.]+$/', $v ) ) { // Long ? } elseif ( preg_match( '/^[0-9+-.]+$/', $v ) ) { // Long ?
$A += self::A_RIGHT; $A += ($A & (self::A_LEFT | self::A_CENTER)) ? 0 : self::A_RIGHT;
} elseif ( preg_match( '/^https?:\/\/\S+$/i', $v ) ) { } elseif ( preg_match( '/^https?:\/\/\S+$/i', $v ) ) {
$h = explode( '#', $v ); $h = explode( '#', $v );
$this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => $h[0], 'L' => isset( $h[1] ) ? $h[1] : '']; $this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => $h[0], 'L' => isset( $h[1] ) ? $h[1] : ''];
@ -660,10 +679,11 @@ class SimpleXLSXGen {
$row .= '<c r="' . $cname . '"' . ($ct ? ' t="' . $ct . '"' : '') . ($cs ? ' s="' . $cs . '"' : '') . '>' $row .= '<c r="' . $cname . '"' . ($ct ? ' t="' . $ct . '"' : '') . ($cs ? ' s="' . $cs . '"' : '') . '>'
. ($ct === 'inlineStr' ? '<is><t>' . $cv . '</t></is>' : '<v>' . $cv . '</v>') . "</c>\r\n"; . ($ct === 'inlineStr' ? '<is><t>' . $cv . '</t></is>' : '<v>' . $cv . '</v>') . "</c>\r\n";
} }
$ROWS[] = $row . "</row>\r\n"; $ROWS[] = '<row r="'.$CUR_ROW.'"'.($RH ? ' customHeight="1" ht="'.$RH.'"' : '').'>'.$row . "</row>";
} }
foreach ( $COL as $k => $max ) { foreach ( $COL as $k => $max ) {
$COLS[] = '<col min="'.$k.'" max="'.$k.'" width="'.min( $max+1, 60).'" />'; $w = isset($this->sheets[$idx]['colwidth'][$k]) ? $this->sheets[$idx]['colwidth'][$k] : min( $max+1, 60);
$COLS[] = '<col min="'.$k.'" max="'.$k.'" width="'.$w.'" />';
} }
$COLS[] = '</cols>'; $COLS[] = '</cols>';
$REF = 'A1:'.$this->num2name(count($COL)) . $CUR_ROW; $REF = 'A1:'.$this->num2name(count($COL)) . $CUR_ROW;
@ -747,6 +767,10 @@ class SimpleXLSXGen {
$this->sheets[$this->curSheet]['mergecells'][] = $range; $this->sheets[$this->curSheet]['mergecells'][] = $range;
return $this; return $this;
} }
public function setColWidth($col, $width) {
$this->sheets[$this->curSheet]['colwidth'][$col] = $width;
return $this;
}
public function esc( $str ) { public function esc( $str ) {
// XML UTF-8: #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] // XML UTF-8: #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
// but we use fast version // but we use fast version

Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 KiB

After

Width:  |  Height:  |  Size: 93 KiB