Gestisci i fogli di lavoro dei file Microsoft Excel con Node.js tramite C++

Aspose.Cells fornisce una classe, Workbook che rappresenta un file Excel. La classe Workbook contiene una collezione Workbook.getWorksheets() che consente di accedere a ogni foglio di lavoro nel file Excel.

Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet offre un’ampia gamma di proprietà e metodi per gestire i fogli di lavoro.

Aggiungere fogli di lavoro a un nuovo file Excel

Per creare un nuovo file Excel in modo programmatico:

  1. Crea un oggetto della classe Workbook.
  2. Chiama il metodo WorksheetCollection.add(SheetType) della classe WorksheetCollection. Un foglio di lavoro vuoto viene aggiunto automaticamente al file Excel. Può essere riferito passando l’indice del foglio di lavoro al Workbook.getWorksheets() collezione.
  3. Ottenere un riferimento al foglio di lavoro.
  4. Lavorare sui fogli di lavoro.
  5. Salva il nuovo file Excel con fogli di lavoro nuovi chiamando il metodo Workbook.save(string, SaveFormat) della classe Workbook.
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 Workbook object
const i = workbook.getWorksheets().getCount();
workbook.getWorksheets().add();

// Obtaining the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(i);

// Setting the name of the newly added worksheet
worksheet.setName("My Worksheet");

// Saving the Excel file
workbook.save(path.join(dataDir, "output.out.xls"));

Aggiunta di fogli di lavoro a un foglio di lavoro progettato

Il processo di aggiunta di fogli di lavoro a un foglio di lavoro di progettazione è lo stesso di aggiungere un nuovo foglio di lavoro, tranne per il fatto che il file Excel esiste già e deve essere aperto prima di aggiungere i fogli di lavoro. Un file di progettazione può essere aperto dalla classe Workbook.

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 inputPath = path.join(dataDir, "book1.xlsx");

// Creating a file stream containing the Excel file to be opened
const fstream = fs.readFileSync(inputPath);

// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fstream);

// Adding a new worksheet to the Workbook 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);

// Setting the name of the newly added worksheet
worksheet.setName("My Worksheet");

// Saving the Excel file
workbook.save(path.join(dataDir, "output.xlsx"));

Accesso ai fogli di lavoro utilizzando il nome del foglio

Accedi a qualsiasi foglio di lavoro specificando il suo nome o indice.

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 inputPath = path.join(dataDir, "book1.xlsx");

// Creating a file stream containing the Excel file to be opened
const fstream = fs.readFileSync(inputPath);

// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fstream);

// Accessing a worksheet using its sheet name
const worksheet = workbook.getWorksheets().get("Sheet1");
const cell = worksheet.getCells().get("A1");
console.log(cell.getValue());

Rimozione dei fogli di lavoro utilizzando il nome del foglio

Per rimuovere fogli di lavoro da un file, chiama il metodo WorksheetCollection.removeAt(string) della classe WorksheetCollection. Passa il nome del foglio al metodo WorksheetCollection.removeAt(string) per rimuovere un foglio di lavoro specifico.

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);

// Removing a worksheet using its sheet name
workbook.getWorksheets().removeAt("Sheet1");

// Save workbook
workbook.save(path.join(dataDir, "output.out.xls"));

Rimozione dei fogli di lavoro utilizzando l’indice del foglio

Rimuovere fogli di lavoro per nome funziona bene quando si conosce il nome del foglio di lavoro. Se non si conosce il nome, usare una versione sovraccaricata del metodo WorksheetCollection.removeAt(string) che prende l’indice del foglio di lavoro invece del suo nome.

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);

// Removing a worksheet using its sheet index
workbook.getWorksheets().removeAt(0);

// Save workbook
workbook.save(path.join(dataDir, "output.out.xls"));

Attivare fogli e fare di una cella attiva nel foglio di lavoro

A volte, è necessario attivare e visualizzare un foglio di lavoro specifico quando un utente apre un file Microsoft Excel in Excel. Allo stesso modo, si potrebbe voler attivare una cella specifica e impostare le barre di scorrimento per mostrare la cella attiva. Aspose.Cells è in grado di eseguire tutte queste operazioni.

Un foglio attivo è un foglio su cui stai lavorando: il nome del foglio attivo sulla scheda è in grassetto per impostazione predefinita.

Una cella attiva è una cella selezionata, la cella in cui i dati vengono inseriti quando si inizia a digitare. Solo una cella è attiva alla volta. La cella attiva è evidenziata da un bordo spesso.

Attivare fogli e rendere una cella attiva

Aspose.Cells fornisce chiamate API specifiche per attivare un foglio e una cella. Ad esempio, la proprietà WorksheetCollection.getActiveSheetIndex() è utile per impostare il foglio attivo in una cartella di lavoro. Allo stesso modo, la proprietà Worksheet.getActiveCell() viene utilizzata per impostare e ottenere una cella attiva nel foglio di lavoro.

Per assicurarti che le barre di scorrimento orizzontali o verticali siano posizionate alla riga e alla colonna di indice desiderati per mostrare dati specifici, usa le proprietà Worksheet.getFirstVisibleRow() e Worksheet.getFirstVisibleColumn().

L’esempio seguente mostra come attivare un foglio di lavoro e rendere una cella attiva in esso. Nell’output generato, le barre di scorrimento verranno scorrere per fare in modo che la seconda riga e la seconda colonna siano la loro prima riga e colonna visibile.

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();

// Add a worksheet if collection is empty
const worksheets = workbook.getWorksheets();
if (worksheets.getCount() === 0) {
worksheets.add();
}

// Get the first worksheet in the workbook.
const worksheet1 = worksheets.get(0);

// Get the cells in the worksheet.
const cells = worksheet1.getCells();

// Input data into B2 cell.
cells.get(1, 1).putValue("Hello World!");

// Set the first sheet as an active sheet.
workbook.getWorksheets().setActiveSheetIndex(0);

// Set B2 cell as an active cell in the worksheet.
worksheet1.setActiveCell("B2");

// Set the B column as the first visible column in the worksheet.
worksheet1.setFirstVisibleColumn(1);

// Set the 2nd row as the first visible row in the worksheet.
worksheet1.setFirstVisibleRow(1);

// Save the excel file.
workbook.save(path.join(dataDir, "output.xls"));

Argomenti avanzati