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

14 Commits

Author SHA1 Message Date
b3060667f2 1.3.12 font size 2023-03-31 17:22:19 +06:00
cb19defa37 1.3.12 font size 2023-03-31 17:16:07 +06:00
5a154b24f9 1.3.12 font size 2023-03-31 17:15:07 +06:00
b46f63fce7 Merge pull request #104 from mrjemson/master
Added Font-Size
2023-03-31 16:28:13 +06:00
bb09c0d38c Updated Readme 2023-03-31 17:05:44 +11:00
91ca3f6fff Added Font-Size Ability 2023-03-31 17:04:03 +11:00
59cdc277ee 1.3.11 2023-03-28 16:25:39 +06:00
343e549ca1 1.3.11 2023-03-28 16:21:08 +06:00
61e55d6277 Merge pull request #101 from xaviermdq/master
Frozen rows/cols support
2023-03-28 13:07:07 +06:00
9b52ff6ac7 Update SimpleXLSXGen.php 2023-03-27 20:18:39 -03:00
317fb0982e Frozen rows/cols support
Added setFrozen method to freeze rows and columns from top-left corner up to, but not including, the row and column of the passed parameter.
For example:
$xlsx->setFrozen('B3');
freezes first column (A) and first and second rows (1 and 2)
2023-03-27 11:17:11 -03:00
765001ae96 1.3.10 2023-02-25 19:09:48 +06:00
c2f3077812 1.3.10 2023-02-25 17:14:53 +06:00
af53c754c3 1.3.10 2023-01-09 11:51:48 +06:00
5 changed files with 131 additions and 22 deletions

View File

