Extract Theme Data from Excel File
Contents
[
Hide
]
Aspose.Cells allows users to extract theme‑related data from an Excel file. For example, you can extract the theme name applied to the workbook and theme colors applied to cells or cell borders, etc.
You can apply a theme to your workbook using Microsoft Excel via Page Layout > Themes command.
JavaScript code to extract theme data from Excel file
The following sample code extracts the theme name applied to the source workbook and then extracts the theme color applied to cell A1 and the theme color applied to the bottom border of the cell.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
<meta charset="utf-8" />
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
#downloadLink { display: none; margin-top: 10px; }
#result p { margin: 6px 0; }
</style>
</head>
<body>
<h1>Aspose.Cells Theme & Color Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, BorderType, 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');
const resultDiv = document.getElementById('result');
resultDiv.innerHTML = '';
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();
// Create workbook object from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Extract theme name applied to this workbook
const themeName = workbook.theme;
resultDiv.innerHTML += `<p>Workbook theme: ${themeName}</p>`;
// Access first worksheet
const worksheet = workbook.worksheets.get(0);
// Access cell A1
const cell = worksheet.cells.get("A1");
// Get the style object
const style = cell.style;
if (style.foregroundThemeColor != null) {
// Extract the theme color applied to this cell if the theme has a foreground theme color defined
const fgColorType = style.foregroundThemeColor.colorType;
resultDiv.innerHTML += `<p>Foreground theme color type: ${fgColorType}</p>`;
} else {
resultDiv.innerHTML += `<p>Theme does not have a foreground color defined.</p>`;
}
// Extract theme color applied to the bottom border of the cell if the theme has a border color defined
const bot = style.borders.get(BorderType.BottomBorder);
if (bot.themeColor != null) {
const botColorType = bot.themeColor.colorType;
resultDiv.innerHTML += `<p>Bottom border theme color type: ${botColorType}</p>`;
} else {
resultDiv.innerHTML += `<p>Theme does not have a border color defined.</p>`;
}
// No file is produced here, but keep download link hidden
const downloadLink = document.getElementById('downloadLink');
downloadLink.style.display = 'none';
downloadLink.href = '#';
downloadLink.textContent = '';
});
</script>
</html>