Having trouble getting help?
Contact Support
Contact Support
Save and open Spreadsheet data as a Base64 string in EJ2 JavaScript Spreadsheet control
In the Spreadsheet control, there is currently no direct option to save and open data as a Base64
string. You can achieve this by saving the Spreadsheet data as blob data and then converting that saved blob data to a Base64
string using FileReader
.
You can get the Spreadsheet data as blob in the saveComplete event when you set the
needBlobData
as true andisFullPost
as false in the beforeSave event.
The following code example shows how to save and open the spreadsheet data as base64 string.
var base64String;
var spreadsheet = new ej.spreadsheet.Spreadsheet({
openUrl: 'https://services.syncfusion.com/js/production/api/spreadsheet/open',
sheets: [{
name: 'Car Sales Report',
ranges: [{ dataSource: data }],
columns: [
{ width: 180 }, { width: 130 }, { width: 130 }, { width: 180 },
{ width: 130 }, { width: 120 }
]
}],
created: () => {
//Applies cell and number formatting to specified range of the active sheet
spreadsheet.cellFormat({ fontWeight: 'bold', textAlign: 'center', verticalAlign: 'middle' }, 'A1:F1');
},
beforeSave: (args) => {
args.needBlobData = true; // To trigger the saveComplete event.
args.isFullPost = false; // Get the spreadsheet data as blob data in the saveComplete event.
},
saveComplete: (args) => {
// Convert blob data to base64 string.
var reader = new FileReader();
reader.readAsDataURL(args.blobData);
reader.onloadend = function () {
base64String = reader.result;
};
}
});
// Render initialized Spreadsheet.
spreadsheet.appendTo('#spreadsheet');
document.getElementById("import").onclick = () => {
// Open the file based on saved base64 string.
fetch(base64String)
.then((response) => response.blob())
.then((fileBlob) => {
var file = new File([fileBlob], 'Sample.xlsx');
spreadsheet.open({ file: file });
});
}
document.getElementById("export").onclick = () => {
spreadsheet.save({
url: 'https://services.syncfusion.com/js/production/api/spreadsheet/save',
fileName: 'Worksheet',
saveType: 'Xlsx',
}); // Specifies the save URL, file name, file type need to be saved.
// Logs base64 string into the console.
console.log('Base64 String - ', base64String);
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 SpreadSheet</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link rel="shortcut icon" href="resources/favicon.ico">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-lists/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-navigations/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-dropdowns/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-grids/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-spreadsheet/styles/material.css" rel="stylesheet">
<link href="styles.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.4.1/shim.min.js"></script>
<script src="https://cdn.syncfusion.com/ej2/28.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<!--Element which is going to render-->
<div id="container">
<button class="e-btn custom-btn" id="import">Import Base64</button>
<button class="e-btn custom-btn" id="export">Export as Base64</button>
<div id="spreadsheet"></div>
</div>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body>
</html>