mirror of
https://github.com/shuchkin/simplexlsxgen.git
synced 2023-08-10 21:12:59 +03:00
Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
e02d764d0d | |||
f65e57c735 | |||
4f3d09925c | |||
95da6b5c05 | |||
b3060667f2 | |||
cb19defa37 | |||
5a154b24f9 | |||
b46f63fce7 | |||
bb09c0d38c | |||
91ca3f6fff |
@ -1,5 +1,13 @@
|
||||
# Changelog
|
||||
|
||||
## 1.3.13 (2023-04-11)
|
||||
* ```$xlsx->rightToLeft()``` - RTL mode. Column A is on the far right, Column B is one column left of Column A, and so on. Also, information in cells is displayed in the Right to Left format.
|
||||
|
||||
|
||||
## 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)
|
||||
|
||||
|
@ -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!*
|
||||
|
||||
@ -72,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>'],
|
||||
@ -85,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')
|
||||
@ -137,6 +138,10 @@ $xlsx->autoFilter('A1:B10');
|
||||
// the row and column of the indicated cell
|
||||
$xlsx->freezePanes('C3');
|
||||
|
||||
// RTL mode
|
||||
// Column A is on the far right, Column B is one column left of Column A, and so on. Also, information in cells is displayed in the Right to Left format.
|
||||
$xlsx->rightToLeft();
|
||||
|
||||
```
|
||||
### JS array to Excel (AJAX)
|
||||
```php
|
||||
|
@ -13,10 +13,10 @@ namespace Shuchkin;
|
||||
*/
|
||||
class SimpleXLSXGen
|
||||
{
|
||||
|
||||
public $curSheet;
|
||||
protected $defaultFont;
|
||||
protected $defaultFontSize;
|
||||
protected $rtl;
|
||||
protected $sheets;
|
||||
protected $template;
|
||||
protected $NF; // numFmts
|
||||
@ -80,6 +80,8 @@ class SimpleXLSXGen
|
||||
{
|
||||
$this->curSheet = -1;
|
||||
$this->defaultFont = 'Calibri';
|
||||
$this->defaultFontSize = 10;
|
||||
$this->rtl = false;
|
||||
$this->sheets = [['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [], 'mergecells' => [], 'colwidth' => [], 'autofilter' => '']];
|
||||
$this->extLinkId = 0;
|
||||
$this->SI = []; // sharedStrings index
|
||||
@ -110,15 +112,13 @@ class SimpleXLSXGen
|
||||
self::B_SLANT_DASH_DOT => 'slantDashDot'
|
||||
];
|
||||
|
||||
|
||||
$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;
|
||||
|
||||
|
||||
$this->template = [
|
||||
'_rels/.rels' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
||||
@ -129,7 +129,8 @@ class SimpleXLSXGen
|
||||
'docProps/app.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties">
|
||||
<TotalTime>0</TotalTime>
|
||||
<Application>' . __CLASS__ . '</Application></Properties>',
|
||||
<Application>' . __CLASS__ . '</Application>
|
||||
</Properties>',
|
||||
'docProps/core.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<dcterms:created xsi:type="dcterms:W3CDTF">{DATE}</dcterms:created>
|
||||
@ -142,9 +143,13 @@ class SimpleXLSXGen
|
||||
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
|
||||
{SHEETS}',
|
||||
'xl/worksheets/sheet1.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
|
||||
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
|
||||
><dimension ref="{REF}"/>{COLS}<sheetData>{ROWS}</sheetData>{AUTOFILTER}{MERGECELLS}{HYPERLINKS}</worksheet>',
|
||||
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
|
||||
<dimension ref="{REF}"/>
|
||||
{SHEETVIEWS}
|
||||
{COLS}
|
||||
<sheetData>{ROWS}</sheetData>
|
||||
{AUTOFILTER}{MERGECELLS}{HYPERLINKS}
|
||||
</worksheet>',
|
||||
'xl/worksheets/_rels/sheet1.xml.rels' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">{HYPERLINKS}</Relationships>',
|
||||
'xl/sharedStrings.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
@ -157,15 +162,15 @@ class SimpleXLSXGen
|
||||
{BORDERS}
|
||||
<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" /></cellStyleXfs>
|
||||
{XF}
|
||||
<cellStyles count="1">
|
||||
<cellStyle name="Normal" xfId="0" builtinId="0"/>
|
||||
</cellStyles>
|
||||
<cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles>
|
||||
</styleSheet>',
|
||||
'xl/workbook.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
|
||||
<fileVersion appName="' . __CLASS__ . '"/><sheets>
|
||||
<fileVersion appName="' . __CLASS__ . '"/>
|
||||
<sheets>
|
||||
{SHEETS}
|
||||
</sheets></workbook>',
|
||||
</sheets>
|
||||
</workbook>',
|
||||
'[Content_Types].xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
|
||||
<Override PartName="/rels/.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
|
||||
@ -178,7 +183,6 @@ class SimpleXLSXGen
|
||||
{TYPES}
|
||||
</Types>',
|
||||
];
|
||||
|
||||
// <col min="1" max="1" width="22.1796875" bestFit="1" customWidth="1"/>
|
||||
// <row r="1" spans="1:2" x14ac:dyDescent="0.35"><c r="A1" t="s"><v>0</v></c><c r="B1"><v>100</v></c></row><row r="2" spans="1:2" x14ac:dyDescent="0.35"><c r="A2" t="s"><v>1</v></c><c r="B2"><v>200</v></c></row>
|
||||
// <si><t>Простой шаблон</t></si><si><t>Будем делать генератор</t></si>
|
||||
@ -191,7 +195,6 @@ class SimpleXLSXGen
|
||||
|
||||
public function addSheet(array $rows, $name = null)
|
||||
{
|
||||
|
||||
$this->curSheet++;
|
||||
if ($name === null) { // autogenerated sheet names
|
||||
$name = 'Sheet' . ($this->curSheet + 1);
|
||||
@ -214,9 +217,7 @@ class SimpleXLSXGen
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$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;
|
||||
} else {
|
||||
@ -231,14 +232,12 @@ class SimpleXLSXGen
|
||||
if (!$fh) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (!$this->_write($fh)) {
|
||||
fclose($fh);
|
||||
return '';
|
||||
}
|
||||
$size = ftell($fh);
|
||||
fseek($fh, 0);
|
||||
|
||||
return (string)fread($fh, $size);
|
||||
}
|
||||
|
||||
@ -253,7 +252,6 @@ class SimpleXLSXGen
|
||||
return false;
|
||||
}
|
||||
fclose($fh);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -268,50 +266,40 @@ class SimpleXLSXGen
|
||||
if (!$fh) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->_write($fh)) {
|
||||
fclose($fh);
|
||||
return false;
|
||||
}
|
||||
|
||||
$size = ftell($fh);
|
||||
|
||||
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
||||
header('Content-Disposition: attachment; filename="' . $filename . '"');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s \G\M\T', time()));
|
||||
header('Content-Length: ' . $size);
|
||||
|
||||
while (ob_get_level()) {
|
||||
ob_end_clean();
|
||||
}
|
||||
fseek($fh, 0);
|
||||
fpassthru($fh);
|
||||
|
||||
fclose($fh);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function _write($fh)
|
||||
{
|
||||
|
||||
|
||||
$dirSignatureE = "\x50\x4b\x05\x06"; // end of central dir signature
|
||||
$zipComments = 'Generated by ' . __CLASS__ . ' PHP class, thanks sergey.shuchkin@gmail.com';
|
||||
|
||||
if (!$fh) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$cdrec = ''; // central directory content
|
||||
$entries = 0; // number of zipped files
|
||||
$cnt_sheets = count($this->sheets);
|
||||
|
||||
foreach ($this->template as $cfilename => $template) {
|
||||
if ($cfilename === 'xl/_rels/workbook.xml.rels') {
|
||||
$s = '';
|
||||
for ($i = 0; $i < $cnt_sheets; $i++) {
|
||||
$s .= '<Relationship Id="rId' . ($i + 2) . '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"' .
|
||||
' Target="worksheets/sheet' . ($i + 1) . ".xml\"/>\n";
|
||||
' Target="worksheets/sheet' . ($i + 1) . ".xml\"/>\r\n";
|
||||
}
|
||||
$s .= '<Relationship Id="rId' . ($i + 2) . '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/></Relationships>';
|
||||
$template = str_replace('{SHEETS}', $s, $template);
|
||||
@ -362,12 +350,10 @@ class SimpleXLSXGen
|
||||
}
|
||||
}
|
||||
$xml = null;
|
||||
|
||||
} elseif ($cfilename === '[Content_Types].xml') {
|
||||
$TYPES = ['<Override PartName="/_rels/.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>'];
|
||||
foreach ($this->sheets as $k => $v) {
|
||||
$TYPES[] = '<Override PartName="/xl/worksheets/sheet' . ($k + 1) .
|
||||
'.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>';
|
||||
$TYPES[] = '<Override PartName="/xl/worksheets/sheet' . ($k + 1) . '.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>';
|
||||
if ($this->extLinkId) {
|
||||
$TYPES[] = '<Override PartName="/xl/worksheets/_rels/sheet' . ($k + 1) . '.xml.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>';
|
||||
}
|
||||
@ -379,22 +365,19 @@ class SimpleXLSXGen
|
||||
$NF = $XF = $FONTS = $F_KEYS = $FILLS = $FL_KEYS = [];
|
||||
$BR = ['<border><left/><right/><top/><bottom/><diagonal/></border>'];
|
||||
$BR_KEYS = [0 => 0];
|
||||
|
||||
foreach ($this->NF as $k => $v) {
|
||||
$NF[] = '<numFmt numFmtId="' . $k . '" formatCode="' . htmlspecialchars($v, ENT_QUOTES) . '"/>';
|
||||
}
|
||||
|
||||
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/>' : '')
|
||||
@ -427,7 +410,6 @@ class SimpleXLSXGen
|
||||
. ($xf[1] & self::A_MIDDLE ? ' vertical="center"' : '')
|
||||
. ($xf[1] & self::A_BOTTOM ? ' vertical="bottom"' : '')
|
||||
. ($xf[1] & self::A_WRAPTEXT ? ' wrapText="1"' : '');
|
||||
|
||||
// border
|
||||
$BR_ID = 0;
|
||||
if ($xf[6] !== '') {
|
||||
@ -451,7 +433,6 @@ class SimpleXLSXGen
|
||||
foreach ($sides as $side => $idx) {
|
||||
$s = 'thin';
|
||||
$c = '';
|
||||
|
||||
$va = explode('#', $ba[$idx]);
|
||||
if (isset($va[1])) {
|
||||
$s = $va[0] === '' ? 'thin' : $va[0];
|
||||
@ -464,7 +445,6 @@ class SimpleXLSXGen
|
||||
if (strlen($c) === 6) {
|
||||
$c = 'FF' . $c;
|
||||
}
|
||||
|
||||
if ($s && $s !== 'none') {
|
||||
$border .= '<' . $side . ' style="' . $s . '">'
|
||||
. '<color ' . ($c === '' ? 'auto="1"' : 'rgb="' . $c . '"') . '/>'
|
||||
@ -472,24 +452,18 @@ class SimpleXLSXGen
|
||||
} else {
|
||||
$border .= '<' . $side . '/>';
|
||||
}
|
||||
|
||||
}
|
||||
$border .= '</border>';
|
||||
$BR[] = $border;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$XF[] = '<xf numFmtId="' . $xf[0] . '" fontId="' . $F_ID . '" fillId="' . $FL_ID . '" borderId="' . $BR_ID . '" xfId="0"'
|
||||
. ($xf[0] > 0 ? ' applyNumberFormat="1"' : '')
|
||||
. ($F_ID > 0 ? ' applyFont="1"' : '')
|
||||
. ($FL_ID > 0 ? ' applyFill="1"' : '')
|
||||
. ($BR_ID > 0 ? ' applyBorder="1"' : '')
|
||||
. ($align ? ' applyAlignment="1"><alignment' . $align . '/></xf>' : '/>');
|
||||
|
||||
}
|
||||
|
||||
|
||||
// wrap collections
|
||||
array_unshift($NF, '<numFmts count="' . count($NF) . '">');
|
||||
$NF[] = '</numFmts>';
|
||||
@ -505,7 +479,8 @@ class SimpleXLSXGen
|
||||
$template = str_replace(
|
||||
['{NUMFMTS}', '{FONTS}', '{XF}', '{FILLS}', '{BORDERS}'],
|
||||
[implode("\r\n", $NF), implode("\r\n", $FONTS), implode("\r\n", $XF), implode("\r\n", $FILLS), implode("\r\n", $BR)],
|
||||
$template);
|
||||
$template
|
||||
);
|
||||
$this->_writeEntry($fh, $cdrec, $cfilename, $template);
|
||||
$entries++;
|
||||
} else {
|
||||
@ -515,7 +490,6 @@ class SimpleXLSXGen
|
||||
}
|
||||
$before_cd = ftell($fh);
|
||||
fwrite($fh, $cdrec);
|
||||
|
||||
// end of central dir
|
||||
fwrite($fh, $dirSignatureE);
|
||||
fwrite($fh, pack('v', 0)); // number of this disk
|
||||
@ -526,7 +500,6 @@ class SimpleXLSXGen
|
||||
fwrite($fh, pack('V', $before_cd)); // offset to start of central dir
|
||||
fwrite($fh, pack('v', mb_strlen($zipComments, '8bit'))); // .zip file comment length
|
||||
fwrite($fh, $zipComments);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -537,7 +510,6 @@ class SimpleXLSXGen
|
||||
|
||||
$e = [];
|
||||
$e['uncsize'] = mb_strlen($data, '8bit');
|
||||
|
||||
// if data to compress is too small, just store it
|
||||
if ($e['uncsize'] < 256) {
|
||||
$e['comsize'] = $e['uncsize'];
|
||||
@ -551,7 +523,6 @@ class SimpleXLSXGen
|
||||
$e['vneeded'] = 10;
|
||||
$e['cmethod'] = 8;
|
||||
}
|
||||
|
||||
$e['bitflag'] = 0;
|
||||
$e['crc_32'] = crc32($data);
|
||||
|
||||
@ -563,11 +534,8 @@ 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");
|
||||
|
||||
$e['offset'] = ftell($fh);
|
||||
|
||||
fwrite($fh, $zipSignature);
|
||||
@ -590,22 +558,22 @@ class SimpleXLSXGen
|
||||
$e['comments'] = '';
|
||||
|
||||
$cdrec .= $dirSignature;
|
||||
$cdrec .= "\x0\x0"; // version made by
|
||||
$cdrec .= pack('v', $e['vneeded']); // version needed to extract
|
||||
$cdrec .= "\x0\x0"; // general bit flag
|
||||
$cdrec .= pack('v', $e['cmethod']); // compression method
|
||||
$cdrec .= pack('v', $e['modtime']); // lastmod time
|
||||
$cdrec .= pack('v', $e['moddate']); // lastmod date
|
||||
$cdrec .= pack('V', $e['crc_32']); // crc32
|
||||
$cdrec .= pack('V', $e['comsize']); // compressed filesize
|
||||
$cdrec .= pack('V', $e['uncsize']); // uncompressed filesize
|
||||
$cdrec .= pack('v', mb_strlen($cfilename, '8bit')); // file name length
|
||||
$cdrec .= pack('v', 0); // extra field length
|
||||
$cdrec .= "\x0\x0"; // version made by
|
||||
$cdrec .= pack('v', $e['vneeded']); // version needed to extract
|
||||
$cdrec .= "\x0\x0"; // general bit flag
|
||||
$cdrec .= pack('v', $e['cmethod']); // compression method
|
||||
$cdrec .= pack('v', $e['modtime']); // lastmod time
|
||||
$cdrec .= pack('v', $e['moddate']); // lastmod date
|
||||
$cdrec .= pack('V', $e['crc_32']); // crc32
|
||||
$cdrec .= pack('V', $e['comsize']); // compressed filesize
|
||||
$cdrec .= pack('V', $e['uncsize']); // uncompressed filesize
|
||||
$cdrec .= pack('v', mb_strlen($cfilename, '8bit')); // file name length
|
||||
$cdrec .= pack('v', 0); // extra field length
|
||||
$cdrec .= pack('v', mb_strlen($e['comments'], '8bit')); // file comment length
|
||||
$cdrec .= pack('v', 0); // disk number start
|
||||
$cdrec .= pack('v', 0); // internal file attributes
|
||||
$cdrec .= pack('V', $e['external_attributes']); // internal file attributes
|
||||
$cdrec .= pack('V', $e['offset']); // relative offset of local header
|
||||
$cdrec .= pack('v', 0); // disk number start
|
||||
$cdrec .= pack('v', 0); // internal file attributes
|
||||
$cdrec .= pack('V', $e['external_attributes']); // internal file attributes
|
||||
$cdrec .= pack('V', $e['offset']); // relative offset of local header
|
||||
$cdrec .= $cfilename;
|
||||
$cdrec .= $e['comments'];
|
||||
}
|
||||
@ -617,28 +585,43 @@ class SimpleXLSXGen
|
||||
setlocale(LC_NUMERIC, 'C');
|
||||
$COLS = [];
|
||||
$ROWS = [];
|
||||
$SHEETVIEWS = '';
|
||||
$SHEETVIEWS = '<sheetViews><sheetView tabSelected="1" workbookViewId="0"'.($this->rtl ? ' rightToLeft="1"' : '').'>';
|
||||
$AC = 'A1'; // Active Cell
|
||||
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);
|
||||
$AC = $this->sheets[$idx]['frozen'];
|
||||
self::cell2coord($AC, $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 (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];
|
||||
}
|
||||
$AC = self::coord2cell($x, $y);
|
||||
}
|
||||
if ($x > 0 || $y > 0) {
|
||||
$split = '';
|
||||
if ($x > 0) $split .= " xSplit=\"{$x}\"";
|
||||
if ($y > 0) $split .= " ySplit=\"{$y}\"";
|
||||
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>";
|
||||
if ($x > 0 && $y === 0) {
|
||||
$activepane = 'topRight';
|
||||
}
|
||||
if ($x === 0 && $y > 0) {
|
||||
$activepane = 'bottomLeft';
|
||||
}
|
||||
$SHEETVIEWS .= '<pane' . $split . ' topLeftCell="' . $AC . '" activePane="' . $activepane . '" state="frozen"/>';
|
||||
}
|
||||
}
|
||||
$SHEETVIEWS .= '<selection activeCell="' . $AC . '" sqref="' . $AC . '"/>';
|
||||
$SHEETVIEWS .= '</sheetView></sheetViews>';
|
||||
$COLS[] = '<cols>';
|
||||
$CUR_ROW = 0;
|
||||
$COL = [];
|
||||
@ -653,18 +636,14 @@ class SimpleXLSXGen
|
||||
$COL[$CUR_COL] = 0;
|
||||
}
|
||||
$cname = $this->num2name($CUR_COL) . $CUR_ROW;
|
||||
|
||||
if ($v === null || $v === '') {
|
||||
$row .= '<c r="' . $cname . '"/>';
|
||||
continue;
|
||||
}
|
||||
|
||||
$ct = $cv = $cf = null;
|
||||
$N = $A = $F = $FL = $C = $BG = 0;
|
||||
$N = $A = $F = $FL = $C = $BG = $FS = 0;
|
||||
$BR = '';
|
||||
|
||||
if (is_string($v)) {
|
||||
|
||||
if ($v[0] === "\0") { // RAW value as string
|
||||
$v = substr($v, 1);
|
||||
$vl = mb_strlen($v);
|
||||
@ -683,9 +662,7 @@ class SimpleXLSXGen
|
||||
$F += self::F_STRIKE;
|
||||
}
|
||||
if (preg_match('/<style([^>]+)>/', $v, $m)) {
|
||||
|
||||
if (preg_match('/ color="([^"]+)"/', $m[1], $m2)) {
|
||||
|
||||
$F += self::F_COLOR;
|
||||
$c = ltrim($m2[1], '#');
|
||||
$C = strlen($c) === 8 ? $c : ('FF' . $c);
|
||||
@ -708,6 +685,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;
|
||||
@ -817,9 +800,7 @@ class SimpleXLSXGen
|
||||
}
|
||||
}
|
||||
if ($cv === null) {
|
||||
|
||||
$v = $this->esc($v);
|
||||
|
||||
if ($cf) {
|
||||
$ct = 'str';
|
||||
$cv = $v;
|
||||
@ -853,13 +834,9 @@ class SimpleXLSXGen
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
$COL[$CUR_COL] = max($vl, $COL[$CUR_COL]);
|
||||
|
||||
$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;
|
||||
}
|
||||
@ -867,19 +844,16 @@ class SimpleXLSXGen
|
||||
$F += self::F_COLOR;
|
||||
$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];
|
||||
}
|
||||
}
|
||||
|
||||
$row .= '<c r="' . $cname . '"' . ($ct ? ' t="' . $ct . '"' : '') . ($cs ? ' s="' . $cs . '"' : '') . '>'
|
||||
. ($cf ? '<f>' . $cf . '</f>' : '')
|
||||
. ($ct === 'inlineStr' ? '<is><t>' . $cv . '</t></is>' : '<v>' . $cv . '</v>') . "</c>\r\n";
|
||||
@ -950,20 +924,16 @@ class SimpleXLSXGen
|
||||
|
||||
public function date2excel($year, $month, $day, $hours = 0, $minutes = 0, $seconds = 0)
|
||||
{
|
||||
|
||||
$excelTime = (($hours * 3600) + ($minutes * 60) + $seconds) / 86400;
|
||||
|
||||
if ((int)$year === 0) {
|
||||
return $excelTime;
|
||||
}
|
||||
|
||||
// self::CALENDAR_WINDOWS_1900
|
||||
$excel1900isLeapYear = True;
|
||||
if (($year === 1900) && ($month <= 2)) {
|
||||
$excel1900isLeapYear = False;
|
||||
}
|
||||
$myExcelBaseDate = 2415020;
|
||||
|
||||
// Julian base date Adjustment
|
||||
if ($month > 2) {
|
||||
$month -= 3;
|
||||
@ -975,7 +945,6 @@ class SimpleXLSXGen
|
||||
$decade = substr($year, 2, 2);
|
||||
// Calculate the Julian Date, then subtract the Excel base date (JD 2415020 = 31-Dec-1899 Giving Excel Date of 0)
|
||||
$excelDate = floor((146097 * $century) / 4) + floor((1461 * $decade) / 4) + floor((153 * $month + 2) / 5) + $day + 1721119 - $myExcelBaseDate + $excel1900isLeapYear;
|
||||
|
||||
return (float)$excelDate + $excelTime;
|
||||
}
|
||||
|
||||
@ -1008,6 +977,10 @@ class SimpleXLSXGen
|
||||
$this->sheets[$this->curSheet]['colwidth'][$col] = $width;
|
||||
return $this;
|
||||
}
|
||||
public function rightToLeft($value = true) {
|
||||
$this->rtl = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function esc($str)
|
||||
{
|
||||
@ -1030,9 +1003,10 @@ class SimpleXLSXGen
|
||||
return $id;
|
||||
}
|
||||
|
||||
|
||||
public static function raw($value)
|
||||
{
|
||||
return "\0" . (string)$value;
|
||||
return "\0" . $value;
|
||||
}
|
||||
|
||||
public static function cell2coord($cell, &$x, &$y)
|
||||
@ -1040,9 +1014,15 @@ class SimpleXLSXGen
|
||||
$x = $y = 0;
|
||||
$lettercount = 0;
|
||||
$cell = str_replace([' ', '\t', '\r', '\n', '\v', '\0'], '', $cell);
|
||||
if (empty($cell)) return;
|
||||
if (empty($cell)) {
|
||||
return;
|
||||
}
|
||||
$cell = strtoupper($cell);
|
||||
for ($i = 0; $i < strlen($cell); $i++) if ($cell[$i] >= 'A' && $cell[$i] <= 'Z') $lettercount++;
|
||||
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;
|
||||
@ -1051,15 +1031,18 @@ class SimpleXLSXGen
|
||||
$e++;
|
||||
}
|
||||
}
|
||||
if ($lettercount < strlen($cell)) $y = ((int)substr($cell, $lettercount)) - 1;
|
||||
if ($lettercount < strlen($cell)) {
|
||||
$y = ((int)substr($cell, $lettercount)) - 1;
|
||||
}
|
||||
}
|
||||
|
||||
public static function coord2cell($x, $y, $absolute = false)
|
||||
public static function coord2cell($x, $y)
|
||||
{
|
||||
$c = '';
|
||||
for ($i = $x; $i >= 0; $i = ((int)($i / 26)) - 1) $c = chr(ord('A') + $i % 26) . $c;
|
||||
if ($absolute) $absolute = '$'; else $absolute = '';
|
||||
return $absolute . $c . $absolute . ($y+1);
|
||||
for ($i = $x; $i >= 0; $i = ((int)($i / 26)) - 1) {
|
||||
$c = chr(ord('A') + $i % 26) . $c;
|
||||
}
|
||||
return $c . ($y+1);
|
||||
}
|
||||
|
||||
public function freezePanes($cell)
|
||||
@ -1067,5 +1050,4 @@ class SimpleXLSXGen
|
||||
$this->sheets[$this->curSheet]['frozen'] = $cell;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
BIN
styles.png
BIN
styles.png
Binary file not shown.
Before Width: | Height: | Size: 52 KiB After Width: | Height: | Size: 190 KiB |
Reference in New Issue
Block a user