Formatta righe e colonne con Node.js tramite C++
Lavorare con le righe
Come regolare l’altezza della riga
Aspose.Cells fornisce una classe, Workbook, che rappresenta un file Microsoft Excel. La classe Workbook contiene un WorksheetCollection che permette l’accesso a ogni foglio di lavoro nel file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una collezione Cells che rappresenta tutte le celle nel foglio di lavoro.
La collezione Cells fornisce diversi metodi per gestire righe o colonne in un foglio di lavoro. Alcuni di questi sono discussi più dettagliatamente di seguito.
Come impostare l’altezza di una riga
È possibile impostare l’altezza di una singola riga chiamando il metodo setRowHeight(number, number) della collezione Cells. Il metodo setRowHeight(number, number) prende i seguenti parametri:
- Indice di riga, l’indice della riga a cui si sta modificando l’altezza.
- Altezza della riga, l’altezza della riga da applicare alla riga.
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();
Come impostare l’altezza di tutte le righe in un foglio di lavoro
Se gli sviluppatori devono impostare la stessa altezza di riga per tutte le righe del foglio di lavoro, possono farlo utilizzando la proprietà getStandardHeight() della raccolta Cells.
Esempio:
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)
Lavorare con colonne
Come impostare la larghezza di una colonna
Imposta la larghezza di una colonna chiamando il metodo setColumnWidth(number, number) della raccolta Cells. Il metodo setColumnWidth(number, number) accetta i seguenti parametri:
- Indice di colonna, l’indice della colonna a cui si sta modificando la larghezza.
- Larghezza di colonna, la larghezza desiderata della colonna.
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
Come impostare la larghezza della colonna in pixel
Imposta la larghezza di una colonna chiamando il metodo setColumnWidthPixel(number, number) della raccolta Cells. Il metodo setColumnWidthPixel(number, number) accetta i seguenti parametri:
- Indice di colonna, l’indice della colonna a cui si sta modificando la larghezza.
- Larghezza della colonna, la larghezza della colonna desiderata 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"));
Come impostare la larghezza di tutte le colonne in un foglio di lavoro
Per impostare la stessa larghezza di colonna per tutte le colonne nel foglio di lavoro, usa la proprietà getStandardWidth() della raccolta Cells.
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
Argomenti avanzati
- Adatta automaticamente righe e colonne
- Converti testo in colonne utilizzando Aspose.Cells
- Copia righe e colonne
- Elimina righe e colonne vuote in un foglio di lavoro
- Raggruppa e scollega righe e colonne
- Nascondi e mostra righe e colonne
- Inserisci o elimina righe in un foglio di lavoro di Excel
- Inserimento ed eliminazione di righe e colonne di un file di Excel
- Rimuovere righe duplicate in un foglio di lavoro
- Aggiorna i riferimenti in altri fogli di lavoro mentre elimini colonne e righe vuote in un foglio di lavoro