Generate Conditional Formatting DataBars Images
Contents
[
Hide
]
Sometimes, you need to generate images of Conditional Formatting DataBars. You can use Aspose.Cells DataBar.toImage(Cell, ImageOrPrintOptions) method to generate these images. This article shows how to generate a DataBar image using Aspose.Cells.
The following sample code generates the DataBar image of cell C1. First, it accesses the format condition object of the cell, and then from that object, it accesses the DataBar object and uses its DataBar.toImage(Cell, ImageOrPrintOptions) method to generate the image of the cell. Finally, it saves the image on disk.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Generate DataBar Image</title>
</head>
<body>
<h1>Generate DataBar Image Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx" />
<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, 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();
// Creating a workbook from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access first worksheet
const worksheet = workbook.worksheets.get(0);
// Access the cell which contains conditional formatting databar
const cell = worksheet.cells.get("C1");
// Create and get the conditional formatting of the worksheet
const idx = worksheet.conditionalFormattings.add();
const fcc = worksheet.conditionalFormattings.get(idx);
fcc.addCondition(AsposeCells.FormatConditionType.DataBar);
fcc.addArea(AsposeCells.CellArea.createCellArea("C1", "C4"));
// Access the conditional formatting databar
const dbar = fcc.get(0).dataBar;
// Create image or print options
const opts = new AsposeCells.ImageOrPrintOptions();
opts.imageType = AsposeCells.ImageType.Png;
// Get the image bytes of the databar
const imgBytes = dbar.toImage(cell, opts);
// Create a blob and provide download link
const blob = new Blob([imgBytes], { type: "image/png" });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'outputGenerateDatabarImage.png';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Generated DataBar Image';
document.getElementById('result').innerHTML = '<p style="color: green;">Image generated successfully! Click the download link to save the PNG file.</p>';
});
</script>
</html>