格式单元格

使用样式对话框格式化单元格

单元格可以通过以编程方式格式化,但使用 Aspose.Cells.GridWeb 控件以所见即所得的方式格式化单元格的最简便方法是使用样式对话框。

使用样式对话框: 选择一系列单元格,然后右键单击并选择格式化单元格

选择格式化单元格

todo:image_alt_text

显示样式对话框。

使用样式对话框格式化单元格

todo:image_alt_text

样式对话框让用户通过自定义字体和边框设置来格式化单元格。

自定义字体设置

您可以使用样式对话框自定义以下字体设置:

  • 字体名称,从列表中选择所需的字体。
  • 字体样式,应用粗体、斜体等字体样式。
  • 字体大小,选择磅值的字体大小。
  • 下划线,给文字加下划线。
  • 删除线,给文字应用删除线效果。
  • 水平对齐,选择水平对齐方式。
  • 垂直对齐,选择垂直对齐方式。
  • 字体颜色,选择字体颜色。
  • 背景,选择背景颜色。

您可以在小预览区域检查所选字体设置。

自定义字体设置

todo:image_alt_text

自定义边框设置

该控件还允许用户通过在样式对话框中自定义边框设置来绘制单元格周围的边框。

查看与边框相关的选项: 单击样式对话框中的边框。 边框相关选项将显示。

样式对话框中的边框选项

todo:image_alt_text

可以从样式对话框中选择以下边框选项:

  • 边框线样式,选择实线、虚线等边框样式。
  • 边框线宽度,选择像素单位的边框宽度。
  • 边框线颜色,选择线条颜色。
  • 边框线,选择边框线的编号和位置。

自定义边框设置

todo:image_alt_text

应用设置

单击样式对话框中的确定以应用更改。

应用了字体和边框设置

todo:image_alt_text

使用API格式化单元格

单元格还可以使用 Aspose.Cells.GridWeb API 进行程序化格式化。每个单元格都有一个 Style 属性,该属性表示一个 GridTableItemStyle 对象。使用 Style 属性自定义字体和边框设置。

设置字体

要进行程序化自定义字体设置:

  1. 将 Aspose.Cells.GridWeb 控件添加到 Web 表单中。
  2. 访问工作表。
  3. 访问要进行格式化的单元格。
  4. 访问单元格的样式。
  5. 设置点数(points)中的字体大小。
  6. 设置字体样式。
  7. 设置前景色和背景色。
  8. 设置水平和垂直对齐。
  9. 将样式重新设置到单元格。

输出:在 A1 中显示自定义字体设置。

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the reference of the worksheet that is currently active and resize first row and column
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
sheet.Cells.Clear();
sheet.Cells.SetColumnWidth(0, 50);
sheet.Cells.SetRowHeight(0, 40);
// Accessing a specific cell of the worksheet
GridCell cell = sheet.Cells["A1"];
// Inserting a value in cell A1
cell.PutValue("Aspose.Cells.GridWeb");
var style = cell.Style;
// Setting font, color and alignment of cell
style.Font.Size = new FontUnit("12pt");
style.Font.Bold = true;
style.ForeColor = Color.Blue;
style.BackColor = Color.Aqua;
style.HorizontalAlign = HorizontalAlign.Center;
// Set the cell style
cell.CopyStyle(style);
sheet.AutoFitColumn(0);

设置边框

边框可以应用于单个单元格,或者应用到一个范围内。

单个单元格

要设置单个单元格的边框:

  1. 将 Aspose.Cells.GridWeb 控件添加到 Web 表单中。
  2. 访问工作表。
  3. 访问要进行格式化的单元格。
  4. 访问单元格的样式对象。
  5. 设置边框样式。
  6. 以像素设置边框宽度。
  7. 设置边框颜色。
  8. 将样式设置为单元格样式。

单个单元格的自定义边框设置

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the reference of the worksheet that is currently active and resize first row and column
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
sheet.Cells.Clear();
sheet.Cells.SetColumnWidth(0, 50);
sheet.Cells.SetRowHeight(0, 40);
// Accessing a specific cell of the worksheet
GridCell cell = sheet.Cells["A1"];
var style = cell.Style;
// Setting the border style, width and color
style.BorderStyle = BorderStyle.Solid;
style.BorderWidth = new Unit(2, UnitType.Pixel);
style.BorderColor = Color.Blue;
// Set the cell style
cell.CopyStyle(style);

单元格范围

在一系列单元格上设置边框:

  1. 将 Aspose.Cells.GridWeb 控件添加到 Web 表单。
  2. 访问所需的工作表
  3. 实例化 WebBorderStyle 类的对象
  4. 将边框的样式设置为实线或虚线等
  5. 以像素设置边框的宽度
  6. 设置边框的颜色
  7. 将存储在 WebBorderStyle 对象中的边框设置应用于指定范围的单元格

具有自定义边框设置的单元格范围

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the reference of the worksheet that is currently active
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
sheet.Cells.Clear();
// Creating an instance of WebBorderStyle
WebBorderStyle bstyle = new WebBorderStyle();
// Setting the border style, width and color
bstyle.BorderStyle = BorderStyle.Double;
bstyle.BorderWidth = new Unit(3, UnitType.Pixel);
bstyle.BorderColor = Color.Blue;
// Applying the instance of WebBorderStyle on a specified range of cells
sheet.Cells.SetBorders(1, 1, 5, 4, SetBorderPosition.Cross, bstyle);

设置数字格式

Aspose.Cells.GridWeb 支持设置数字格式。 有 59 种内置数字格式。 要查看它们,请参阅支持的数字格式列表

所有内置的数字格式都位于 NumberType 枚举中。 要使用内置数字格式,请使用单元格对象的 SetNumberType 方法将 NumberType 设置为 NumberType 枚举中的数字格式。

要设置自定义数字格式,请使用单元格的 SetCustom 方法。

应用于 B1 和 B2 的数字格式设置

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the reference of the worksheet that is currently active
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
sheet.Cells.Clear();
sheet.Cells.SetColumnWidth(0, 50);
sheet.Cells.SetRowHeight(0, 40);
// Putting values to cells
sheet.Cells["A1"].PutValue("Currency1 Number Format");
sheet.Cells["A2"].PutValue("Custom Number Format");
sheet.Cells["B1"].PutValue(7800);
sheet.Cells["B2"].PutValue(2500);
// Setting the number format of "B1" cell to Currency1
sheet.Cells["B1"].SetNumberType((int)NumberType.Currency1);
// Setting the custom number format of "B2" cell
sheet.Cells["B2"].SetCustom("#,##0.0000");