Impostazioni di riempimento
Colori e motivi di sfondo
Microsoft Excel può impostare i colori del primo piano (contorno) e lo sfondo (riempimento) delle celle e i motivi di sfondo.
Aspose.Cells supporta anche queste funzionalità in modo flessibile. In questo argomento, impariamo ad utilizzare queste funzionalità utilizzando Aspose.Cells.
Impostazione di colori e motivi di sfondo
Aspose.Cells fornisce una classe, Workbook, che rappresenta un file Microsoft Excel. La classe Workbook contiene una raccolta worksheets che consente l’accesso a ogni foglio di lavoro nel file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una raccolta cells. Ogni elemento nella raccolta cells rappresenta un oggetto della classe Cell.
La proprietà Cell ha la proprietà style che viene utilizzata per ottenere e impostare la formattazione di una cella. La classe Style fornisce proprietà per impostare i colori di primo piano e di sfondo delle celle. Aspose.Cells fornisce un’enumerazione BackgroundType che contiene un set di tipi predefiniti di pattern di sfondo come elencato di seguito.
| Motivi di sfondo | Descrizione |
|---|---|
| DiagonalCrosshatch | Rappresenta un motivo a croce diagonale |
| DiagonalStripe | Rappresenta un motivo a strisce diagonali |
| Gray6 | Rappresenta un motivo grigio al 6.25% |
| Gray12 | Rappresenta un motivo grigio al 12.5% |
| Gray25 | Rappresenta un motivo grigio al 25% |
| Gray50 | Rappresenta 50% modello grigio |
| Gray75 | Rappresenta 75% modello grigio |
| HorizontalStripe | Rappresenta modello a strisce orizzontali |
| None | Rappresenta nessuno sfondo |
| ReverseDiagonalStripe | Rappresenta modello a strisce diagonali invertite |
| Solid | Rappresenta modello solido |
| ThickDiagonalCrosshatch | Rappresenta modello spesso di incroci diagonali |
| ThinDiagonalCrosshatch | Rappresenta modello sottile di incroci diagonali |
| ThinDiagonalStripe | Rappresenta modello sottile a strisce diagonali |
| ThinHorizontalCrosshatch | Rappresenta modello sottile di incroci orizzontali |
| ThinHorizontalStripe | Rappresenta modello sottile a strisce orizzontali |
| ThinReverseDiagonalStripe | Rappresenta modello sottile a strisce diagonali invertite |
| ThinVerticalStripe | Rappresenta modello sottile a strisce verticali |
| VerticalStripe | Rappresenta modello a strisce verticali |
Nell’esempio seguente, il colore dell’oggetto A1 è impostato ma A2 è configurato per avere sia colori di primo piano sia di sfondo con un modello di sfondo a strisce verticali.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Example Title</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 } = 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 () => {
// Instantiating a Workbook object
const workbook = new Workbook();
// Adding a new worksheet to the Workbook object
const i = workbook.worksheets.add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.worksheets.get(i);
// Define a Style and get the A1 cell style
let style = worksheet.cells.get("A1").style;
// Setting the foreground color to yellow
style.foregroundColor = AsposeCells.Color.Yellow;
// Setting the background pattern to vertical stripe
style.pattern = AsposeCells.BackgroundType.VerticalStripe;
// Apply the style to A1 cell
worksheet.cells.get("A1").style = style;
// Get the A2 cell style
style = worksheet.cells.get("A2").style;
// Setting the foreground color to blue
style.foregroundColor = AsposeCells.Color.Blue;
// Setting the background color to yellow
style.backgroundColor = AsposeCells.Color.Yellow;
// Setting the background pattern to vertical stripe
style.pattern = AsposeCells.BackgroundType.VerticalStripe;
// Apply the style to A2 cell
worksheet.cells.get("A2").style = style;
// Saving the Excel file
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'book1.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook created and ready for download.</p>';
});
</script>
</html>
Importante sapere
- Per impostare il colore di primo piano o di sfondo di una cella, utilizza le proprietà foregroundColor o backgroundColor dell’oggetto Style. Entrambe avranno effetto solo se la proprietà pattern dell’oggetto Style è configurata.
- La proprietà foregroundColor imposta il colore della sfumatura della cella. La proprietà pattern specifica il tipo di motivo di sfondo utilizzato per il colore di primo piano o di sfondo. Aspose.Cells offre un enumerazione, BackgroundType, che contiene un set di tipi predefiniti di motivi di sfondo.
- Se si seleziona il valore BackgroundType.None dall’enumerazione BackgroundType, il colore di primo piano non viene applicato. Allo stesso modo, il colore di sfondo non viene applicato se si selezionano i valori BackgroundType.None o BackgroundType.Solid.
- Quando si recupera il colore di sfondo di una cella, se style.pattern è BackgroundType.None, style.foregroundColor restituirà Color.Empty.
Applicazione degli effetti di riempimento a sfumatura
Per applicare gli effetti di riempimento gradiente desiderati alla cella, utilizza di conseguenza la proprietà twoColorGradient dell’oggetto Style.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example</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, GradientStyleType, TextAlignmentType } = 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');
let workbook;
if (fileInput.files.length) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
workbook = new Workbook();
}
const worksheet = workbook.worksheets.get(0);
worksheet.cells.get(2, 1).putValue("test");
const style = worksheet.cells.get("B3").style;
style.isGradient = true;
style.twoColorGradient = [ new Color(255, 255, 255), new Color(79, 129, 189), GradientStyleType.Horizontal, 1 ];
style.font.color = Color.Red;
style.horizontalAlignment = TextAlignmentType.Center;
style.verticalAlignment = TextAlignmentType.Center;
worksheet.cells.get("B3").style = style;
worksheet.cells.rowHeightPixel = [2, 53];
worksheet.cells.merge(2, 1, 1, 2);
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Colori e Palette
Una palette è il numero di colori disponibili per creare un’immagine. L’uso di una palette standardizzata in una presentazione consente all’utente di creare un aspetto costante. Ogni file di Microsoft Excel (97-2003) ha una palette di 56 colori che possono essere applicati a celle, caratteri, linee guida, oggetti grafici, riempimenti e linee in un grafico.
Con Aspose.Cells è possibile utilizzare non solo i colori esistenti nella palette ma anche colori personalizzati. Prima di utilizzare un colore personalizzato, aggiungilo prima alla palette.
Questo argomento tratta come aggiungere colori personalizzati alla palette.
Aggiunta colori personalizzati alla palette
Aspose.Cells supporta la palette a 56 colori di Microsoft Excel. Per utilizzare un colore personalizzato non definito nella palette, aggiungi il colore alla palette.
Aspose.Cells fornisce una classe, Workbook, che rappresenta un file Microsoft Excel. La classe Workbook fornisce un metodo changePalette che accetta i seguenti parametri per aggiungere un colore personalizzato e modificare la palette:
- Colore personalizzato, il colore personalizzato da aggiungere.
- Indice, l’indice del colore nella palette che il colore personalizzato sostituirà. Dovrebbe essere compreso tra 0 e 55.
Nell’esempio seguente viene aggiunto un colore personalizzato (Orchidea) alla palette prima di applicarlo a un carattere.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Example Title</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');
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();
// Instantiating a Workbook object by opening the Excel file from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Checks if a color is in the palette for the spreadsheet.
console.log(workbook.isColorInPalette(AsposeCells.Color.Orchid));
// Adding Orchid color to the palette at 55th index
workbook.changePalette(AsposeCells.Color.Orchid, 55);
console.log(workbook.isColorInPalette(AsposeCells.Color.Orchid));
// Adding a new worksheet to the Excel object
const i = workbook.worksheets.add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.worksheets.get(i);
// Accessing the "A1" cell from the worksheet
const cell = worksheet.cells.get("A1");
// Adding some value to the "A1" cell
cell.putValue("Hello Aspose!");
// Defining new Style object
const styleObject = workbook.createStyle();
// Setting the Orchid (custom) color to the font
styleObject.font.color = workbook.colors[55];
// Applying the style to the cell
cell.style = styleObject;
// Saving the Excel file
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'out.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Result';
document.getElementById('result').innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>