控件管理
介绍
开发者可以添加不同的绘图对象,如文本框、复选框、单选按钮、组合框、标签、按钮、线条、矩形、弧、椭圆、旋转器、滚动条、分组框等。Aspose.Cells for Python via .NET提供了Aspose.Cells.Drawing命名空间,其中包含所有绘图对象。然而,仍有一些绘图对象或形状尚未支持。在设计器电子表格中使用Microsoft Excel创建这些绘图对象,然后导入到Aspose.Cells。Aspose.Cells for Python via .NET允许您从设计器电子表格加载这些绘图对象并写入生成的文件。
将文本框控件添加到工作表
强调报告中重要信息的一种方式是使用文本框。例如,添加文本以突出公司名称或指示销售额最高的地区等。Aspose.Cells for Python via .NET提供了 TextBoxCollection 类,用于向集合中添加新文本框。还有另一类 TextBox,表示用于定义所有类型设置的文本框。它具有一些重要成员:
- text_frame 属性返回一个 MsoTextFrame 对象,用于调整文本框的内容。
- placement 属性指定放置类型。
- font 属性指定字体属性。
- add_hyperlink 方法为文本框添加超链接。
- fill_format 属性返回一个 MsoFillFormat 对象,用于设置文本框的填充格式。
- line_format 属性返回用于设置文本框线条样式和粗细的 MsoLineFormat 对象。
- text 属性指定文本框的输入文本。
以下示例在工作簿的第一个工作表中创建了两个文本框。第一个文本框配有不同的格式设置。第二个是一个简单的文本框。
from aspose.cells import Workbook | |
from aspose.cells.drawing import MsoLineDashStyle, PlacementType | |
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) | |
# Instantiate a new Workbook. | |
workbook = Workbook() | |
# Get the first worksheet in the book. | |
worksheet = workbook.worksheets[0] | |
# Add a new textbox to the collection. | |
textboxIndex = worksheet.text_boxes.add(2, 1, 160, 200) | |
# Get the textbox object. | |
textbox0 = worksheet.text_boxes[textboxIndex] | |
# Fill the text. | |
textbox0.text = "ASPOSE______The .NET & JAVA Component Publisher!" | |
# Get the textbox text frame. | |
# MsoTextFrame textframe0 = textbox0.TextFrame; | |
# Set the textbox to adjust it according to its contents. | |
# textframe0.AutoSize = true; | |
# Set the placement. | |
textbox0.placement = PlacementType.FREE_FLOATING | |
# Set the font color. | |
textbox0.font.color = Color.blue | |
# Set the font to bold. | |
textbox0.font.is_bold = True | |
# Set the font size. | |
textbox0.font.size = 14 | |
# Set font attribute to italic. | |
textbox0.font.is_italic = True | |
# Add a hyperlink to the textbox. | |
textbox0.add_hyperlink("http:// Www.aspose.com/") | |
# Get the filformat of the textbox. | |
fillformat = textbox0.fill | |
# Get the lineformat type of the textbox. | |
lineformat = textbox0.line | |
# Set the line weight. | |
lineformat.weight = 6.0 | |
# Set the dash style to squaredot. | |
lineformat.dash_style = MsoLineDashStyle.SQUARE_DOT | |
# Add another textbox. | |
textboxIndex = worksheet.text_boxes.add(15, 4, 85, 120) | |
# Get the second textbox. | |
textbox1 = worksheet.text_boxes[textboxIndex] | |
# Input some text to it. | |
textbox1.text = "This is another simple text box" | |
# Set the placement type as the textbox will move and | |
# Resize with cells. | |
textbox1.placement = PlacementType.MOVE_AND_SIZE | |
# Save the excel file. | |
workbook.save(dataDir + "book1.out.xls") |
在设计器电子表格中操作文本框控件
Aspose.Cells for Python via .NET还允许您访问设计师工作表中的文本框并进行操作。使用 Worksheet.TextBoxes 属性可获取工作表中的文本框集合。
以下示例使用上面示例中创建的 Microsoft Excel 文件。它获取了两个文本框的文本字符串,并将第二个文本框的文本更改后保存了文件。
from aspose.cells import Workbook | |
# 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(".") | |
# Instantiate a new Workbook. | |
# Open the existing excel file. | |
workbook = Workbook(dataDir + "book1.xls") | |
# Get the first worksheet in the book. | |
worksheet = workbook.worksheets[0] | |
# Get the first textbox object. | |
textbox0 = worksheet.text_boxes[0] | |
# Obtain the text in the first textbox. | |
text0 = textbox0.text | |
# Get the second textbox object. | |
textbox1 = worksheet.text_boxes[1] | |
# Obtain the text in the second textbox. | |
text1 = textbox1.text | |
# Change the text of the second textbox. | |
textbox1.text = "This is an alternative text" | |
# Save the excel file. | |
workbook.save(dataDir + "output.out.xls") |
将复选框控件添加到工作表
复选框在您希望为用户提供两种选择(如真或假;是或否)时非常方便;Aspose.Cells for Python via .NET 允许您在工作表中使用复选框。例如,您可能开发了一个财务预测工作表,在其中可以考虑某项收购或不考虑。在这种情况下,您可能希望在工作表顶部放置一个复选框。然后,您可以将该复选框的状态链接到另一个单元格,因此如果选中复选框,则单元格的值为 True;如果未选中,则值为 False。
使用Microsoft Excel
要在工作表中放置复选框控件,请按照以下步骤进行:
- 确保显示“表单”工具栏。
- 单击“表单”工具栏上的复选框工具。
- 在工作表区域,单击并拖动以定义容纳复选框和复选框旁边标签的矩形。
- 放置复选框后,将鼠标光标移至标签区域并更改标签。
- 在单元格链接字段中,指定应链接到该复选框的单元格地址。
- 单击确定。
使用Aspose.Cells for Python via .NET
Aspose.Cells for Python via .NET 提供 CheckBoxCollection 类,用于向集合中添加新复选框。还有另一个类 Aspose.Cells.Drawing.CheckBox,代表复选框。它具有一些重要成员:
- linked_cell属性指定与复选框链接的单元格。
- text属性指定与复选框关联的文本字符串。它是复选框的标签。
- value属性指定复选框是否已选中。
以下示例显示如何向工作表添加复选框。
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. | |
excelbook = Workbook() | |
# Add a checkbox to the first worksheet in the workbook. | |
index = excelbook.worksheets[0].check_boxes.add(5, 5, 100, 120) | |
# Get the checkbox object. | |
checkbox = excelbook.worksheets[0].check_boxes[index] | |
# Set its text string. | |
checkbox.text = "Click it!" | |
# Put a value into B1 cell. | |
excelbook.worksheets[0].cells.get("B1").put_value("LnkCell") | |
# Set B1 cell as a linked cell for the checkbox. | |
checkbox.linked_cell = "B1" | |
# Check the checkbox by default. | |
checkbox.value = True | |
# Save the excel file. | |
excelbook.save(dataDir + "book1.out.xls") |
向工作表添加单选按钮控件
单选按钮,或选项按钮,是一个由圆形框制成的控件。用户通过选择圆形框来做出选择。单选按钮通常,如果不是总是,会以其他形式出现和表现。这样的单选按钮会以组的形式出现和表现。用户可以通过只选择其中一个来决定哪一个按钮是有效的。当用户点击其中一个按钮时,它会被填充。当组中的一个按钮被选中时,同一组的按钮是空的。
使用Microsoft Excel
要在工作表中放置单选按钮控件,请按照以下步骤执行:
- 确保表单工具栏已显示。
- 单击 选项按钮 工具。
- 在工作表中,单击并拖动以定义将容纳选项按钮和选项按钮旁边标签的矩形。
- 一旦单选按钮放置在工作表中,将鼠标光标移入标签区域并更改标签。
- 在 单元格链接 字段中,指定应与此单选按钮链接的单元格的地址。
- 点击确定。
使用Aspose.Cells for Python via .NET
Aspose.Cells.Drawing.ShapeCollection 类提供了一个名为 add_radio_button 的方法,用于向工作表添加单选按钮控件。该方法返回一个 Aspose.Cells.Drawing.RadioButton 对象。类 Aspose.Cells.Drawing.RadioButton 表示一个选项按钮。它有一些重要成员:
- linked_cell 属性指定与单选按钮关联的单元格。
- text 属性指定与单选按钮相关联的文本字符串。它是单选按钮的标签。
- is_checked 属性指定单选按钮是否选中。
- fill_format 属性指定单选按钮的填充格式。
- line_format 属性指定选项按钮的线条格式样式。
以下示例显示了如何向工作表添加单选按钮。该示例添加了代表年龄组的三个单选按钮。
向工作表添加组合框控件
为了更容易进行数据输入,或者将输入限制为您定义的某些项,您可以创建一个组合框或下拉列表,这些有效条目是从工作表上的其他单元格编制而成的。当您为单元格创建下拉列表时,它会在该单元格旁边显示一个箭头。要在该单元格中输入信息,单击箭头,然后单击所需的条目。
使用Microsoft Excel
要在工作表中放置组合框控件,请按照以下步骤执行:
- 确保表单工具栏已显示。
- 单击 组合框 工具。
- 在您的工作表区域,单击并拖动以定义将容纳组合框的矩形。
- 一旦组合框放置在工作表中,请右键单击控件,选择设置控件格式并指定输入范围。
- 在单元格链接字段中,指定应链接到该组合框的单元格地址。
- 单击确定。
使用Aspose.Cells for Python via .NET
Aspose.Cells.Drawing.ShapeCollection 类提供了一个名为 add_combo_box 的方法,用于向工作表添加组合框控件。该方法返回一个 Aspose.Cells.Drawing.ComboBox 对象。类 Aspose.Cells.Drawing.ComboBox 表示一个组合框。它有一些重要成员:
- linked_cell 属性指定与组合框关联的单元格。
- input_range 属性指定用于填充组合框的工作表单元格范围。
- drop_down_lines 属性指定下拉部分中显示的列表行数。
- shadow属性指示下拉框是否具有3D阴影。
以下示例显示如何向工作表添加下拉框。
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) | |
# Create a new Workbook. | |
workbook = Workbook() | |
# Get the first worksheet. | |
sheet = workbook.worksheets[0] | |
# Get the worksheet cells collection. | |
cells = sheet.cells | |
# Input a value. | |
cells.get("B3").put_value("Employee:") | |
# Set it bold. | |
cells.get("B3").get_style().font.is_bold = True | |
# Input some values that denote the input range | |
# For the combo box. | |
cells.get("A2").put_value("Emp001") | |
cells.get("A3").put_value("Emp002") | |
cells.get("A4").put_value("Emp003") | |
cells.get("A5").put_value("Emp004") | |
cells.get("A6").put_value("Emp005") | |
cells.get("A7").put_value("Emp006") | |
# Add a new combo box. | |
comboBox = sheet.shapes.add_combo_box(2, 0, 2, 0, 22, 100) |
将标签控件添加到工作表
标签是向用户提供有关电子表格内容信息的手段。Aspose.Cells for Python via .NET 使在工作表中添加和操作标签成为可能。ShapeCollection 类提供一个名为 add_label 的方法,用于在工作表中添加标签控件。该方法返回一个 Label 对象。类 Label 表示工作表中的标签。它具有一些重要成员:
- text方法指定标签的标题字符串。
- placement方法指定PlacementType,即标签附加到工作表中的单元格的方式。
以下示例显示如何向工作表添加标签。
from aspose.cells import Workbook | |
from aspose.cells.drawing import PlacementType | |
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) | |
# Create a new Workbook. | |
workbook = Workbook() | |
# Get the first worksheet in the workbook. | |
sheet = workbook.worksheets[0] | |
# Add a new label to the worksheet. | |
label = sheet.shapes.add_label(2, 0, 2, 0, 60, 120) | |
# Set the caption of the label. | |
label.text = "This is a Label" | |
# Set the Placement Type, the way the | |
# Label is attached to the cells. | |
label.placement = PlacementType.FREE_FLOATING | |
# Saves the file. | |
workbook.save(dataDir + "book1.out.xls") |
将列表框控件添加到工作表
列表框控件创建一个列表控件,允许选择单个或多个项目。
使用Microsoft Excel
要在工作表中放置列表框控件:
- 确保表单工具栏已显示。
- 点击列表框工具。
- 在工作表区域,单击并拖动以定义将容纳列表框的矩形。
- 将列表框放置在工作表中后,右键单击控件,然后点击格式控件,并指定输入范围。
- 在单元格链接字段中,指定应将该列表框链接到的单元格地址,并设置选择类型(单选,多选,扩展)属性
- 点击确定。
使用Aspose.Cells for Python via .NET
ShapeCollection类提供了一个名为add_list_box的方法,用于向工作表添加列表框控件。该方法返回一个Aspose.Cells.Drawing.ListBox对象。ListBox类表示列表框。它有一些重要成员:
- linked_cell方法指定与列表框链接的单元格。
- input_range方法指定用于填充列表框的工作表单元格范围。
- selection_type方法指定列表框的选择模式。
- shadow方法指示列表框是否具有3D阴影。
以下示例显示了如何向工作表中添加列表框。
from aspose.cells import Workbook | |
from aspose.cells.drawing import PlacementType, SelectionType | |
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) | |
# Create a new Workbook. | |
workbook = Workbook() | |
# Get the first worksheet. | |
sheet = workbook.worksheets[0] | |
# Get the worksheet cells collection. | |
cells = sheet.cells | |
# Input a value. | |
cells.get("B3").put_value("Choose Dept:") | |
# Set it bold. | |
cells.get("B3").get_style().font.is_bold = True | |
# Input some values that denote the input range | |
# For the list box. | |
cells.get("A2").put_value("Sales") | |
cells.get("A3").put_value("Finance") | |
cells.get("A4").put_value("MIS") | |
cells.get("A5").put_value("R&D") | |
cells.get("A6").put_value("Marketing") | |
cells.get("A7").put_value("HRA") | |
# Add a new list box. | |
listBox = sheet.shapes.add_list_box(2, 0, 3, 0, 122, 100) | |
# Set the placement type. | |
listBox.placement = PlacementType.FREE_FLOATING | |
# Set the linked cell. | |
listBox.linked_cell = "A1" | |
# Set the input range. | |
listBox.input_range = "A2:A7" | |
# Set the selection tyle. | |
listBox.selection_type = SelectionType.SINGLE | |
# Set the list box with 3-D shading. | |
listBox.shadow = True | |
# Saves the file. | |
workbook.save(dataDir + "book1.out.xls") |
向工作表添加按钮控件
按钮对于执行某些操作非常有用。有时,将VBA宏分配给按钮或分配超链接以打开网页非常有用。
使用Microsoft Excel
要在工作表中放置按钮控件:
- 确保表单工具栏已显示。
- 单击按钮工具。
- 在工作表区域,单击并拖动以定义将容纳按钮的矩形。
- 将按钮放置在工作表中后,右键单击控件并选择格式控件,然后指定VBA宏和相关字体、对齐、大小、边距等属性。
- 单击确定。
使用Aspose.Cells for Python via .NET
ShapeCollection类提供了一个名为add_button的方法,用于向工作表中添加按钮控件。该方法返回一个Aspose.Cells.Drawing.Button对象。类Aspose.Cells.Drawing.Button表示按钮,具有一些重要成员:
- text属性指定按钮的标题。
- font属性指定按钮控件标签的字体属性。
- placement属性指定PlacementType,按钮与工作表中的单元格的连接方式。
- add_hyperlink属性为按钮控件添加超链接。单击按钮将导航到相关的URL。
以下示例显示了如何向工作表中添加按钮。
向工作表添加线控件
使用Microsoft Excel
- 在 绘图 工具栏上,单击 自选图形,指向 线条,然后选择所需的线条样式。
- 拖动以绘制线条。
- 执行以下操作中的一个或两个:
- 要限制线条以与起点呈15度角的方式绘制,请在拖动时按住 SHIFT 键。
- 要使线条从第一个端点向相反方向延伸,请在拖动时按住 CTRL 键。
使用Aspose.Cells for Python via .NET
ShapeCollection 类提供了一个名为 add_line 的方法,用于向工作表添加线形状。该方法返回一个 LineShape 对象。类 LineShape 表示一条线。它有一些重要成员:
- line_format 方法指定线的格式。
- placement 方法指定 PlacementType,线如何连接到工作表中的单元格。
以下示例展示了如何向工作表添加线条。它创建了三条具有不同样式的线。
from aspose.cells import Workbook | |
from aspose.cells.drawing import MsoLineDashStyle, PlacementType | |
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() | |
# Get the first worksheet in the book. | |
worksheet = workbook.worksheets[0] | |
# Add a new line to the worksheet. | |
line1 = worksheet.shapes.add_line(5, 0, 1, 0, 0, 250) | |
# Set the line dash style | |
line1.line.dash_style = MsoLineDashStyle.SOLID | |
# Set the placement. | |
line1.placement = PlacementType.FREE_FLOATING | |
# Add another line to the worksheet. | |
line2 = worksheet.shapes.add_line(7, 0, 1, 0, 85, 250) | |
# Set the line dash style. | |
line2.line.dash_style = MsoLineDashStyle.DASH_LONG_DASH | |
# Set the weight of the line. | |
line2.line.weight = 4.0 | |
# Set the placement. | |
line2.placement = PlacementType.FREE_FLOATING | |
# Add the third line to the worksheet. | |
line3 = worksheet.shapes.add_line(13, 0, 1, 0, 0, 250) | |
# Set the line dash style | |
line3.line.dash_style = MsoLineDashStyle.SOLID | |
# Set the placement. | |
line3.placement = PlacementType.FREE_FLOATING | |
# Make the gridlines invisible in the first worksheet. | |
workbook.worksheets[0].is_gridlines_visible = False | |
# Save the excel file. | |
workbook.save(dataDir + "book1.out.xls") |
向线条添加箭头
Aspose.Cells for Python via .NET 还允许您绘制箭头线。可以在线条上添加箭头,并对线条进行格式化。例如,您可以更改线条颜色,或指定线宽和线型。
以下示例展示了如何向线添加箭头。
from aspose.cells import Workbook | |
from aspose.cells.drawing import FillType, MsoArrowheadLength, MsoArrowheadStyle, MsoArrowheadWidth, PlacementType | |
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) | |
# Instantiate a new Workbook. | |
workbook = Workbook() | |
# Get the first worksheet in the book. | |
worksheet = workbook.worksheets[0] | |
# Add a line to the worksheet | |
line2 = worksheet.shapes.add_line(7, 0, 1, 0, 85, 250) | |
# Set the line color | |
line2.line.fill_type = FillType.SOLID | |
line2.line.solid_fill.color = Color.blue | |
# Set the weight of the line. | |
line2.line.weight = 3.0 | |
# Set the placement. | |
line2.placement = PlacementType.FREE_FLOATING | |
# Set the line arrows. | |
line2.line.end_arrowhead_width = MsoArrowheadWidth.MEDIUM | |
line2.line.end_arrowhead_style = MsoArrowheadStyle.ARROW | |
line2.line.end_arrowhead_length = MsoArrowheadLength.MEDIUM | |
line2.line.begin_arrowhead_style = MsoArrowheadStyle.ARROW_DIAMOND | |
line2.line.begin_arrowhead_length = MsoArrowheadLength.MEDIUM | |
# Make the gridlines invisible in the first worksheet. | |
workbook.worksheets[0].is_gridlines_visible = False | |
# Save the excel file. | |
workbook.save(dataDir + "book1.out.xlsx") |
在工作表中添加矩形控件
Aspose.Cells for Python via .NET 允许您在工作表中绘制矩形形状。您可以创建矩形、正方形等,还可以设置填充色和边框线颜色。例如,可以更改矩形的颜色,设置阴影颜色,或根据需要指定矩形的线宽和样式。
使用Microsoft Excel
- 在绘图工具栏上,单击矩形。
- 拖动绘制矩形。
- 执行以下操作中的一个或两个:
- 若要从起点绘制正方形并约束矩形,请按住SHIFT键并拖动。
- 若要从中心点绘制矩形,请按住CTRL键并拖动。
使用Aspose.Cells for Python via .NET
ShapeCollection 类提供了一个名为 add_rectangle 的方法,用于向工作表添加矩形形状。该方法返回一个 Aspose.Cells.Drawing.RectangleShape 对象。类 Aspose.Cells.Drawing.RectangleShape 表示一个矩形。它有一些重要成员:
- line_format 属性指定矩形的线条格式属性。
- placement 属性指定 PlacementType,矩形如何连接到工作表中的单元格。
- fill_format 属性指定矩形的填充格式样式。
以下示例展示了如何向工作表添加矩形。
from aspose.cells import Workbook | |
from aspose.cells.drawing import MsoLineDashStyle, PlacementType | |
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. | |
excelbook = Workbook() | |
# Add a rectangle control. | |
rectangle = excelbook.worksheets[0].shapes.add_rectangle(3, 0, 2, 0, 70, 130) | |
# Set the placement of the rectangle. | |
rectangle.placement = PlacementType.FREE_FLOATING | |
# Set the line weight. | |
rectangle.line.weight = 4.0 | |
# Set the dash style of the rectangle. | |
rectangle.line.dash_style = MsoLineDashStyle.SOLID | |
# Save the excel file. | |
excelbook.save(dataDir + "book1.out.xls") |
向工作表添加弧形控件
Aspose.Cells for Python via .NET 允许您在工作表中绘制弧形。可以创建简单的弧和填充弧。还可以设置填充色和边框线颜色。例如,您可以指定/更改弧的颜色,设置阴影颜色,或根据需要指定形状的线宽和样式。
使用Microsoft Excel
- 在绘图工具栏上,单击AutoShapes中的弧形。
- 拖动绘制弧形。
使用Aspose.Cells for Python via .NET
ShapeCollection 类提供了一个名为 add_arc 的方法,用于向工作表添加弧形状。该方法返回一个 Aspose.Cells.Drawing.ArcShape 对象。类 Aspose.Cells.Drawing.ArcShape 表示一条弧。它有一些重要成员:
- line_format 属性指定弧形状的线条格式属性。
- placement 属性指定 PlacementType,弧形状如何连接到工作表中的单元格。
- fill_format 属性指定了形状的填充格式样式。
- lower_right_row 属性指定右下角行索引。
- lower_right_column 属性指定右下角列索引。
以下示例显示了如何将弧形形状添加到工作表。该示例创建了两个弧形形状:一个填充,另一个是简单的。
from aspose.cells import Workbook | |
from aspose.cells.drawing import FillType, MsoLineDashStyle, PlacementType | |
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) | |
# Instantiate a new Workbook. | |
excelbook = Workbook() | |
# Add an arc shape. | |
arc1 = excelbook.worksheets[0].shapes.add_arc(2, 0, 2, 0, 130, 130) | |
# Set the fill shape color | |
arc1.fill.fill_type = FillType.SOLID | |
arc1.fill.solid_fill.color = Color.blue | |
# Set the placement of the arc. | |
arc1.placement = PlacementType.FREE_FLOATING | |
# Set the line weight. | |
arc1.line.weight = 1.0 | |
# Set the dash style of the arc. | |
arc1.line.dash_style = MsoLineDashStyle.SOLID | |
# Add another arc shape. | |
arc2 = excelbook.worksheets[0].shapes.add_arc(9, 0, 2, 0, 130, 130) | |
# Set the line color | |
arc2.line.fill_type = FillType.SOLID | |
arc2.line.solid_fill.color = Color.blue | |
# Set the placement of the arc. | |
arc2.placement = PlacementType.FREE_FLOATING | |
# Set the line weight. | |
arc2.line.weight = 1.0 | |
# Set the dash style of the arc. | |
arc2.line.dash_style = MsoLineDashStyle.SOLID | |
# Save the excel file. | |
excelbook.save(dataDir + "book1.out.xls") |
向工作表添加椭圆控件
Aspose.Cells for Python via .NET 允许您在工作表中绘制椭圆形。创建简单的填充椭圆形,并格式化填充色和边框线颜色。例如,可以指定/更改椭圆的颜色,设置阴影颜色,或根据需要指定形状的线宽和样式。
使用Microsoft Excel
- 在绘图工具栏上,单击椭圆。
- 拖动以绘制椭圆。
- 可以执行以下操作中的一个或者两个:
- 要使椭圆从起点处绘制成圆形,请按住SHIFT键并拖动。
- 要从中心点绘制椭圆,请按住CTRL键并拖动。
使用Aspose.Cells for Python via .NET
ShapeCollection 类提供了一个名为 add_oval 的方法,该方法用于向工作表添加椭圆形状。该方法返回一个 Aspose.Cells.Drawing.Oval 对象。类 Aspose.Cells.Drawing.Oval 表示椭圆形状。它具有一些重要成员:
- line_format 属性指定椭圆形状的线条格式属性。
- placement 属性指定了 PlacementType,即椭圆形状附加到工作表中的单元格的方式。
- fill_format 属性指定了形状的填充格式样式。
- lower_right_row 属性指定右下角行索引。
- lower_right_column 属性指定右下角列索引。
以下示例显示了如何将椭圆形状添加到工作表。该示例创建了两个椭圆形状:一个填充的椭圆和一个简单的圆形。
from aspose.cells import Workbook | |
from aspose.cells.drawing import MsoLineDashStyle, PlacementType | |
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. | |
excelbook = Workbook() | |
# Add an oval shape. | |
oval1 = excelbook.worksheets[0].shapes.add_oval(2, 0, 2, 0, 130, 160) | |
# Set the placement of the oval. | |
oval1.placement = PlacementType.FREE_FLOATING | |
# Set the line weight. | |
oval1.line.weight = 1.0 | |
# Set the dash style of the oval. | |
oval1.line.dash_style = MsoLineDashStyle.SOLID | |
# Add another oval (circle) shape. | |
oval2 = excelbook.worksheets[0].shapes.add_oval(9, 0, 2, 15, 130, 130) | |
# Set the placement of the oval. | |
oval2.placement = PlacementType.FREE_FLOATING | |
# Set the line weight. | |
oval2.line.weight = 1.0 | |
# Set the dash style of the oval. | |
oval2.line.dash_style = MsoLineDashStyle.SOLID | |
# Save the excel file. | |
excelbook.save(dataDir + "book1.out.xls") |
向工作表添加微调控件
旋转框是附加在按钮(称为旋钮)上的文本框,包括一个向上箭头和一个向下箭头,点击以逐步更改文本框中的值。通过使用旋转框,可以观察输入变化将如何影响财务模型的输出。可以将旋钮附加到特定的输入单元格。当点击旋钮上的向上或向下箭头时,目标输入单元格中的整数值增加或减少。Aspose.Cells for Python via .NET 允许您在工作表中创建旋转器。
使用Microsoft Excel
在工作表中放置微调框控件:
- 确保表单工具栏已显示。
- 单击微调工具。
- 在工作表区域,单击并拖动以定义将容纳微调器的矩形。 将微调按钮放置在工作表中后,右键单击控件,然后单击格式控件,指定最大、最小和递增值。 在单元格链接字段中,指定与此微调按钮应链接的单元格的地址。 单击确定。
使用Aspose.Cells for Python via .NET
ShapeCollection类提供一个名为add_spinner的方法,用于向工作表添加微调按钮控件。该方法返回一个Aspose.Cells.Drawing.Spinner对象。Aspose.Cells.Drawing.Spinner类表示微调按钮,具有一些重要成员:
linked_cell属性指定与微调按钮关联的单元格。 max属性指定微调按钮范围的最大值。 min属性指定微调按钮范围的最小值。 incremental_change属性指定微调按钮递增一行滚动的值。 shadow属性指示微调按钮是否具有3D阴影。 current_value属性指定微调按钮的当前值。
以下示例显示了如何向工作表添加微调按钮。
from aspose.cells import BackgroundType, Workbook | |
from aspose.cells.drawing import PlacementType | |
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) | |
# Instantiate a new Workbook. | |
excelbook = Workbook() | |
# Get the first worksheet. | |
worksheet = excelbook.worksheets[0] | |
# Get the worksheet cells. | |
cells = worksheet.cells | |
# Input a string value into A1 cell. | |
cells.get("A1").put_value("Select Value:") | |
# Set the font color of the cell. | |
cells.get("A1").get_style().font.color = Color.red | |
# Set the font text bold. | |
cells.get("A1").get_style().font.is_bold = True | |
# Input value into A2 cell. | |
cells.get("A2").put_value(0) | |
# Set the shading color to black with solid background. | |
cells.get("A2").get_style().foreground_color = Color.black | |
cells.get("A2").get_style().pattern = BackgroundType.SOLID | |
# Set the font color of the cell. | |
cells.get("A2").get_style().font.color = Color.white | |
# Set the font text bold. | |
cells.get("A2").get_style().font.is_bold = True | |
# Add a spinner control. | |
spinner = excelbook.worksheets[0].shapes.add_spinner(1, 0, 1, 0, 20, 18) | |
# Set the placement type of the spinner. | |
spinner.placement = PlacementType.FREE_FLOATING | |
# Set the linked cell for the control. | |
spinner.linked_cell = "A2" | |
# Set the maximum value. | |
spinner.max = 10 | |
# Set the minimum value. | |
spinner.min = 0 | |
# Set the incr. change for the control. | |
spinner.incremental_change = 2 | |
# Set it 3-D shading. | |
spinner.shadow = True | |
# Save the excel file. | |
excelbook.save(dataDir + "book1.out.xls") |
向工作表添加滚动条控件
滚动条控件用于帮助以类似于微调按钮控件的方式选择工作表上的数据。通过向工作表添加控件并将其链接到单元格,可以返回控件当前位置的数值。
使用Microsoft Excel
- 若要在Excel 2003及更早版本中添加滚动条,请单击表单工具栏上的滚动条按钮,然后创建一个覆盖B2:B6单元格高度并且大约为列宽四分之一的滚动条。
- 若要在Excel 2007中添加滚动条,请单击开发人员选项卡,单击插入,然后在“表单控件”部分单击滚动条。
- 右键单击滚动条,然后单击格式控件。
- 键入以下信息,然后单击确定:
- 在当前数值框中,键入1。
- 在最小值框中,键入1。此值限制滚动条的顶部到列表中的第一项。
- 在最大值框中,键入20。此数字指定列表中的条目最大数量。
- 在增量更改框中,键入1。该值控制滚动条控制当前值增加多少个数字。
- 在页面更改框中,键入5。这个条目控制当前值的增量,如果你在滚动条内部点击滚动条两侧。
- 将一个数字值放入G1单元格(根据列表中选择的项目),在单元格链接框中输入G1。
- 单击任何单元格,以便滚动条未被选择。
当你单击滚动条上或下的控制时,单元格G1会更新为指示当前滚动条值加/减滚动条的增量的数字。
使用Aspose.Cells for Python via .NET
ShapeCollection类提供了一个名为add_scroll_bar的方法,用于在工作表中添加滚动条控件。该方法返回一个Aspose.Cells.Drawing.ScrollBar对象。类Aspose.Cells.Drawing.ScrollBar表示一个滚动条。它有一些重要成员:
- linked_cell属性指定与滚动条链接的单元格。
- max属性指定滚动条范围的最大值。
- min属性指定滚动条范围的最小值。
- incremental_change属性指定滚动条增量的值。
- shadow属性指示滚动条是否具有3D阴影。
- current_value属性指定滚动条的当前值。
- page_change属性指定如果在滚动条滚动框两侧点击,将要增加多少当前值。
下面的示例展示了如何将滚动条添加到工作表。
from aspose.cells import Workbook | |
from aspose.cells.drawing import PlacementType | |
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) | |
# Instantiate a new Workbook. | |
excelbook = Workbook() | |
# Get the first worksheet. | |
worksheet = excelbook.worksheets[0] | |
# Invisible the gridlines of the worksheet. | |
worksheet.is_gridlines_visible = False | |
# Get the worksheet cells. | |
cells = worksheet.cells | |
# Input a value into A1 cell. | |
cells.get("A1").put_value(1) | |
# Set the font color of the cell. | |
cells.get("A1").get_style().font.color = Color.maroon | |
# Set the font text bold. | |
cells.get("A1").get_style().font.is_bold = True | |
# Set the number format. | |
cells.get("A1").get_style().number = 1 | |
# Add a scrollbar control. | |
scrollbar = worksheet.shapes.add_scroll_bar(0, 0, 1, 0, 125, 20) | |
# Set the placement type of the scrollbar. | |
scrollbar.placement = PlacementType.FREE_FLOATING | |
# Set the linked cell for the control. | |
scrollbar.linked_cell = "A1" | |
# Set the maximum value. | |
scrollbar.max = 20 | |
# Set the minimum value. | |
scrollbar.min = 1 | |
# Set the incr. change for the control. | |
scrollbar.incremental_change = 1 | |
# Set the page change attribute. | |
scrollbar.page_change = 5 | |
# Set it 3-D shading. | |
scrollbar.shadow = True | |
# Save the excel file. | |
excelbook.save(dataDir + "book1.out.xls") |
在工作表中对组控件添加GroupBox控件
有时候你确实需要实现单选按钮或其他属于某个组的控件,可以通过包括一个组框或矩形控件来实现。这两个对象中的任何一个都将作为组的分隔符。添加其中一个形状后,然后你可以添加两个或两个以上的单选按钮或其他组对象。
使用Microsoft Excel
要在工作表中放置一个组框控件并在其中放置控件:
- 要开始一个窗体,在主菜单上,点击查看,然后依次点击工具栏和窗体。
- 在窗体工具栏上,点击组框并在工作表上绘制一个矩形。
- 为框输入说明字符串。
- 在表单工具栏上,点击选项按钮,并点击位于标题字符串下方的分组框内。
- 再次从表单工具栏上,点击选项按钮,并点击位于第一个单选按钮下方的分组框内。
- 再次从表单工具栏上,点击选项按钮,并点击位于上一个单选按钮下方的分组框内。
使用Aspose.Cells for Python via .NET
ShapeCollection类提供了一个名为add_group_box的方法,用于向工作表中添加一个分组框控件。该方法返回一个Aspose.Cells.Drawing.GroupBox对象。此外,ShapeCollection类的group方法将形状分组,它以Shape数组作为参数并返回一个GroupShape对象。Aspose.Cells.Drawing.GroupBox类代表一个分组框。它有一些重要成员:
下面的示例演示了如何在工作表中添加一个分组框和对控件进行分组。
from aspose.cells import Workbook | |
from aspose.cells.drawing import MsoLineDashStyle, PlacementType | |
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. | |
excelbook = Workbook() | |
# Add a group box to the first worksheet. | |
box = excelbook.worksheets[0].shapes.add_group_box(1, 0, 1, 0, 300, 250) | |
# Set the caption of the group box. | |
box.text = "Age Groups" | |
box.placement = PlacementType.FREE_FLOATING | |
# Make it 2-D box. | |
box.shadow = False | |
# Add a radio button. | |
radio1 = excelbook.worksheets[0].shapes.add_radio_button(3, 0, 2, 0, 30, 110) | |
# Set its text string. | |
radio1.text = "20-29" | |
# Set A1 cell as a linked cell for the radio button. | |
radio1.linked_cell = "A1" | |
# Make the radio button 3-D. | |
radio1.shadow = True | |
# Set the weight of the radio button. | |
radio1.line.weight = 4.0 | |
# Set the dash style of the radio button. | |
radio1.line.dash_style = MsoLineDashStyle.SOLID | |
# Add another radio button. | |
radio2 = excelbook.worksheets[0].shapes.add_radio_button(6, 0, 2, 0, 30, 110) | |
# Set its text string. | |
radio2.text = "30-39" | |
# Set A1 cell as a linked cell for the radio button. | |
radio2.linked_cell = "A1" | |
# Make the radio button 3-D. | |
radio2.shadow = True | |
# Set the weight of the radio button. | |
radio2.line.weight = 4.0 | |
# Set the dash style of the radio button. | |
radio2.line.dash_style = MsoLineDashStyle.SOLID | |
# Add another radio button. | |
radio3 = excelbook.worksheets[0].shapes.add_radio_button(9, 0, 2, 0, 30, 110) | |
# Set its text string. | |
radio3.text = "40-49" | |
# Set A1 cell as a linked cell for the radio button. | |
radio3.linked_cell = "A1" | |
# Make the radio button 3-D. | |
radio3.shadow = True | |
# Set the weight of the radio button. | |
radio3.line.weight = 4.0 | |
# Set the dash style of the radio button. | |
radio3.line.dash_style = MsoLineDashStyle.SOLID | |
# Get the shapes. | |
shapeobjects = [box, radio1, radio2, radio3] | |
# Group the shapes. | |
group = excelbook.worksheets[0].shapes.group(shapeobjects) | |
# Save the excel file. | |
excelbook.save(dataDir + "book1.out.xls") |