Zellen mit Node.js via C++ formatieren
Einführung
Wie man Zellen mit den Methoden GetStyle und SetStyle formatiert
Auf Zellen verschiedene Arten von Formatierungsstilen anwenden, um Hintergrund- oder Vordergrundfarben, Rahmen, Schriftarten, horizontale und vertikale Ausrichtungen, Einrückungsebene, Textausrichtung, Drehwinkel und vieles mehr festzulegen.
Wie man die GetStyle und SetStyle Methoden verwendet
Wenn Entwickler unterschiedliche Formatierungsstile auf verschiedene Zellen anwenden müssen, ist es besser, das Style der Zelle mit der Cell.getStyle()-Methode zu erhalten, die Stil-Attribute zu spezifizieren und dann die Formatierung mit der Cell.setStyle(Style)-Methode anzuwenden. Ein Beispiel zeigt, wie dieser Ansatz angewendet wird, um verschiedene Formatierungen auf eine Zelle anzuwenden.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Obtaining the reference of the first worksheet
const worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
const cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("Hello Aspose!");
// Defining a Style object
let style;
// Get the style from A1 cell
style = cell.getStyle();
// Setting the vertical alignment
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the horizontal alignment
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the font color of the text
style.getFont().setColor(AsposeCells.Color.Green);
// Setting to shrink according to the text contained in it
style.setShrinkToFit(true);
// Setting the bottom border color to red
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Red);
// Setting the bottom border type to medium
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Medium);
// Applying the style to A1 cell
cell.setStyle(style);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));
Wie man das Style-Objekt verwendet, um verschiedene Zellen zu formatieren
Wenn Entwickler denselben Formatierungsstil auf verschiedene Zellen anwenden möchten, können sie das Style-Objekt verwenden. Folgen Sie den untenstehenden Schritten, um das Style-Objekt zu verwenden:
- Fügen Sie ein Style-Objekt hinzu, indem Sie die createStyle()-Methode der Workbook-Klasse aufrufen
- Greifen Sie auf das neu hinzugefügte Style-Objekt zu
- Legen Sie die gewünschten Eigenschaften/Attribute des Style-Objekts fest, um die gewünschten Formatierungseinstellungen anzuwenden
- Weisen Sie das konfigurierte Style-Objekt Ihren gewünschten Zellen zu
Dieser Ansatz kann die Effizienz Ihrer Anwendungen erheblich verbessern und auch Speicherplatz sparen.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Adding a new worksheet to the Excel object
const i = workbook.getWorksheets().add();
// Obtaining the reference of the first worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(i);
// Accessing the "A1" cell from the worksheet
const cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("Hello Aspose!");
// Adding a new Style
const style = workbook.createStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the font color of the text in the "A1" cell
style.getFont().setColor(AsposeCells.Color.Green);
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border color of the cell to red
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Red);
// Setting the bottom border type of the cell to medium
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Medium);
// Assigning the Style object to the "A1" cell
cell.setStyle(style);
// Apply the same style to some other cells
worksheet.getCells().get("B1").setStyle(style);
worksheet.getCells().get("C1").setStyle(style);
worksheet.getCells().get("D1").setStyle(style);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));
Wie man die Microsoft Excel 2007 vordefinierten Stile verwendet
Wenn Sie unterschiedliche Formatierungsstile für Microsoft Excel 2007 anwenden müssen, wenden Sie Stile mithilfe der Aspose.Cells API an. Ein Beispiel unten zeigt diesen Ansatz zur Anwendung eines vordefinierten Stils auf einer Zelle.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiate a new Workbook.
const workbook = new AsposeCells.Workbook();
// Create a style object.
const style = workbook.createStyle();
// Input a value to A1 cell.
workbook.getWorksheets().get(0).getCells().get("A1").putValue("Test");
// Apply the style to the cell.
workbook.getWorksheets().get(0).getCells().get("A1").setStyle(style);
// Save the Excel 2007 file.
workbook.save(path.join(dataDir, "book1.out.xlsx"));
Wie man ausgewählte Zeichen in einer Zelle formatiert
Der Umgang mit Schriftart-Einstellungen erklärt, wie Text in Zellen formatiert wird, aber es erklärt nur, wie der gesamte Zellinhalt formatiert wird. Was ist, wenn Sie nur bestimmte Zeichen formatieren möchten?
Aspose.Cells unterstützt diese Funktion ebenfalls. Dieser Artikel erklärt, wie diese Funktion effektiv genutzt werden kann.
Wie man ausgewählte Zeichen formatiert
Aspose.Cells stellt die Klasse Workbook bereit, die eine Microsoft Excel-Datei repräsentiert. Die Workbook-Klasse enthält eine getWorksheets()-Sammlung, die den Zugriff auf jedes Tabellenblatt in einer Excel-Datei ermöglicht. Ein Tabellenblatt wird durch die Worksheet-Klasse repräsentiert. Die Worksheet-Klasse bietet eine getCells()-Sammlung. Jedes Element in der getCells()-Sammlung repräsentiert ein Objekt der Cell-Klasse.
Die Cell-Klasse bietet die characters(number, number)-Methode, die die folgenden Parameter akzeptiert, um einen Zeichenbereich in einer Zelle auszuwählen:
- Startindex: Der Index des Zeichens, von dem die Auswahl beginnt.
- Anzahl der Zeichen: Die Anzahl der ausgewählten Zeichen.
Die characters(number, number)-Methode gibt eine Instanz der FontSetting-Klasse zurück, die es Entwicklern ermöglicht, die Charaktere auf die gleiche Weise zu formatieren wie eine Zelle, wie im folgenden Codebeispiel gezeigt. Im Ausgabedokument wird im A1-Feld das Wort ‘Visit’ mit der Standardschriftart formatiert, aber ‘Aspose!’ ist fett und blau.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Obtaining the reference of the first(default) worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
const cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("Visit Aspose!");
// Setting the font of selected characters to bold
const font = cell.characters(6, 7).getFont();
font.isBold = true;
// Setting the font color of selected characters to blue
font.color = AsposeCells.Color.Blue;
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));
Wie man Zeilen und Spalten formatiert
Manchmal müssen Entwickler dieselbe Formatierung auf Zeilen oder Spalten anwenden. Die Formatierung einzelner Zellen nacheinander dauert oft länger und ist keine gute Lösung. Um dieses Problem zu lösen, bietet Aspose.Cells einen einfachen, schnellen Weg, der in diesem Artikel ausführlich erörtert wird.
Formatierung von Zeilen & Spalten
Aspose.Cells bietet die Klasse Workbook, die eine Microsoft Excel-Datei repräsentiert. Die Workbook-Klasse enthält eine getWorksheets()-Sammlung, die den Zugriff auf jedes Tabellenblatt in der Excel-Datei ermöglicht. Ein Tabellenblatt wird durch die Worksheet-Klasse dargestellt. Die Worksheet-Klasse bietet eine getCells()-Sammlung. Die getCells()-Sammlung bietet eine getRows()-Sammlung.
Wie man eine Zeile formatiert
Jeder Eintrag in der getRows()-Sammlung repräsentiert ein Row-Objekt. Das Row-Objekt bietet die applyStyle(Style, StyleFlag)-Methode, die verwendet wird, um die Zeilenformatierung festzulegen. Um eine gleiche Formatierung auf eine Zeile anzuwenden, verwenden Sie das Style-Objekt. Die unten dargestellten Schritte zeigen, wie es verwendet wird.
- Fügen Sie ein Style-Objekt der Workbook-Klasse hinzu, indem Sie seine createStyle()-Methode aufrufen.
- Legen Sie die Eigenschaften des Style-Objekts fest, um die Formatierungseinstellungen anzuwenden.
- Schalten Sie die relevanten Attribute für das StyleFlag-Objekt ein.
- Weisen Sie das konfigurierte Style-Objekt dem Row-Objekt zu.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(0);
// Adding a new Style to the styles
const style = workbook.createStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the font color of the text in the "A1" cell
style.getFont().setColor(AsposeCells.Color.Green);
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border color of the cell to red
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Red);
// Setting the bottom border type of the cell to medium
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Medium);
// Creating StyleFlag
const styleFlag = new AsposeCells.StyleFlag();
styleFlag.setHorizontalAlignment(true);
styleFlag.setVerticalAlignment(true);
styleFlag.setShrinkToFit(true);
styleFlag.setBorders(true);
styleFlag.setFontColor(true);
// Accessing a row from the Rows collection
const row = worksheet.getCells().getRows().get(0);
// Assigning the Style object to the Style property of the row
row.applyStyle(style, styleFlag);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));
Wie man eine Spalte formatiert
Die getCells()-Sammlung bietet auch eine getColumns()-Sammlung. Jedes Element in der getColumns()-Sammlung repräsentiert ein Column-Objekt. Ähnlich wie bei einem Row-Objekt bietet auch das Column-Objekt die applyStyle(Style, StyleFlag)-Methode zum Formatieren einer Spalte.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(0);
// Adding a new Style to the styles
const style = workbook.createStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the font color of the text in the "A1" cell
style.getFont().setColor(AsposeCells.Color.Green);
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border color of the cell to red
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Red);
// Setting the bottom border type of the cell to medium
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Medium);
// Creating StyleFlag
const styleFlag = new AsposeCells.StyleFlag();
styleFlag.setHorizontalAlignment(true);
styleFlag.setVerticalAlignment(true);
styleFlag.setShrinkToFit(true);
styleFlag.setBorders(true);
styleFlag.setFontColor(true);
// Accessing a column from the Columns collection
const column = worksheet.getCells().getColumns().get(0);
// Applying the style to the column
column.applyStyle(style, styleFlag);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));
Erweiterte Themen
- Ausrichtungseinstellungen
- Rahmeneinstellungen
- Bedingte Formate von Excel- und ODS-Dateien festlegen.
- Excel-Themen und Farben
- Fülleinstellungen
- Schriftarteinstellungen
- Zellenformat in einer Arbeitsmappe
- Implementieren des 1904-Datumsformats
- Zusammenführen und Aufheben der Zellenzusammenführung
- Nummern-Einstellungen
- Stil für Zellen abrufen und festlegen