Reusing Style Objects
Contents
[
Hide
]
Reusing style objects can save memory and make a program faster.
To apply some formatting to a large range of cells in a worksheet:
- Create a style object.
- Specify the attributes.
- Apply the style to the cells in the range.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example - Create Workbook and Set Font</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, Color } = 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 () => {
// Creating a new workbook (empty)
const workbook = new Workbook();
// Access the first worksheet
const worksheet = workbook.worksheets.get(0);
// Access cells
const cell1 = worksheet.cells.get("A1");
const cell2 = worksheet.cells.get("B1");
// Set the styles of both cells to Times New Roman
const styleObject = workbook.createStyle();
styleObject.font.color = Color.Red;
styleObject.font.name = "Times New Roman";
cell1.style = styleObject;
cell2.style = styleObject;
// Put the values inside the cell
cell1.value = "Hello World!";
cell2.value = "Hello World!!";
// Save to XLSX
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'SampleOutput_out.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook created and saved successfully. Click the download link to get the file.</p>';
});
</script>
</html>
Because the Cell.style / Cell.style approach uses a lot less memory, and is efficient, the older Cell.style property which consumed a lot of unnecessary memory was removed with the release of Aspose.Cells 7.1.0.