Compare commits

...

2 Commits

Author SHA1 Message Date
Sergey Shuchkin 0bf38275d7 1.2.14 2022-06-10 12:11:00 +06:00
Sergey Shuchkin 459b1666e9 1.2.13 2022-06-10 12:07:39 +06:00
2 changed files with 51 additions and 1 deletions

View File

@ -1,4 +1,6 @@
# Changelog
## 1.2.14 (2022-06-10)
* added example [JS array to Excel (AJAX)](https://github.com/shuchkin/simplexlsxgen#js-array-to-excel-ajax)
## 1.2.13 (2022-06-01)
* setColWidth(num_col_started_1, size_in_chars) - set column width

View File

@ -79,7 +79,7 @@ $data = [
SimpleXLSXGen::fromArray( $data )
->setDefaultFont( 'Courier New' )
->setDefaultFontSize( 14 )
->setColWidth(1, 35)
->setColWidth(1, 35) // 1 - num column, 35 - size in chars
->mergeCells('A20:B20')
->saveAs('styles_and_tags.xlsx');
```
@ -104,6 +104,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