Générer des images de mise en forme conditionnelle DataBars
Contents
[
Hide
]
Parfois, vous devez générer des images de barres de données de formatage conditionnel. Vous pouvez utiliser la méthode Aspose.Cells DataBar.toImage(Cell, ImageOrPrintOptions) pour générer ces images. Cet article montre comment générer une image de barre de données à l’aide d’Aspose.Cells.
Le code exemple suivant génère l’image de la barre de données de la cellule C1. Il accède d’abord à l’objet de condition de mise en forme de la cellule, puis depuis cet objet, il accède à l’objet DataBar et utilise sa méthode DataBar.toImage(Cell, ImageOrPrintOptions) pour générer l’image de la cellule. Enfin, il enregistre l’image sur le disque.
<!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>