Import XML to Excel workbook with JavaScript via C++
Contents
[
Hide
]
Aspose.Cells allows you to import the XML map inside the workbook using the Workbook.importXml(string, string, number, number) method. You can import XML Map using Microsoft Excel with the following steps:
- Select Developer tab
- Click Import in the XML section and follow the required steps.
You will need to provide your XML data to complete the import. Here is a sample XML data that you can use for testing.
Import XML Map using Microsoft Excel
The following screenshot shows how to import XML Map using Microsoft Excel.
![]() |
---|
Import XML Map using Aspose.Cells for JavaScript via C++
The following sample code shows how to make use of the Workbook.importXml(string, string, number, number). It generates the output excel file as shown in this screenshot.
![]() |
---|
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Import XML</title>
</head>
<body>
<h1>Import XML into Workbook Example</h1>
<input type="file" id="xmlInput" accept=".xml,.txt" />
<button id="runExample">Import XML and Save</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, Worksheet, Cell } = 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('xmlInput');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an XML file.</p>';
return;
}
const file = fileInput.files[0];
const xmlText = await file.text();
// Create a workbook
const workbook = new Workbook();
// Import your XML Map data starting from cell A1 on Sheet1
workbook.importXml(xmlText, "Sheet1", 0, 0);
// Save workbook to XLSX and provide download link
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output_out.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
resultDiv.innerHTML = '<p style="color: green;">XML imported and workbook saved. Click the download link to get the file.</p>';
});
</script>
</html>