Formatieren von Zeilen und Spalten mit Node.js über C++
Arbeiten mit Zeilen
Wie man die Zeilenhöhe anpasst
Aspose.Cells stellt eine Klasse, Workbook, bereit, die eine Microsoft Excel-Datei repräsentiert. Die Klasse Workbook enthält eine WorksheetCollection, die Zugriff auf jedes Arbeitsblatt in der Excel-Datei ermöglicht. Ein Arbeitsblatt wird durch die Klasse Worksheet dargestellt. Die Klasse Worksheet bietet eine Cells-Sammlung, die alle Zellen im Arbeitsblatt repräsentiert.
Die Sammlung Cells bietet mehrere Methoden zur Verwaltung von Zeilen oder Spalten in einem Arbeitsblatt. Einige davon werden im Folgenden ausführlicher erläutert.
Wie man die Höhe einer Zeile festlegt
Es ist möglich, die Höhe einer einzelnen Zeile festzulegen, indem die Cells-Sammlungsmethode setRowHeight(number, number) aufgerufen wird. Die setRowHeight(number, number)-Methode übernimmt folgende Parameter:
- Zeilenindex, der Index der Zeile, deren Höhe geändert wird.
- Zeilenhöhe, die auf die Zeile anzuwendende Zeilenhöhe.
try {
const path = require("path");
const fs = require("fs");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "book1.xls");
// Creating a file stream containing the Excel file to be opened
const fstream = fs.createReadStream(filePath);
// Reading the file stream into a buffer
const chunks = [];
fstream.on('data', chunk => chunks.push(chunk));
fstream.on('end', () => {
const buffer = Buffer.concat(chunks);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(buffer);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Setting the height of the second row to 13
worksheet.getCells().setRowHeight(1, 13);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));
// Closing the file stream to free all resources
fstream.close();
Wie man die Höhe aller Zeilen in einem Arbeitsblatt festlegt
Wenn Entwickler die gleiche Zeilenhöhe für alle Zeilen im Arbeitsblatt festlegen müssen, können sie dies durch Verwendung der getStandardHeight()-Eigenschaft der Cells-Sammlung tun.
Beispiel:
const fs = require("fs");
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, "book1.xls");
// Creating a file stream containing the Excel file to be opened
const fstream = fs.readFileSync(filePath);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fstream);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Setting the height of all rows in the worksheet to 15
worksheet.getCells().setStandardHeight(15);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));
// Closing the file stream to free all resources
// (Note: Closing the file stream is unnecessary in this context as it's a
// synchronous read performed using fs.readFileSync, which does not require
// explicit closure, but if using fs.createReadStream, you would handle it accordingly)
Arbeiten mit Spalten
Wie man die Breite einer Spalte festlegt
Setzen Sie die Breite einer Spalte, indem Sie die Cells-Sammlungsmethode setColumnWidth(number, number) aufrufen. Die setColumnWidth(number, number)-Methode erfordert folgende Parameter:
- Spaltenindex, der Index der Spalte, deren Breite geändert wird.
- Spaltenbreite, die gewünschte Spaltenbreite.
const fs = require("fs");
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, "book1.xls");
// Creating a file stream containing the Excel file to be opened
const fstream = fs.readFileSync(filePath);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fstream);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Setting the width of the second column to 17.5
worksheet.getCells().setColumnWidth(1, 17.5);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));
// Closing the file stream to free all resources
fstream; // Note: No explicit close needed for fs.readFileSync
Wie man die Spaltenbreite in Pixeln festlegt
Setzen Sie die Breite einer Spalte, indem Sie die Cells-Sammlungsmethode setColumnWidthPixel(number, number) aufrufen. Die setColumnWidthPixel(number, number)-Methode erfordert folgende Parameter:
- Spaltenindex, der Index der Spalte, deren Breite geändert wird.
- Spaltenbreite, die gewünschte Spaltenbreite in Pixel.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// Source directory
const sourceDir = path.join(__dirname, "data");
const outDir = path.join(__dirname, "output");
// Load source Excel file
const workbook = new AsposeCells.Workbook(path.join(sourceDir, "Book1.xlsx"));
// Access first worksheet
const worksheet = workbook.getWorksheets().get(0);
// Set the width of the column in pixels
worksheet.getCells().setColumnWidthPixel(7, 200);
workbook.save(path.join(outDir, "SetColumnWidthInPixels_Out.xlsx"));
Wie man die Breite aller Spalten in einem Arbeitsblatt festlegt
Um die gleiche Spaltenbreite für alle Spalten im Arbeitsblatt festzulegen, verwenden Sie die Cells-Sammlungseigenschaft getStandardWidth().
const path = require("path");
const fs = require("fs");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Creating a file stream containing the Excel file to be opened
const filePath = path.join(dataDir, "book1.xls");
// Instantiating a Workbook object
// Opening the Excel file
const workbook = new AsposeCells.Workbook(filePath);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Setting the width of all columns in the worksheet to 20.5
worksheet.getCells().setStandardWidth(20.5);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));
// Closing the file stream to free all resources
// No explicit close needed in Node.js
Erweiterte Themen
- Automatische Anpassung von Zeilen und Spalten
- Text in Spalten konvertieren mit Aspose.Cells
- Kopieren von Zeilen und Spalten
- Leere Zeilen und Spalten in einem Arbeitsblatt löschen
- Gruppieren und Aufheben der Gruppierung von Zeilen und Spalten
- Zeilen und Spalten ausblenden und anzeigen
- Zeilen in einem Excel-Arbeitsblatt einfügen oder löschen
- Einfügen und Löschen von Zeilen und Spalten einer Excel-Datei
- Duplikate Zeilen in einem Arbeitsblatt entfernen
- Aktualisieren Sie Verweise in anderen Arbeitsblättern, während Sie leere Spalten und Zeilen in einem Arbeitsblatt löschen