Skip to content

Instantly share code, notes, and snippets.

@abhigarg
Forked from code-boxx/0-JS-EXCEL.MD
Created November 5, 2023 19:39
Show Gist options
  • Save abhigarg/f5456dfa390d866c47ca707fca719d20 to your computer and use it in GitHub Desktop.
Save abhigarg/f5456dfa390d866c47ca707fca719d20 to your computer and use it in GitHub Desktop.
Javascript Create Excel File

JAVASCRIPT CREATE EXCEL FILE

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Javascript Excel</title>
<!-- (A) LOAD SHEETJS FROM CDN -->
<!-- https://sheetjs.com/ -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
</head>
<body>
<!-- (B) CLICK TO GENERATE DEMO EXCEL -->
<button id="demo">Download Demo Excel</button>
<!-- (C) JAVASCRIPT -->
<script>
document.getElementById("demo").onclick = () => {
// (C1) DUMMY DATA
var data = [
["Joa Doe", "[email protected]"],
["Job Doe", "[email protected]"],
["Joe Doe", "[email protected]"],
["Jon Doe", "[email protected]"],
["Joy Doe", "[email protected]"]
];
// (C2) CREATE NEW EXCEL "FILE"
var workbook = XLSX.utils.book_new(),
worksheet = XLSX.utils.aoa_to_sheet(data);
workbook.SheetNames.push("First");
workbook.Sheets["First"] = worksheet;
// (C3) "FORCE DOWNLOAD" XLSX FILE
XLSX.writeFile(workbook, "demo.xlsx");
};
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Javascript Excel</title>
<!-- (A) LOAD SHEETJS FROM CDN -->
<!-- https://sheetjs.com/ -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
</head>
<body>
<!-- (B) CLICK TO GENERATE DEMO EXCEL -->
<button id="demo">Upload Demo Excel</button>
<!-- (C) JAVASCRIPT -->
<script>
document.getElementById("demo").onclick = () => {
// (C1) DUMMY DATA
var data = [
["Joa Doe", "[email protected]"],
["Job Doe", "[email protected]"],
["Joe Doe", "[email protected]"],
["Jon Doe", "[email protected]"],
["Joy Doe", "[email protected]"]
];
// (C2) CREATE NEW EXCEL "FILE"
var workbook = XLSX.utils.book_new(),
worksheet = XLSX.utils.aoa_to_sheet(data);
workbook.SheetNames.push("First");
workbook.Sheets["First"] = worksheet;
// (C3) TO BLOB
var xlsblob = new Blob(
[new Uint8Array(XLSX.write(workbook, { bookType: "xlsx", type: "array" }))],
{type:"application/octet-stream"}
);
// (C4) FORM DATA
var data = new FormData();
data.append("xls", xlsblob, "demo.xlsx");
// (C5) UPLOAD TO SERVER
fetch("2b-upload.php", { method: "POST", body: data })
.then(res => res.text())
.then(txt => console.log(txt));
};
</script>
</body>
</html>
<?php
echo move_uploaded_file(
$_FILES["xls"]["tmp_name"],
__DIR__ . DIRECTORY_SEPARATOR . $_FILES["xls"]["name"]
) ? "OK" : "ERROR" ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment