格式单元格

介绍

使用GetStyle和SetStyle方法格式化单元格

在单元格上应用不同种类的格式样式,设置背景或前景颜色、边框、字体、水平和垂直对齐、缩进级别、文本方向、旋转角度等。

使用GetStyle和SetStyle方法

如果开发人员需要对不同单元格应用不同的格式样式,则最好使用Style方法获取单元格,使用Cell.get_style方法指定样式属性,然后使用Cell.set_style方法应用格式。下面给出了一个示例,演示了如何在单元格上应用各种格式。

from aspose.cells import BorderType, CellBorderType, TextAlignmentType, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Obtaining the reference of the first worksheet
worksheet = workbook.worksheets[0]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Hello Aspose!")
# Get the style from A1 cell
style = cell.get_style()
# Setting the vertical alignment
style.vertical_alignment = TextAlignmentType.CENTER
# Setting the horizontal alignment
style.horizontal_alignment = TextAlignmentType.CENTER
# Setting the font color of the text
style.font.color = Color.green
# Setting to shrink according to the text contained in it
style.shrink_to_fit = True
# Setting the bottom border color to red
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red
# Setting the bottom border type to medium
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM
# Applying the style to A1 cell
cell.set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

如何使用样式对象为不同单元格设置格式

如果开发人员需要将相同的格式应用于不同的单元格,他们可以使用Style对象。请按照下面的步骤使用Style对象:

  1. 通过调用Workbook类的create_style方法添加Style对象
  2. 访问新添加的Style对象
  3. 设置Style对象的所需属性/属性,以应用所需的格式设置
  4. 将配置的Style对象分配给所需的单元格

这种方法可以极大地提高您的应用程序的效率,并节省内存。

from aspose.cells import BorderType, CellBorderType, TextAlignmentType, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Excel object
i = workbook.worksheets.add()
# Obtaining the reference of the first worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Hello Aspose!")
# Adding a new Style
style = workbook.create_style()
# Setting the vertical alignment of the text in the "A1" cell
style.vertical_alignment = TextAlignmentType.CENTER
# Setting the horizontal alignment of the text in the "A1" cell
style.horizontal_alignment = TextAlignmentType.CENTER
# Setting the font color of the text in the "A1" cell
style.font.color = Color.green
# Shrinking the text to fit in the cell
style.shrink_to_fit = True
# Setting the bottom border color of the cell to red
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red
# Setting the bottom border type of the cell to medium
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM
# Assigning the Style object to the "A1" cell
cell.set_style(style)
# Apply the same style to some other cells
worksheet.cells.get("B1").set_style(style)
worksheet.cells.get("C1").set_style(style)
worksheet.cells.get("D1").set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

如何使用Microsoft Excel 2007预定义样式

如果你需要对Microsoft Excel 2007应用不同的格式,可以使用Aspose.Cells for Python via .NET API应用样式。以下示例演示了如何在单元格上应用预定义样式。

from aspose.cells import Workbook
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiate a new Workbook.
workbook = Workbook()
# Create a style object .
style = workbook.create_style()
# Input a value to A1 cell.
workbook.worksheets[0].cells.get("A1").put_value("Test")
# Apply the style to the cell.
workbook.worksheets[0].cells.get("A1").set_style(style)
# Save the Excel 2007 file.
workbook.save(dataDir + "book1.out.xlsx")

如何格式化单元格中的选定字符

处理字体设置解释了如何格式化单元格中的文本,但它只解释了如何格式化所有单元格内容。如果您只想格式化选定的字符怎么办?

Aspose.Cells for Python via .NET也支持此功能。本主题讲解了如何有效使用此功能。

如何格式化选定的字符

Aspose.Cells for Python via .NET提供了一个 Workbook 类,代表一个Microsoft Excel文件。Workbook 类包含一个 worksheets 集合,允许访问Excel文件中的每个工作表。一个工作表由 Worksheet 类表示。Worksheet 类提供一个 cells 集合。cells 集合中的每个项代表一个 Cell 类的对象。

Cell类提供characters方法,该方法接受以下参数以选择单元格中的字符范围:

  • 起始索引,选择开始的字符的索引。
  • 字符数,要选择的字符数。

characters方法返回FontSetting类的实例,允许开发人员以与单元格相同的方式格式化字符,如下面的代码示例所示。在输出文件中,A1单元格中的’Visit’单词将使用默认字体格式,但’Aspose!‘将以粗体和蓝色格式显示。

from aspose.cells import Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Obtaining the reference of the first(default) worksheet by passing its sheet index
worksheet = workbook.worksheets[0]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Visit Aspose!")
# Setting the font of selected characters to bold
cell.characters(6, 7).font.is_bold = True
# Setting the font color of selected characters to blue
cell.characters(6, 7).font.color = Color.blue
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

如何格式化行和列

有时,开发人员需要在行或列上应用相同的格式。逐个单元格应用格式通常需要更长时间,不是一个好的解决方案。 为了解决此问题,Aspose.Cells for Python via .NET提供了一个简单、快速的方法,详细内容请参阅本文。

格式化行和列

Aspose.Cells for Python via .NET提供了一个 Workbook 类,代表一个Microsoft Excel文件。Workbook 类包含一个 worksheets 集合,可以访问Excel文件中的每个工作表。一个工作表由 Worksheet 类表示。Worksheet 类提供一个 cells 集合。cells 集合提供一个 rows 集合。

如何格式化一行

集合中的每个项目代表一个Row对象。Row对象提供了apply_style方法,用于设置行的格式。要将相同的格式应用于一行,请使用Style对象。以下步骤显示如何使用它。

  1. 通过调用其create_style方法向Workbook类添加一个Style对象。
  2. 设置Style对象的属性以应用格式设置。
  3. 使StyleFlag对象的相关属性为ON。
  4. 将配置的Style对象分配给Row对象。
from aspose.cells import BorderType, CellBorderType, StyleFlag, TextAlignmentType, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Obtaining the reference of the first (default) worksheet by passing its sheet index
worksheet = workbook.worksheets[0]
# Adding a new Style to the styles
style = workbook.create_style()
# Setting the vertical alignment of the text in the "A1" cell
style.vertical_alignment = TextAlignmentType.CENTER
# Setting the horizontal alignment of the text in the "A1" cell
style.horizontal_alignment = TextAlignmentType.CENTER
# Setting the font color of the text in the "A1" cell
style.font.color = Color.green
# Shrinking the text to fit in the cell
style.shrink_to_fit = True
# Setting the bottom border color of the cell to red
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red
# Setting the bottom border type of the cell to medium
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM
# Creating StyleFlag
styleFlag = StyleFlag()
styleFlag.horizontal_alignment = True
styleFlag.vertical_alignment = True
styleFlag.shrink_to_fit = True
styleFlag.borders = True
styleFlag.font_color = True
# Accessing a row from the Rows collection
row = worksheet.cells.rows[0]
# Assigning the Style object to the Style property of the row
row.apply_style(style, styleFlag)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

如何格式化一列

cells集合还提供一个columns集合。集合中的每个项目代表一个Column对象。类似于Row对象,Column对象还提供了apply_style方法,用于格式化列。

from aspose.cells import BorderType, CellBorderType, StyleFlag, TextAlignmentType, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Obtaining the reference of the first (default) worksheet by passing its sheet index
worksheet = workbook.worksheets[0]
# Adding a new Style to the styles
style = workbook.create_style()
# Setting the vertical alignment of the text in the "A1" cell
style.vertical_alignment = TextAlignmentType.CENTER
# Setting the horizontal alignment of the text in the "A1" cell
style.horizontal_alignment = TextAlignmentType.CENTER
# Setting the font color of the text in the "A1" cell
style.font.color = Color.green
# Shrinking the text to fit in the cell
style.shrink_to_fit = True
# Setting the bottom border color of the cell to red
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red
# Setting the bottom border type of the cell to medium
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM
# Creating StyleFlag
styleFlag = StyleFlag()
styleFlag.horizontal_alignment = True
styleFlag.vertical_alignment = True
styleFlag.shrink_to_fit = True
styleFlag.borders = True
styleFlag.font_color = True
# Accessing a column from the Columns collection
column = worksheet.cells.columns[0]
# Applying the style to the column
column.apply_style(style, styleFlag)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

高级主题