mirror of
https://github.com/shuchkin/simplexlsxgen.git
synced 2023-08-10 21:12:59 +03:00
Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
053574d1e1 | |||
f74163d311 | |||
0bf38275d7 | |||
459b1666e9 | |||
b63c66f8e9 | |||
1b5701a07f | |||
199e5691e7 | |||
c54b314328 | |||
6db113f753 | |||
ff3f016358 | |||
bcd3586731 |
21
CHANGELOG.md
21
CHANGELOG.md
@ -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 `<style color="#FFFF00" bgcolor="#00FF00">Yellow text on blue background</style>`, 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)
|
||||
|
60
README.md
60
README.md
@ -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');
|
||||
```
|
||||

|
||||
@ -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
|
||||
|
@ -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
|
||||
|
BIN
styles.png
BIN
styles.png
Binary file not shown.
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 40 KiB |
Reference in New Issue
Block a user