Export XLSX on Browser???
See original GitHub issuePerhaps I’m misunderstanding the function of the library, but I’m trying to export an excel file from the browser using exceljs, and the writefile function doesn’t seem to work. When I attempt to invoke writeFile, I get the following error:
Uncaught TypeError: fs.createWriteStream is not a function
Is exceljs designed to write files from the browser, or from the server?
My code:
index.html
<!DOCTYPE html>
<head>
<title>Test Excel JS</title>
<meta charset="utf-8">
<meta name="description" content="">
<link rel="stylesheet" href="app.css">
</head>
<body>
<div>
<label>Test</label>
<button onclick="test()">Test this Stuff and Check your console log</button>
</div>
<script src="bundle.js"></script>
<script>
var test = function(){
var workbook = generateTestFile();
workbook.xlsx.writeFile('newtestfile.xlsx')
.then(function() {
console.log('Done Writing file: ');
});
};
</script>
</body>
</html>
app.js (bundled to bundle.js using browserify)
'use strict';
global.Excel = require('../node_modules/exceljs/dist/es5/exceljs.browser');
global.isRowBold = function(excelRow){
return excelRow.getCell('name').value === "Jeff";
};
global.getRowColor = function(excelRow){
return excelRow.getCell('color').value;
};
global.getCellColor = function(excelRow, cell){
return (excelRow.getCell('name').value === 'John' && cell.value === 0)? 'orange' : excelRow.getCell('color').value;
};
global.getFont = function(isBold, color){
return {
name: 'Arial Black',
color: color,
family: 2,
size: 14,
bold: isBold
};
};
global.getTestHeader = function(){
return [
{key: "id", header: "Id"},
{key: "name", header: "Name", width: 32},
{key: "color", header: "Color", width: 10}
];
};
global.getTestData = function(){
return [
{
id: 0,
name: "John",
color: "green"
},
{
id: 1,
name: "Rehan",
color: "blue"
},
{
id: 2,
name: "Jeff",
color: "yellow"
}
];
};
global.generateTestFile = function(){
var workbook = new Excel.Workbook();
var worksheet = workbook.addWorksheet('Sheet 1');
//Set Column Headers
worksheet.columns = getTestHeader();
//Add Rows
var testData = getTestData();
var length = testData.length;
for(var i = 0; i < length; i++){
worksheet.addRow(testData[i]);
}
//Format Rows
worksheet.eachRow(function(row, rowNumber){
var isBold = isRowBold(row);
row.eachCell(function(cell, colNumber){
var cellColor = getCellColor(row, cell);
cell.font = getFont(isBold, cellColor);
});
});
return workbook;
};
Issue Analytics
- State:
- Created 6 years ago
- Comments:5
Top Results From Across the Web
Create an .xlsx file in a browser | by Marian Čaikovski - Medium
xlsx format is the best option. Export into pure text files such as .csv might cause dispensable problems. For example, dates might be...
Read more >Switch to Excel for the web from Sheets - Microsoft Support
To download a Sheet as an Excel file, go to the Sheet and select File > Download > Microsoft Excel (.xlsx). Excel for...
Read more >How to Import and Export Excel XLSX Using JavaScript
Learn how to import and export Excel XLSX files using JavaScript. ... Once we add that code, we can open the page in...
Read more >Export Dataset to default to XLSX files when browsing to find ...
Export Dataset to default to XLSX files when browsing to find Excel files. Export Format XLS File is selected and ".
Read more >SheetJS JS-XLSX In-Browser HTML Table Export Demo
SheetJS JS-XLSX In-Browser HTML Table Edit Demo ... This demonstration focuses on HTML contenteditable. You can drop a workbook into the box and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@pleger For some reason surrounding
data
in brackets and adding charset to thetype
property did it for me.When I try this, I get ‘Failed - No file’ in Chrome. Logging blob to the console shows that it’s instantiated and looks right as far as I can tell. No idea what to do now.