Configuración de fuente con Node.js a través de C++

Configuración de fuente

Aspose.Cells proporciona una clase, Workbook que representa un archivo de Microsoft Excel. La clase Workbook contiene una colección Worksheets que permite acceder a cada hoja de cálculo en un archivo de Excel. Una hoja de cálculo está representada por la clase Worksheet. La clase Worksheet proporciona una colección Cells. Cada elemento en la colección Cells representa un objeto de la clase Cell.

Aspose.Cells proporciona los métodos getStyle y setStyle de la clase Cell que se usan para obtener y establecer el estilo de formato de una celda. La clase Style proporciona propiedades para configurar la fuente.

Establecer nombre de fuente

Los desarrolladores pueden aplicar cualquier fuente al texto dentro de una celda usando el método Font setName del objeto.

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

Establecer estilo de fuente en negrita

Los desarrolladores pueden hacer que el texto esté en negrita configurando el método setIsBold del objeto Font en 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");

Establecer tamaño de fuente

Establece el tamaño de fuente con el método setSize del objeto 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");

Establecer color de fuente

Utilice el método setColor del objeto Font para establecer el color de la fuente. Seleccione cualquier color del enumerador Color (parte de Node.js) y asígnele al método 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");

Establecer tipo de subrayado de fuente

Utilice el método setUnderline del objeto Font para subrayar el texto. Aspose.Cells ofrece varios tipos predefinidos de subrayado de fuente en la enumeración FontUnderlineType.

Tipos de subrayado de fuente Descripción
Accounting Un solo subrayado contable
Double Doble subrayado
DoubleAccounting Doble subrayado
None Sin subrayado
Single Un solo subrayado
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);

Establecer efecto tachado

Aplicar tachado configurando el método setIsStrikeout del objeto Font a 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"));

Establecer efecto subíndice

Aplicar subíndice configurando el método setIsSubscript del objeto Font a 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"));

Establecer efecto superíndice en fuente

Los desarrolladores pueden aplicar el efecto superíndice en la fuente configurando el método setIsSuperscript del objeto Font a 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"));

Temas avanzados