@ -1,5 +1,12 @@
# Changelog
## 1.3.12 (2023-03-31)
* ```<style font-size="32">Big Text</style>``` - font size in cells, thx [Andrew Robinson](https://github.com/mrjemson)
## 1.3.11 (2023-03-28)
* freezePanes( corner_cell ) - freezePanes to keep an area of a worksheet visible while you scroll, corner_cell is not included, thx [Javier](https://github.com/xaviermdq)
## 1.3.10 (2022-12-14)
* added borders ```<style border="medium">Black Border</style>``` see colored [examples](https://github.com/shuchkin/simplexlsxgen#formatting)
* added formulas ```<f v="100">SUM(B1:B10)</f>``` see [examples](https://github.com/shuchkin/simplexlsxgen#data-types)

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)
- CSV reader/writer [here](https://github.com/shuchkin/simplecsv)
**Sergey Shuchkin** <sergey.shuchkin@gmail.com> 2020-2022<br/>
**Sergey Shuchkin** <sergey.shuchkin@gmail.com> 2020-2023<br/>
*Hey, bro, please ★ the package for my motivation :) and [donate](https://opencollective.com/simplexlsx) for more motivation!*
@ -33,6 +33,7 @@ $ composer require shuchkin/simplexlsxgen
or download class [here](https://github.com/shuchkin/simplexlsxgen/blob/master/src/SimpleXLSXGen.php)
## Examples
Use UTF-8 encoded strings.
### Data types
```php
$data = [
@ -47,7 +48,7 @@ $data = [
['Date', '2020-05-20'],
['Time', '02:38:00'],
['Datetime PHP', new DateTime('2021-02-06 21:07:00')],
['String', 'Long UTF-8 String in autoresized column'],
['String', 'Very long UTF-8 string in autoresized column'],
['Formula', '<f v="135.35">SUM(B1:B2)</f>'],
['Hyperlink', 'https://github.com/shuchkin/simplexlsxgen'],
['Hyperlink + Anchor', '<a href="https://github.com/shuchkin/simplexlsxgen">SimpleXLSXGen</a>'],
@ -71,6 +72,7 @@ $data = [
['Italic + Hyperlink + Anchor', '<i><a href="https://github.com/shuchkin/simplexlsxgen">SimpleXLSXGen</a></i>'],
['Green', '<style color="#00FF00">12345.67</style>'],
['Bold Red Text', '<b><style color="#FF0000">12345.67</style></b>'],
['Size 32 Font', '<style font-size="32">Big Text</style>'],
['Blue Text and Yellow Fill', '<style bgcolor="#FFFF00" color="#0000FF">12345.67</style>'],
['Border color', '<style border="#000000">Black Thin Border</style>'],
['<top>Border style</top>','<style border="medium"><wraptext>none, thin, medium, dashed, dotted, thick, double, hair, mediumDashed, dashDot,mediumDashDot, dashDotDot, mediumDashDotDot, slantDashDot</wraptext></style>'],
@ -84,7 +86,7 @@ $data = [
['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],
['<top>Word wrap</top>', "<wraptext>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book</wraptext>"]
['<top>Word wrap</top>', "<wraptext>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book</wraptext>"],
];
SimpleXLSXGen::fromArray($data)
->setDefaultFont('Courier New')
@ -95,6 +97,21 @@ SimpleXLSXGen::fromArray($data)
```
![XLSX screenshot](styles.png)
### RAW Strings
Prefix #0 cell value (use double quotes).
```php
$PushkinDOB = '1799-07-06';
$data = [
['Datetime as raw string', "\0".'2023-01-09 11:16:34'],
['Date as raw string', "\0".$PushkinDOB],
['Disable type detection', "\0".'+12345'],
['Insert greater/less them simbols', "\0".'20- short term: <6 month'],
];
SimpleXLSXGen::fromArray($data)
->saveAs('test_rawstrings.xlsx');
```
### More examples
```php
// Fluid interface, output to browser for download
@ -117,6 +134,10 @@ exit();
// Autofilter
$xlsx->autoFilter('A1:B10');
// Freeze rows and columns from top-left corner up to, but not including,
// the row and column of the indicated cell
$xlsx->freezePanes('C3');
```
### JS array to Excel (AJAX)
```php

View File

@ -1,7 +1,7 @@
{
"name": "shuchkin/simplexlsxgen",
"description": "Export data to Excel XLSx file. PHP XLSX generator.",
"keywords": ["php", "excel", "xlsx", "generator", "creator", "backend"],
"keywords": ["php", "excel", "xlsx", "generator", "writer", "creator", "backend"],
"homepage": "https://github.com/shuchkin/simplexlsxgen",
"license": "MIT",
"authors": [

View File

@ -80,6 +80,7 @@ class SimpleXLSXGen
{
$this->curSheet = -1;
$this->defaultFont = 'Calibri';
$this->defaultFontSize = 10;
$this->sheets = [['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [], 'mergecells' => [], 'colwidth' => [], 'autofilter' => '']];
$this->extLinkId = 0;
$this->SI = []; // sharedStrings index
@ -111,9 +112,9 @@ class SimpleXLSXGen
];
$this->XF = [ // styles 0 - num fmt, 1 - align, 2 - font, 3 - fill, 4 - font color, 5 - bgcolor, 6 - border
[self::N_NORMAL, self::A_DEFAULT, self::F_NORMAL, self::FL_NONE, 0, 0, ''],
[self::N_NORMAL, self::A_DEFAULT, self::F_NORMAL, self::FL_GRAY_125, 0, 0, ''], // hack
$this->XF = [ // styles 0 - num fmt, 1 - align, 2 - font, 3 - fill, 4 - font color, 5 - bgcolor, 6 - border, 7 - font size
[self::N_NORMAL, self::A_DEFAULT, self::F_NORMAL, self::FL_NONE, 0, 0, '', 0],
[self::N_NORMAL, self::A_DEFAULT, self::F_NORMAL, self::FL_GRAY_125, 0, 0, '', 0], // hack
];
$this->XF_KEYS[implode('-', $this->XF[0])] = 0; // & keys
$this->XF_KEYS[implode('-', $this->XF[1])] = 1;
@ -215,7 +216,7 @@ class SimpleXLSXGen
}
}
$this->sheets[$this->curSheet] = ['name' => $name, 'hyperlinks' => [], 'mergecells' => [], 'colwidth' => [], 'autofilter' => ''];
$this->sheets[$this->curSheet] = ['name' => $name, 'hyperlinks' => [], 'mergecells' => [], 'colwidth' => [], 'autofilter' => '', 'frozen' => ''];
if (isset($rows[0]) && is_array($rows[0])) {
$this->sheets[$this->curSheet]['rows'] = $rows;
@ -385,16 +386,16 @@ class SimpleXLSXGen
}
foreach ($this->XF as $xf) {
// 0 - num fmt, 1 - align, 2 - font, 3 - fill, 4 - font color, 5 - bgcolor, 6 - border
// 0 - num fmt, 1 - align, 2 - font, 3 - fill, 4 - font color, 5 - bgcolor, 6 - border, 7 - font size
// fonts
$F_KEY = $xf[2] . '-' . $xf[4];
$F_KEY = $xf[2] . '-' . $xf[4].'-'.$xf[7];
if (isset($F_KEYS[$F_KEY])) {
$F_ID = $F_KEYS[$F_KEY];
} else {
$F_ID = $F_KEYS[$F_KEY] = count($FONTS);
$FONTS[] = '<font><name val="' . $this->defaultFont . '"/><family val="2"/>'
. ($this->defaultFontSize ? '<sz val="' . $this->defaultFontSize . '"/>' : '')
. ($xf[7] ? '<sz val="' . $xf[7] . '"/>' : '<sz val="' . $this->defaultFontSize . '"/>')
. ($xf[2] & self::F_BOLD ? '<b/>' : '')
. ($xf[2] & self::F_ITALIC ? '<i/>' : '')
. ($xf[2] & self::F_UNDERLINE ? '<u/>' : '')
@ -563,8 +564,6 @@ class SimpleXLSXGen
$lastmod_dateM = str_pad(decbin(date('m')), 4, '0', STR_PAD_LEFT);
$lastmod_dateY = str_pad(decbin(date('Y') - 1980), 7, '0', STR_PAD_LEFT);
# echo "ModTime: $lastmod_timeS-$lastmod_timeM-$lastmod_timeH (".date("s H H").")\n";
# echo "ModDate: $lastmod_dateD-$lastmod_dateM-$lastmod_dateY (".date("d m Y").")\n";
$e['modtime'] = bindec("$lastmod_timeH$lastmod_timeM$lastmod_timeS");
$e['moddate'] = bindec("$lastmod_dateY$lastmod_dateM$lastmod_dateD");
@ -617,7 +616,40 @@ class SimpleXLSXGen
setlocale(LC_NUMERIC, 'C');
$COLS = [];
$ROWS = [];
$SHEETVIEWS = '';
if (count($this->sheets[$idx]['rows'])) {
if ($this->sheets[$idx]['frozen'] !== '' || isset($this->sheets[$idx]['frozen'][0]) || isset($this->sheets[$idx]['frozen'][1])) {
$x = $y = 0;
if (is_string($this->sheets[$idx]['frozen'])) {
$cell = $this->sheets[$idx]['frozen'];
self::cell2coord($cell, $x, $y);
} else {
if (isset($this->sheets[$idx]['frozen'][0])) {
$x = $this->sheets[$idx]['frozen'][0];
}
if (isset($this->sheets[$idx]['frozen'][1])) {
$y = $this->sheets[$idx]['frozen'][1];
}
$cell = self::coord2cell($x, $y);
}
if ($x > 0 || $y > 0) {
$split = '';
if ($x > 0) {
$split .= ' xSplit="'.$x.'"';
}
if ($y > 0) {
$split .= ' ySplit="'.$y.'"';
}
$activepane = 'bottomRight';
if ($x > 0 && $y === 0) {
$activepane = 'topRight';
}
if ($x === 0 && $y > 0) {
$activepane = 'bottomLeft';
}
$SHEETVIEWS = '<sheetViews><sheetView tabSelected="1" workbookViewId="0"><pane'.$split.' topLeftCell="'.$cell.'" activePane="'.$activepane.'" state="frozen"/></sheetView></sheetViews>';
}
}
$COLS[] = '<cols>';
$CUR_ROW = 0;
$COL = [];
@ -639,7 +671,7 @@ class SimpleXLSXGen
}
$ct = $cv = $cf = null;
$N = $A = $F = $FL = $C = $BG = 0;
$N = $A = $F = $FL = $C = $BG = $FS = 0;
$BR = '';
if (is_string($v)) {
@ -687,6 +719,12 @@ class SimpleXLSXGen
$BR = $b;
}
}
if (preg_match('/ font-size="([^"]+)"/', $m[1], $m2)) {
$FS = (int)$m2[1];
if ($RH === 0) { // fix row height
$RH = ($FS > $this->defaultFontSize) ? round($FS * 1.50,1) : 0;
}
}
}
if (strpos($v, '<left>') !== false) {
$A += self::A_LEFT;
@ -837,7 +875,7 @@ class SimpleXLSXGen
$cs = 0;
if (($N + $A + $F + $FL > 0) || $BR !== '') {
if (($N + $A + $F + $FL + $FS > 0) || $BR !== '') {
if ($FL === self::FL_COLOR) {
$FL += self::FL_SOLID;
@ -847,15 +885,14 @@ class SimpleXLSXGen
$C = 'FF0563C1';
}
$XF_KEY = $N . '-' . $A . '-' . $F . '-' . $FL . '-' . $C . '-' . $BG . '-' . $BR;
// echo $cname .'='.$XF_KEY.PHP_EOL;
$XF_KEY = $N . '-' . $A . '-' . $F . '-' . $FL . '-' . $C . '-' . $BG . '-' . $BR . '-' . $FS;
if (isset($this->XF_KEYS[$XF_KEY])) {
$cs = $this->XF_KEYS[$XF_KEY];
}
if ($cs === 0) {
$cs = count($this->XF);
$this->XF_KEYS[$XF_KEY] = $cs;
$this->XF[] = [$N, $A, $F, $FL, $C, $BG, $BR];
$this->XF[] = [$N, $A, $F, $FL, $C, $BG, $BR, $FS];
}
}
@ -903,13 +940,15 @@ class SimpleXLSXGen
//restore locale
setlocale(LC_NUMERIC, $_loc);
return str_replace(['{REF}', '{COLS}', '{ROWS}', '{AUTOFILTER}', '{MERGECELLS}', '{HYPERLINKS}'],
[$REF,
return str_replace(['{REF}', '{COLS}', '{ROWS}', '{AUTOFILTER}', '{MERGECELLS}', '{HYPERLINKS}', '{SHEETVIEWS}'],
[
$REF,
implode("\r\n", $COLS),
implode("\r\n", $ROWS),
$AUTOFILTER,
implode("\r\n", $MERGECELLS),
implode("\r\n", $HYPERLINKS)
implode("\r\n", $HYPERLINKS),
$SHEETVIEWS
],
$template);
}
@ -1009,7 +1048,49 @@ class SimpleXLSXGen
public static function raw($value)
{
return "\0" . (string)$value;
return "\0" . $value;
}
public static function cell2coord($cell, &$x, &$y)
{
$x = $y = 0;
$lettercount = 0;
$cell = str_replace([' ', '\t', '\r', '\n', '\v', '\0'], '', $cell);
if (empty($cell)) {
return;
}
$cell = strtoupper($cell);
for ($i = 0, $len = strlen($cell); $i < $len; $i++) {
if ($cell[$i] >= 'A' && $cell[$i] <= 'Z') {
$lettercount++;
}
}
if ($lettercount > 0) {
$x = ord($cell[$lettercount - 1]) - ord('A');
$e = 1;
for ($i = $lettercount - 2 ; $i >= 0 ; $i--) {
$x += (ord($cell[$i]) - ord('A') + 1) * (26**$e);
$e++;
}
}
if ($lettercount < strlen($cell)) {
$y = ((int)substr($cell, $lettercount)) - 1;
}
}
public static function coord2cell($x, $y, $absolute = false)
{
$c = '';
for ($i = $x; $i >= 0; $i = ((int)($i / 26)) - 1) {
$c = chr(ord('A') + $i % 26) . $c;
}
return ($absolute ? '$' : '') . $c . $absolute . ($y+1);
}
public function freezePanes($cell)
{
$this->sheets[$this->curSheet]['frozen'] = $cell;
return $this;
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

After

Width:  |  Height:  |  Size: 190 KiB