Json with JavaScript via C++
Contents
[
Hide
]
Aspose.Cells supports converting a workbook to Json (JavaScript Object Notation) file.
Convert Excel Workbook to JSON
The Aspose.Cells API provides support for converting spreadsheets to JSON format. To export the workbook to JSON, pass SaveFormat.Json as the second parameter of Workbook.save(string, SaveFormat) method. You may also use JsonSaveOptions class to specify additional settings for exporting worksheet to JSON.
The following code example demonstrates exporting the active worksheet to JSON by using SaveFormat.Json enumeration member. Please see the code to convert source file to the output Json file generated by the code for reference.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Convert Workbook to JSON</title>
</head>
<body>
<h1>Convert Workbook to JSON Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Convert to JSON</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate Workbook from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Save workbook as JSON
const outputData = workbook.save(SaveFormat.Json);
const blob = new Blob([outputData], { type: 'application/json' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'book1.json';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download JSON File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook converted to JSON successfully! Click the download link to get the JSON file.</p>';
});
</script>
</html>