使用 C++ 通过 JavaScript 自定义 Ribbon XML
Contents
[
Hide
]
自2007年以来,Microsoft Office通过在应用程序窗口顶部使用Ribbon替换了菜单和工具栏。Ribbon可定制。 C++ 实现的 Script 允许你
- 保留Ribbon XML而无需解析它,
- 读取和写入Ribbon XML而无需解析它,
- 获取和设置Ribbon XML数据。
如果要更改Ribbon XML,则必须使用XML解析器或其他Ribbon XML工具解析它。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example - Set Ribbon XML</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</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('fileInput');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Loads the workbook from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Set custom ribbon XML
const ribbonXml = `<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"></customUI>`;
workbook.ribbonXml = ribbonXml;
// Save the modified workbook 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);
const outputFileName = 'output_' + (file.name || 'modified.xlsx');
downloadLink.download = outputFileName;
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
resultDiv.innerHTML = '<p style="color: green;">Ribbon XML set successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>