Ausrichtungseinstellungen

Konfigurieren von Ausrichtungseinstellungen

Ausrichtungseinstellungen in Microsoft Excel

Jeder, der Microsoft Excel verwendet hat, um Zellen zu formatieren, wird mit den Ausrichtungseinstellungen in Microsoft Excel vertraut sein.

Wie Sie aus der obigen Abbildung sehen können, gibt es verschiedene Arten von Ausrichtungsoptionen:

  • Textausrichtung (horizontal & vertikal)
  • Einrückung
  • Orientierung
  • Textkontrolle
  • Textrichtung

Alle diese Ausrichtungseinstellungen werden vollständig von Aspose.Cells unterstützt und werden im Folgenden näher erläutert.

Ausrichtungseinstellungen in Aspose.Cells

Aspose.Cells stellt die Klasse Workbook bereit, die eine Excel-Datei repräsentiert. Die Klasse Workbook enthält eine Worksheets-Sammlung, die Zugriff auf jedes Arbeitsblatt in der Excel-Datei ermöglicht. Ein Arbeitsblatt wird durch die Klasse Worksheet repräsentiert. Die Klasse Worksheet bietet eine getCells()-Sammlung. Jedes Element in der Sammlung Cells stellt ein Objekt der Klasse Cell dar.

Aspose.Cells stellt die Methoden getStyle und setStyle für die Klasse Cell bereit, um die Formatierung einer Zelle abzurufen und zu setzen. Die Klasse Style bietet nützliche Eigenschaften zur Konfiguration der Ausrichtungsoptionen.

Wählen Sie einen beliebigen Text-Ausrichtungstyp mit dem TextAlignmentType-Enum. Die vordefinierten Text-Ausrichtungstypen im TextAlignmentType-Enum sind:

Textausrichtungstypen Beschreibung
Bottom Stellt die untere Textausrichtung dar
Center Stellt die zentrale Textausrichtung dar
CenterAcross Stellt die zentrale überkreuzte Textausrichtung dar
Distributed Stellt die verteilte Textausrichtung dar
Fill Stellt die Fülltextausrichtung dar
General Stellt die allgemeine Textausrichtung dar
Justify Stellt die Textausrichtung als blocksatz dar
Left Stellt die linksbündige Textausrichtung dar
Right Stellt die rechtsbündige Textausrichtung dar
Top Stellt die obere Textausrichtung dar
JustifiedLow Richtet den Text mit einer angepassten Kachidalänge für arabischen Text aus.
ThaiDistributed Verteilt insbesondere thailändischen Text, da jeder Buchstabe als Wort behandelt wird.

Horizontale Ausrichtung

Verwenden Sie die Style-setHorizontalAlignment-Methode des Objekts Style, um den Text horizontal auszurichten.

const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create directory if it is not already present.
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir);
}
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Obtaining the reference of the 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("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
const style = cell.getStyle();
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);
cell.setStyle(style);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003);

Vertikale Ausrichtung

Ähnlich wie bei der horizontalen Ausrichtung verwenden Sie die Style-setVerticalAlignment-Methode des Objekts Style, um den Text vertikal auszurichten.

const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create directory if it is not already present.
if (!require("fs").existsSync(dataDir)) {
require("fs").mkdirSync(dataDir);
}
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Clearing all the worksheets
workbook.getWorksheets().clear();
// Adding a new worksheet to the Excel object
const i = workbook.getWorksheets().add();
// Obtaining the reference of the newly added 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("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
const style = cell.getStyle();
// Setting the vertical alignment of the text in a cell
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);
cell.setStyle(style);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003);

Einrückung

Es ist möglich, die Einrückungsebene des Textes in einer Zelle mit der Style-setIndentLevel-Methode des Objekts Style zu setzen.

const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create directory if it is not already present.
if (!require("fs").existsSync(dataDir)) {
require("fs").mkdirSync(dataDir);
}
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Obtaining the reference of the 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("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
const style = cell.getStyle();
// Setting the indentation level of the text (inside the cell) to 2
style.setIndentLevel(2);
cell.setStyle(style);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003);

Ausrichtung

Setzen Sie die Orientierung (Drehung) des Textes in einer Zelle mit der Style-setRotationAngle-Methode des Objekts Style.

const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create directory if it is not already present.
if (!require("fs").existsSync(dataDir)) {
require("fs").mkdirSync(dataDir);
}
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Obtaining the reference of the 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("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
const style = cell.getStyle();
// Setting the rotation of the text (inside the cell) to 25
style.setRotationAngle(25);
cell.setStyle(style);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003);

Textsteuerung

Im Folgenden wird erläutert, wie Sie Text steuern können, indem Sie Textrahmen, Anpassung an die Größe und andere Formatierungsoptionen festlegen.

Textumschlag

Das Textumbruch-Feature in einer Zelle erleichtert das Lesen: Die Höhe der Zelle passt sich an, um den gesamten Text aufzunehmen, anstatt ihn abzuschneiden oder in angrenzende Zellen auslaufen zu lassen. Aktivieren oder deaktivieren Sie den Textumbruch mit der Style-setIsTextWrapped(boolean)-Methode des Objekts Style.

