Ändern Sie das Format einer Zelle
Mögliche Verwendungsszenarien
Wenn Sie bestimmte Daten hervorheben möchten, können Sie den Stil der Zellen ändern.
Wie man das Format einer Zelle in Excel ändert
Um das Format einer einzelnen Zelle in Excel zu ändern, befolgen Sie diese Schritte:
-
Öffnen Sie Excel und öffnen Sie die Arbeitsmappe, die die Zelle enthält, die Sie formatieren möchten.
-
Suchen Sie die Zelle, die Sie formatieren möchten.
-
Klicken Sie mit der rechten Maustaste auf die Zelle und wählen Sie “Zellen formatieren” aus dem Kontextmenü. Alternativ können Sie die Zelle auswählen, zum Register Start in der Excel-Befehlsleiste gehen, auf die Dropdown-Schaltfläche “Format” in der Gruppe “Zellen” klicken und “Zellen formatieren” auswählen.
-
Das Dialogfeld “Zellen formatieren” wird angezeigt. Hier können Sie verschiedene Formatierungsoptionen auswählen, die auf die ausgewählte Zelle angewendet werden sollen. Sie können z.B. die Schriftart, die Schriftgröße, die Schriftfarbe, das Zahlenformat, die Rahmen, die Hintergrundfarbe usw. ändern. Erkunden Sie die verschiedenen Registerkarten im Dialogfeld, um auf verschiedene Formatierungsoptionen zuzugreifen.
-
Klicken Sie nach dem Anwenden der gewünschten Formatierungsänderungen auf die Schaltfläche “OK”, um die Formatierung auf die ausgewählte Zelle anzuwenden.
So ändern Sie das Format einer Zelle mit Node.js
Um das Format einer Zelle mit Aspose.Cells zu ändern, können Sie die folgenden Methoden verwenden:
Beispielcode
In diesem Beispiel erstellen wir ein Excel-Arbeitsbuch, fügen einige Beispieldaten hinzu, greifen auf das erste Arbeitsblatt zu und holen zwei Zellen («A2» und «B3»). Dann holen wir den Stil der Zelle, setzen verschiedene Formatierungsoptionen (z. B. Schriftfarbe, fett) und ändern das Format der Zelle. Schließlich speichern wir das Arbeitsbuch in eine neue Datei.
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
const filePath = path.join(dataDir, "sample.xlsx"); | |
// Loads the workbook which contains hidden external links | |
const workbook = new AsposeCells.Workbook(filePath); | |
// Get the first worksheet | |
const ws = workbook.getWorksheets().get(0); | |
const cells = ws.getCells(); | |
// Setting the value to the cells | |
let cell = cells.get("A1"); | |
cell.putValue("Fruit"); | |
cell = cells.get("B1"); | |
cell.putValue("Count"); | |
cell = cells.get("C1"); | |
cell.putValue("Price"); | |
cell = cells.get("A2"); | |
cell.putValue("Apple"); | |
cell = cells.get("A3"); | |
cell.putValue("Mango"); | |
cell = cells.get("A4"); | |
cell.putValue("Blackberry"); | |
cell = cells.get("A5"); | |
cell.putValue("Cherry"); | |
cell = cells.get("B2"); | |
cell.putValue(5); | |
cell = cells.get("B3"); | |
cell.putValue(3); | |
cell = cells.get("B4"); | |
cell.putValue(6); | |
cell = cells.get("B5"); | |
cell.putValue(4); | |
cell = cells.get("C2"); | |
cell.putValue(5); | |
cell = cells.get("C3"); | |
cell.putValue(20); | |
cell = cells.get("C4"); | |
cell.putValue(30); | |
cell = cells.get("C5"); | |
cell.putValue(60); | |
// Access the worksheet | |
const worksheet = workbook.getWorksheets().get(0); | |
const a2 = worksheet.getCells().get("A2"); | |
// Get style of A2 | |
const style = a2.getStyle(); | |
// Change the format | |
style.getFont().setColor(AsposeCells.Color.Red); | |
style.getFont().setIsBold(true); | |
const flag = new AsposeCells.StyleFlag(); | |
flag.setFontColor(true); | |
a2.setStyle(style, flag); | |
const b3 = worksheet.getCells().get("B3"); | |
// Get style of B3 | |
const style2 = b3.getStyle(); | |
// Change the format | |
style2.getFont().setColor(AsposeCells.Color.Blue); | |
style2.getFont().setIsItalic(true); | |
b3.setStyle(style2); | |
// Save the modified workbook | |
workbook.save("output.xlsx"); |