填充设置

颜色和背景图案

Microsoft Excel可以设置单元格的前景(轮廓)和背景(填充)颜色以及背景图案。

Aspose.Cells还以灵活的方式支持这些功能。在本主题中,我们将学习如何使用Aspose.Cells来使用这些功能。

设置颜色和背景图案

Aspose.Cells 提供一个类 Workbook 来表示一个 Microsoft Excel 文件。Workbook 类包含一个 Worksheets 集合,可访问 Excel 文件中的每个工作表。工作表由 Worksheet 类表示。Worksheet 类提供一个 Cells 集合。Cells 集合中的每一项代表一个 Cell 类的对象。

Cell 具有用于获取和设置单元格格式的 getStylesetStyle 方法。Style 类提供设置单元格前景色和背景色的属性。Aspose.Cells 提供一个 BackgroundType 枚举,包含一组预定义的背景图案类型,具体如下。

背景图案 描述
DiagonalCrosshatch 代表对角线交叉图案
DiagonalStripe 代表对角线条纹图案
Gray6 代表6.25%灰色图案
Gray12 代表12.5%灰色图案
Gray25 代表25%灰色图案
Gray50 代表50%灰色图案
Gray75 代表75%灰色图案
HorizontalStripe 代表水平条纹图案
None 代表无背景
ReverseDiagonalStripe 代表反对角线条纹图案
Solid 代表实线图案
ThickDiagonalCrosshatch 代表粗对角线交叉图案
ThinDiagonalCrosshatch 代表细对角线交叉图案
ThinDiagonalStripe 代表细对角线条纹图案
ThinHorizontalCrosshatch 代表细水平交叉图案
ThinHorizontalStripe 代表细水平条纹图案
ThinReverseDiagonalStripe 代表细反对角线条纹图案
ThinVerticalStripe 代表细竖条纹图案
VerticalStripe 代表竖条纹图案

在下面的示例中,A1单元格的前景色已设置,但A2配置为具有前景色和背景色的垂直条纹背景模式。

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 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);
// Define a Style and get the A1 cell style
let style = worksheet.getCells().get("A1").getStyle();
// Setting the foreground color to yellow
style.setForegroundColor(AsposeCells.Color.Yellow);
// Setting the background pattern to vertical stripe
style.setPattern(AsposeCells.BackgroundType.VerticalStripe);
// Apply the style to A1 cell
worksheet.getCells().get("A1").setStyle(style);
// Get the A2 cell style
style = worksheet.getCells().get("A2").getStyle();
// Setting the foreground color to blue
style.setForegroundColor(AsposeCells.Color.Blue);
// Setting the background color to yellow
style.setBackgroundColor(AsposeCells.Color.Yellow);
// Setting the background pattern to vertical stripe
style.setPattern(AsposeCells.BackgroundType.VerticalStripe);
// Apply the style to A2 cell
worksheet.getCells().get("A2").setStyle(style);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003);

重要知识

应用梯度填充效果

要将所需的渐变填充效果应用于单元格,请相应使用 Style 对象的 setTwoColorGradient 方法。

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");
// Loads the workbook which contains hidden external links
const workbook = new AsposeCells.Workbook();
// Get the first worksheet (default) in the workbook
const worksheet = workbook.getWorksheets().get(0);
// Input a value into B3 cell
worksheet.getCells().get(2, 1).putValue("test");
// Get the Style of the cell
const style = worksheet.getCells().get("B3").getStyle();
// Set Gradient pattern on
style.setIsGradient(true);
// Specify two color gradient fill effects
style.setTwoColorGradient(new AsposeCells.Color(255, 255, 255), new AsposeCells.Color(79, 129, 189), AsposeCells.GradientStyleType.Horizontal, 1);
// Set the color of the text in the cell
style.getFont().setColor(AsposeCells.Color.Red);
// Specify horizontal and vertical alignment settings
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);
// Apply the style to the cell
worksheet.getCells().get("B3").setStyle(style);
// Set the third row height in pixels
worksheet.getCells().setRowHeightPixel(2, 53);
// Merge the range of cells (B3:C3)
worksheet.getCells().merge(2, 1, 1, 2);
// Save the Excel file
workbook.save(path.join(dataDir, "output.xlsx"));

颜色和调色板

调色板是在创建图像时可用的颜色数量。在演示文稿中使用标准调色板可以让用户创建一致的外观。每个Microsoft Excel(97-2003)文件都有一个包含可应用于单元格、字体、网格线、图形对象、填充和图表中的线条的56种颜色的调色板。

使用 Aspose.Cells 不仅可以使用调色板的现有颜色,还可以使用自定义颜色。在使用自定义颜色之前,首先将其添加到调色板中。

本主题讨论如何向调色板中添加自定义颜色。

向调色板中添加自定义颜色

Aspose.Cells 支持 Microsoft Excel 的 56 种颜色调色板。要使用在调色板中未定义的自定义颜色,需要将颜色添加到调色板中。

Aspose.Cells 提供一个类 Workbook,代表一个 Microsoft Excel 文件。Workbook 类提供一个 changePalette 方法,接受以下参数以添加自定义颜色以修改调色板:

  • Custom Color,要添加的自定义颜色。
  • Index,自定义颜色在调色板中的索引,将替换指定的颜色。应该在 0-55 之间。

下面的示例在应用于字体之前向调色板中添加了自定义颜色(兰花紫)。

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");
// Loads the workbook which contains hidden external links
const workbook = new AsposeCells.Workbook(filePath);
// Checks if a color is in the palette for the spreadsheet.
console.log(workbook.isColorInPalette(AsposeCells.Color.Orchid));
// Adding Orchid color to the palette at 55th index
workbook.changePalette(AsposeCells.Color.Orchid, 55);
console.log(workbook.isColorInPalette(AsposeCells.Color.Orchid));
// 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!");
// Defining new Style object
const styleObject = workbook.createStyle();
// Setting the Orchid (custom) color to the font
styleObject.getFont().setColor(workbook.getColors()[55]);
// Applying the style to the cell
cell.setStyle(styleObject);
// Saving the Excel file
workbook.save("out.xlsx");