const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create Workbook Object
const wb = new AsposeCells.Workbook();
// Open first Worksheet in the workbook
const ws = wb.getWorksheets().get(0);
// Get Worksheet Cells Collection
const cell = ws.getCells();
// Increase the width of First Column Width
cell.setColumnWidth(0, 35);
// Increase the height of first row
cell.setRowHeight(0, 36);
// Add Text to the First Cell
cell.checkCell(0, 0).putValue("I am using the latest version of Aspose.Cells to test this functionality");
// Make Cell's Text wrap
const style = cell.checkCell(0, 0).getStyle();
style.setIsTextWrapped(true);
cell.checkCell(0, 0).setStyle(style);
// Save Excel File
wb.save(path.join(dataDir, "WrappingText.out.xlsx"));
Anpassen an Größe

Eine Option zum Textumbruch in einem Feld ist, die Textgröße so zu verkleinern, dass sie in die Zelle passt. Das erfolgt durch Setzen der Style-setShrinkToFit(boolean)-Methode des Objekts Style auf wahr.

const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create directory if it is not already present.
if (!require("fs").existsSync(dataDir)) {
require("fs").mkdirSync(dataDir);
}
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Obtaining the reference of the 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("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
const style = cell.getStyle();
// Shrinking the text to fit according to the dimensions of the cell
style.setShrinkToFit(true);
cell.setStyle(style);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003);
Zellen zusammenführen

Wie Microsoft Excel unterstützt Aspose.Cells das Zusammenfassen mehrerer Zellen zu einer. Aspose.Cells bietet zwei Ansätze hierfür. Eine Möglichkeit ist, die Cells-merge-Methode der Cells-Sammlung aufzurufen. Die merge-Methode nimmt die folgenden Parameter, um die Zellen zu verbinden:

  • Erste Zeile: Die erste Zeile, ab der das Zusammenführen beginnt.
  • Erste Spalte: Die erste Spalte, ab der das Zusammenführen beginnt.
  • Anzahl der Zeilen: Die Anzahl der zu zusammenführenden Zeilen.
  • Anzahl der Spalten: Die Anzahl der zu zusammenführenden Spalten.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create directory if it is not already present.
if (!require("fs").existsSync(dataDir)) {
require("fs").mkdirSync(dataDir);
}
// Create a Workbook.
const wbk = new AsposeCells.Workbook();
// Create a Worksheet and get the first sheet.
const worksheet = wbk.getWorksheets().get(0);
// Create a Cells object to fetch all the cells.
const cells = worksheet.getCells();
// Merge some Cells (C6:E7) into a single C6 Cell.
cells.merge(5, 2, 2, 3);
// Input data into C6 Cell.
cells.get(5, 2).putValue("This is my value");
// Create a Style object to fetch the Style of C6 Cell.
const style = cells.get(5, 2).getStyle();
// Create a Font object
const font = style.getFont();
// Set the name.
font.setName("Times New Roman");
// Set the font size.
font.setSize(18);
// Set the font color
font.setColor(AsposeCells.Color.Blue);
// Bold the text
font.setIsBold(true);
// Make it italic
font.setIsItalic(true);
// Set the background color of C6 Cell to Red
style.setForegroundColor(AsposeCells.Color.Red);
style.setPattern(AsposeCells.BackgroundType.Solid);
// Apply the Style to C6 Cell.
cells.get(5, 2).setStyle(style);
// Save the Workbook.
wbk.save(path.join(dataDir, "mergingcells.out.xls"));

Der andere Weg ist, zuerst die Cells-createRange-Methode der Sammlung Cells aufzurufen, um einen Bereich der zu verbindenden Zellen zu erstellen. Die createRange-Methode nimmt denselben Parameter wie die oben diskutierte merge-Methode und gibt ein Range-Objekt zurück. Das Range-Objekt bietet außerdem eine merge-Methode, die den Bereich, der im Range-Objekt angegeben ist, verbindet.

Textausrichtung

Es ist möglich, die Lesereihenfolge von Text in Zellen festzulegen. Die Lesereihenfolge gibt die visuelle Reihenfolge an, in der Zeichen, Wörter usw. angezeigt werden. Zum Beispiel ist Englisch eine von links nach rechts lesbare Sprache, während Arabisch eine von rechts nach links lesbare Sprache ist.

Die Lese-Reihenfolge wird mit der Style-TextDirection-Eigenschaft des Objekts Style gesetzt. Aspose.Cells bietet vordefinierte Textrichtungsarten im TextDirectionType-Enum.

Text Direction Types Beschreibung
Context Die Lese-Reihenfolge, die mit der Sprache des ersten eingegebenen Zeichens übereinstimmt
LeftToRight Lesereihenfolge von links nach rechts
RightToLeft Lesereihenfolge von rechts nach links
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);
// Instantiating a Workbook object
// Obtaining the reference of 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("I am using the latest version of Aspose.Cells to test this functionality.");
// Gets style in the "A1" cell
const style = cell.getStyle();
// Shrinking the text to fit according to the dimensions of the cell
style.setTextDirection(AsposeCells.TextDirectionType.LeftToRight);
cell.setStyle(style);
// Saving the Excel file
workbook.save("book1.xlsx");

Erweiterte Themen