Impostazioni del font con Node.js tramite C++
Configurazione delle Impostazioni del Carattere
Aspose.Cells fornisce una classe, Workbook, che rappresenta un file Microsoft Excel. La classe Workbook contiene una raccolta Worksheets che consente l’accesso a ogni foglio di lavoro in un file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una raccolta Cells. Ogni elemento nella raccolta Cells rappresenta un oggetto della classe Cell.
Aspose.Cells fornisce i metodi Cell.getStyle e Cell.setStyle che vengono usati per ottenere e impostare lo stile di formattazione di una cella. La classe Style fornisce proprietà per la configurazione delle impostazioni di font.
Impostazione del nome del carattere
Gli sviluppatori possono applicare qualsiasi font al testo all’interno di una cella usando il metodo setName dell’oggetto Font.
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. | |
const fs = require("fs"); | |
if (!fs.existsSync(dataDir)) { | |
fs.mkdirSync(dataDir); | |
} | |
// 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 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("Hello Aspose!"); | |
// Obtaining the style of the cell | |
const style = cell.getStyle(); | |
// Setting the font name to "Times New Roman" | |
style.getFont().setName("Times New Roman"); | |
// Applying the style to the cell | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003); |
Impostare lo stile del carattere su Grassetto
Gli sviluppatori possono rendere il testo in grassetto impostando il metodo setIsBold dell’oggetto Font su true.
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"); | |
// 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 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("Hello Aspose!"); | |
// Obtaining the style of the cell | |
const style = cell.getStyle(); | |
// Setting the font weight to bold | |
style.getFont().setIsBold(true); | |
// Applying the style to the cell | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("out.xlsx"); |
Impostazione della dimensione del carattere
Imposta la dimensione del font con il metodo setSize dell’oggetto Font.
const AsposeCells = require("aspose.cells.node"); | |
const path = require("path"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
const filePath = path.join(dataDir, "sample.xlsx"); | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Adding a new worksheet to the Excel 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); | |
// Accessing the "A1" cell from the worksheet | |
const cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Hello Aspose!"); | |
// Obtaining the style of the cell | |
const style = cell.getStyle(); | |
// Setting the font size to 14 | |
style.getFont().setSize(14); | |
// Applying the style to the cell | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("out.xlsx"); |
Impostazione del colore del carattere
Usa il metodo setColor dell’oggetto Font per impostare il colore del font. Seleziona un colore dall’enumerazione Color (parte di Node.js) e assegnalo al metodo setColor.
const AsposeCells = require("aspose.cells.node"); | |
const path = require("path"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
const filePath = path.join(dataDir, "sample.xlsx"); | |
// 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 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("Hello Aspose!"); | |
// Obtaining the style of the cell | |
const style = cell.getStyle(); | |
// Setting the font color to blue | |
style.getFont().setColor(AsposeCells.Color.Blue); | |
// Applying the style to the cell | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("out.xlsx"); |
Impostazione del tipo sottolineato del carattere
Usa il metodo setUnderline dell’oggetto Font per sottolineare il testo. Aspose.Cells offre vari tipi di sottolineatura predefiniti nella enumerazione FontUnderlineType.
Tipi di sottolineatura del carattere | Descrizione |
---|---|
Accounting | Un solo sottolineatura contabile |
Double | Doppia sottolineatura |
DoubleAccounting | Doppia sottolineatura contabile |
None | Nessuna sottolineatura |
Single | Una singola sottolineatura |
const AsposeCells = require("aspose.cells.node"); | |
const path = require("path"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
const outputPath = path.join(dataDir, "out.xlsx"); | |
// 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(); | |
// 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("Hello Aspose!"); | |
// Obtaining the style of the cell | |
const style = cell.getStyle(); | |
// Setting the font to be underlined | |
style.getFont().setUnderline(AsposeCells.FontUnderlineType.Single); | |
// Applying the style to the cell | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(outputPath); |
Impostazione dell’effetto barrato
Applicare il sbarrato impostando il metodo setIsStrikeout dell’oggetto Font su true.
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 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("Hello Aspose!"); | |
// Obtaining the style of the cell | |
const style = cell.getStyle(); | |
// Setting the strike out effect on the font | |
style.getFont().setIsStrikeout(true); | |
// Applying the style to the cell | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "out.xlsx")); |
Impostazione dell’effetto pedice
Applicare il pedice impostando il metodo setIsSubscript dell’oggetto Font su true.
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 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("Hello Aspose!"); | |
// Obtaining the style of the cell | |
const style = cell.getStyle(); | |
// Setting the strike out effect on the font | |
style.getFont().setIsStrikeout(true); | |
// Applying the style to the cell | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "out.xlsx")); |
Impostazione dell’effetto esponente sulla font
Gli sviluppatori possono applicare l’effetto apice impostando il metodo setIsSuperscript dell’oggetto Font su true.
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 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("Hello Aspose!"); | |
// Obtaining the style of the cell | |
const style = cell.getStyle(); | |
// Setting superscript effect | |
style.getFont().setIsSuperscript(true); | |
// Applying the style to the cell | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "out.xlsx")); |