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

11 Commits

Author SHA1 Message Date
053574d1e1 1.2.15 2022-07-05 11:29:18 +06:00
f74163d311 1.2.15 2022-07-05 11:19:42 +06:00
0bf38275d7 1.2.14 2022-06-10 12:11:00 +06:00
459b1666e9 1.2.13 2022-06-10 12:07:39 +06:00
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 115 additions and 17 deletions

View File

@ -1,9 +1,24 @@
# Changelog
## 1.2.15 (2022-07-05)
* added wrap words in long strings `<wraptext>long long line</wraptext>`
# 1.2.10 (2022-04-24)
* Added colors `&lt;style color="#FFFF00" bgcolor="#00FF00"&gt;Yellow text on blue background&lt;/style&gt;`, thx [mrjemson](https://github.com/mrjemson)
## 1.2.14 (2022-06-10)
* added example [JS array to Excel (AJAX)](https://github.com/shuchkin/simplexlsxgen#js-array-to-excel-ajax)
# 1.1.12 (2022-03-15)
## 1.2.13 (2022-06-01)
* setColWidth(num_col_started_1, size_in_chars) - set column width
## 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')`
## 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)
- 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!*
@ -70,12 +70,18 @@ $data = [
['Center', '<center>12345.67</center>'],
['Right', '<right>Right Text</right>'],
['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],
['<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' )
->setDefaultFontSize( 14 )
->mergeCells('A16:B16')
->setColWidth(1, 35) // 1 - num column, 35 - size in chars
->mergeCells('A20:B20')
->saveAs('styles_and_tags.xlsx');
```
![XLSX screenshot](styles.png)
@ -99,6 +105,54 @@ $xlsx->addSheet( $books2, 'Stephen King catalog');
$xlsx->downloadAs('books_2021.xlsx');
exit();
```
### JS array to Excel (AJAX)
```php
<?php // array2excel.php
if (isset($_POST['array2excel'])) {
require __DIR__.'/simplexlsxgen/src/SimpleXLSXGen.php';
$data = json_decode($_POST['array2excel'], false);
\Shuchkin\SimpleXLSXGen::fromArray($data)->downloadAs('file.xlsx');
return;
}
?>
<html lang="en">
<head>
<title>JS array to Excel</title>
</head>
<script>
function array2excel() {
var books = [
["ISBN", "title", "author", "publisher", "ctry"],
[618260307, "The Hobbit", "J. R. R. Tolkien", "Houghton Mifflin", "USA"],
[908606664, "Slinky Malinki", "Lynley Dodd", "Mallinson Rendel", "NZ"]
];
var json = JSON.stringify(books);
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
var file = new Blob([this.response], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
} else {
alert("Error: " + this.status + " " + this.statusText);
}
}
}
request.open('POST', "array2excel.php");
request.responseType = "blob";
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send("array2excel=" + encodeURIComponent(json));
}
</script>
<body>
<input type="button" onclick="array2excel()" value="array2excel" />
</body>
</html>
```
## Debug
```php

View File

@ -46,12 +46,16 @@ class SimpleXLSXGen {
const A_DEFAULT = 0;
const A_LEFT = 1;
const A_RIGHT = 2;
const A_CENTER = 3;
const A_CENTER = 4;
const A_TOP = 8;
const A_MIDDLE = 16;
const A_BOTTOM = 32;
const A_WRAPTEXT = 64;
public function __construct() {
$this->curSheet = -1;
$this->defaultFont = 'Calibri';
$this->sheets = [ ['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [], 'mergecells' => [] ] ];
$this->sheets = [ ['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [], 'mergecells' => [], 'colwidth' => [] ] ];
$this->SI = []; // sharedStrings index
$this->SI_KEYS = []; // & keys
$this->XF = [ // styles
@ -153,7 +157,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]) ) {
$this->sheets[$this->curSheet]['rows'] = $rows;
@ -346,15 +350,19 @@ class SimpleXLSXGen {
.( $xf[3] & self::FL_COLOR ? '><fgColor rgb="'.$xf[5].'"/><bgColor indexed="64"/></patternFill>' : ' />')
.'</fill>';
}
$align = ($xf[1] === self::A_LEFT ? ' applyAlignment="1"><alignment horizontal="left"/>' : '')
.($xf[1] === self::A_RIGHT ? ' applyAlignment="1"><alignment horizontal="right"/>' : '')
.($xf[1] === self::A_CENTER ? ' applyAlignment="1"><alignment horizontal="center"/>' : '');
$align = ($xf[1] & self::A_LEFT ? ' horizontal="left"' : '')
.($xf[1] & self::A_RIGHT ? ' horizontal="right"' : '')
.($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[1] & self::A_WRAPTEXT ? ' wrapText="1"' : '');
$XF[] = '<xf numFmtId="'.$xf[0].'" fontId="'.$F_ID.'" fillId="'.$FL_ID.'" borderId="0" xfId="0"'
.($xf[0] > 0 ? ' applyNumberFormat="1"' : '')
.($F_ID > 0 ? ' applyFont="1"' : '')
.($FL_ID > 0 ? ' applyFill="1"' : '')
.($align ? $align . '</xf>' : '/>');
.($align ? ' applyAlignment="1"><alignment'.$align . '/></xf>' : '/>');
}
// wrap collections
@ -481,8 +489,9 @@ class SimpleXLSXGen {
$COL = [];
foreach( $this->sheets[$idx]['rows'] as $r ) {
$CUR_ROW++;
$row = '<row r="'.$CUR_ROW.'">';
$row = '';
$CUR_COL = 0;
$RH = 0; // row height
foreach( $r as $v ) {
$CUR_COL++;
if ( !isset($COL[ $CUR_COL ])) {
@ -528,6 +537,9 @@ class SimpleXLSXGen {
$FL += self::FL_COLOR;
$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 ) {
$A += self::A_LEFT;
@ -538,6 +550,18 @@ class SimpleXLSXGen {
if ( strpos( $v, '<right>' ) !== false ) {
$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 ( strpos( $v, '<wraptext>' ) !== false ) {
$A += self::A_WRAPTEXT;
}
if ( preg_match( '/<a href="(https?:\/\/[^"]+)">(.*?)<\/a>/i', $v, $m ) ) {
$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] : ''];
@ -582,7 +606,7 @@ class SimpleXLSXGen {
$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
} 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 ) ) {
$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] : ''];
@ -660,10 +684,11 @@ class SimpleXLSXGen {
$row .= '<c r="' . $cname . '"' . ($ct ? ' t="' . $ct . '"' : '') . ($cs ? ' s="' . $cs . '"' : '') . '>'
. ($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 ) {
$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>';
$REF = 'A1:'.$this->num2name(count($COL)) . $CUR_ROW;
@ -747,6 +772,10 @@ class SimpleXLSXGen {
$this->sheets[$this->curSheet]['mergecells'][] = $range;
return $this;
}
public function setColWidth($col, $width) {
$this->sheets[$this->curSheet]['colwidth'][$col] = $width;
return $this;
}
public function esc( $str ) {
// XML UTF-8: #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
// but we use fast version

Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 KiB

After

Width:  |  Height:  |  Size: 40 KiB