控件管理
介绍
开发人员可以添加不同的绘图对象,如文本框、复选框、单选按钮、组合框、标签、按钮、直线、矩形、弧线、椭圆、微调控件、滚动条、分组框等。Aspose.Cells 提供了 Aspose.Cells.Drawing 命名空间,其中包含所有的绘图对象。但是,目前还不支持一些绘图对象或形状。可以在 Microsoft Excel 中创建这些绘图对象,并将设计好的电子表格导入到 Aspose.Cells 中。Aspose.Cells 允许从设计好的电子表格加载这些绘图对象,然后将它们写入生成的文件中。
将文本框控件添加到工作表
在报告中强调重要信息的一种方法是使用文本框。例如,添加文本以突出显示公司名称或指示地理区域的最高销售额等。Aspose.Cells 提供 TextBoxCollection 类,用于向集合添加新的文本框。还有另一个类 TextBox,表示用于定义所有类型设置的文本框。它具有一些重要成员:
- TextFrame 属性返回一个 MsoTextFrame 对象,用于调整文本框的内容。
- Placement 属性指定放置类型。
- Font 属性指定字体属性。
- AddHyperlink 方法为文本框添加超链接。
- FillFormat 属性返回一个 MsoFillFormat 对象,用于设置文本框的填充格式。
- LineFormat 属性返回用于设置文本框线条样式和粗细的 MsoLineFormat 对象。
- Text 属性指定文本框的输入文本。
以下示例在工作簿的第一个工作表中创建了两个文本框。第一个文本框配有不同的格式设置。第二个是一个简单的文本框。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet in the book. | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Add a new textbox to the collection. | |
int textboxIndex = worksheet.TextBoxes.Add(2, 1, 160, 200); | |
// Get the textbox object. | |
Aspose.Cells.Drawing.TextBox textbox0 = worksheet.TextBoxes[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.FreeFloating; | |
// Set the font color. | |
textbox0.Font.Color = Color.Blue; | |
// Set the font to bold. | |
textbox0.Font.IsBold = true; | |
// Set the font size. | |
textbox0.Font.Size = 14; | |
// Set font attribute to italic. | |
textbox0.Font.IsItalic = true; | |
// Add a hyperlink to the textbox. | |
textbox0.AddHyperlink("http:// Www.aspose.com/"); | |
// Get the filformat of the textbox. | |
Aspose.Cells.Drawing.FillFormat fillformat = textbox0.Fill; | |
// Get the lineformat type of the textbox. | |
Aspose.Cells.Drawing.LineFormat lineformat = textbox0.Line; | |
// Set the line weight. | |
lineformat.Weight = 6; | |
// Set the dash style to squaredot. | |
lineformat.DashStyle = MsoLineDashStyle.SquareDot; | |
// Add another textbox. | |
textboxIndex = worksheet.TextBoxes.Add(15, 4, 85, 120); | |
// Get the second textbox. | |
Aspose.Cells.Drawing.TextBox textbox1 = worksheet.TextBoxes[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.MoveAndSize; | |
// Save the excel file. | |
workbook.Save(dataDir + "book1.out.xls"); |
在设计器电子表格中操作文本框控件
Aspose.Cells还允许您访问设计器工作表中的文本框并对其进行操作。使用Worksheet.TextBoxes属性获取工作表中的文本框集合。
以下示例使用上面示例中创建的 Microsoft Excel 文件。它获取了两个文本框的文本字符串,并将第二个文本框的文本更改后保存了文件。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate a new Workbook. | |
// Open the existing excel file. | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Get the first worksheet in the book. | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Get the first textbox object. | |
Aspose.Cells.Drawing.TextBox textbox0 = worksheet.TextBoxes[0]; | |
// Obtain the text in the first textbox. | |
string text0 = textbox0.Text; | |
// Get the second textbox object. | |
Aspose.Cells.Drawing.TextBox textbox1 = worksheet.TextBoxes[1]; | |
// Obtain the text in the second textbox. | |
string 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"); |
将复选框控件添加到工作表
如果您希望为用户提供在两个选项之间进行选择的方式,例如true或false;yes或no,复选框非常有用。Aspose.Cells允许您在工作表中使用复选框。例如,您可能已经开发了一个财务预测工作表,在其中您可以选择是否考虑某个收购。在这种情况下,您可能想要在工作表顶部放置一个复选框。然后,您可以将该复选框的状态链接到另一个单元格,以便如果选择了复选框,则单元格的值为True;如果未选择,则单元格的值为False。
使用Microsoft Excel
要在工作表中放置复选框控件,请按照以下步骤进行:
- 确保显示“表单”工具栏。
- 单击“表单”工具栏上的复选框工具。
- 在工作表区域,单击并拖动以定义容纳复选框和复选框旁边标签的矩形。
- 放置复选框后,将鼠标光标移至标签区域并更改标签。
- 在单元格链接字段中,指定应链接到该复选框的单元格地址。
- 单击确定。
使用Aspose.Cells
Aspose.Cells提供了CheckBoxCollection类,用于添加新的复选框到集合中。还有另一个类Aspose.Cells.Drawing.CheckBox,代表一个复选框。它具有一些重要成员:
- LinkedCell属性指定与复选框链接的单元格。
- Text属性指定与复选框关联的文本字符串。它是复选框的标签。
- Value属性指定复选框是否已选中。
以下示例显示如何向工作表添加复选框。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook excelbook = new Workbook(); | |
// Add a checkbox to the first worksheet in the workbook. | |
int index = excelbook.Worksheets[0].CheckBoxes.Add(5, 5, 100, 120); | |
// Get the checkbox object. | |
Aspose.Cells.Drawing.CheckBox checkbox = excelbook.Worksheets[0].CheckBoxes[index]; | |
// Set its text string. | |
checkbox.Text = "Click it!"; | |
// Put a value into B1 cell. | |
excelbook.Worksheets[0].Cells["B1"].PutValue("LnkCell"); | |
// Set B1 cell as a linked cell for the checkbox. | |
checkbox.LinkedCell = "B1"; | |
// Check the checkbox by default. | |
checkbox.Value = true; | |
// Save the excel file. | |
excelbook.Save(dataDir + "book1.out.xls"); |
向工作表添加单选按钮控件
单选按钮,或选项按钮,是一个由圆形框制成的控件。用户通过选择圆形框来做出选择。单选按钮通常,如果不是总是,会以其他形式出现和表现。这样的单选按钮会以组的形式出现和表现。用户可以通过只选择其中一个来决定哪一个按钮是有效的。当用户点击其中一个按钮时,它会被填充。当组中的一个按钮被选中时,同一组的按钮是空的。
使用Microsoft Excel
要在工作表中放置单选按钮控件,请按照以下步骤执行:
- 确保表单工具栏已显示。
- 单击 选项按钮 工具。
- 在工作表中,单击并拖动以定义将容纳选项按钮和选项按钮旁边标签的矩形。
- 一旦单选按钮放置在工作表中,将鼠标光标移入标签区域并更改标签。
- 在 单元格链接 字段中,指定应与此单选按钮链接的单元格的地址。
- 点击确定。
使用Aspose.Cells
Aspose.Cells.Drawing.ShapeCollection 类提供了一个名为 AddRadioButton 的方法,用于向工作表添加单选按钮控件。该方法返回一个 Aspose.Cells.Drawing.RadioButton 对象。类 Aspose.Cells.Drawing.RadioButton 表示一个选项按钮。它有一些重要成员:
- LinkedCell 属性指定与单选按钮关联的单元格。
- Text 属性指定与单选按钮相关联的文本字符串。它是单选按钮的标签。
- IsChecked 属性指定单选按钮是否选中。
- FillFormat 属性指定单选按钮的填充格式。
- LineFormat 属性指定选项按钮的线条格式样式。
以下示例显示了如何向工作表添加单选按钮。该示例添加了代表年龄组的三个单选按钮。
向工作表添加组合框控件
为了更容易进行数据输入,或者将输入限制为您定义的某些项,您可以创建一个组合框或下拉列表,这些有效条目是从工作表上的其他单元格编制而成的。当您为单元格创建下拉列表时,它会在该单元格旁边显示一个箭头。要在该单元格中输入信息,单击箭头,然后单击所需的条目。
使用Microsoft Excel
要在工作表中放置组合框控件,请按照以下步骤执行:
- 确保表单工具栏已显示。
- 单击 组合框 工具。
- 在您的工作表区域,单击并拖动以定义将容纳组合框的矩形。
- 一旦组合框放置在工作表中,请右键单击控件,选择设置控件格式并指定输入范围。
- 在单元格链接字段中,指定应链接到该组合框的单元格地址。
- 单击确定。
使用Aspose.Cells
Aspose.Cells.Drawing.ShapeCollection 类提供了一个名为 AddComboBox 的方法,用于向工作表添加组合框控件。该方法返回一个 Aspose.Cells.Drawing.ComboBox 对象。类 Aspose.Cells.Drawing.ComboBox 表示一个组合框。它有一些重要成员:
- LinkedCell 属性指定与组合框关联的单元格。
- InputRange 属性指定用于填充组合框的工作表单元格范围。
- DropDownLines 属性指定下拉部分中显示的列表行数。
- Shadow属性指示下拉框是否具有3D阴影。
以下示例显示如何向工作表添加下拉框。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Create a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet. | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Get the worksheet cells collection. | |
Cells cells = sheet.Cells; | |
// Input a value. | |
cells["B3"].PutValue("Employee:"); | |
// Set it bold. | |
cells["B3"].GetStyle().Font.IsBold = true; | |
// Input some values that denote the input range | |
// For the combo box. | |
cells["A2"].PutValue("Emp001"); | |
cells["A3"].PutValue("Emp002"); | |
cells["A4"].PutValue("Emp003"); | |
cells["A5"].PutValue("Emp004"); | |
cells["A6"].PutValue("Emp005"); | |
cells["A7"].PutValue("Emp006"); | |
// Add a new combo box. | |
Aspose.Cells.Drawing.ComboBox comboBox = sheet.Shapes.AddComboBox(2, 0, 2, 0, 22, 100); |
将标签控件添加到工作表
标签是向用户提供有关电子表格内容的信息的手段。Aspose.Cells使得可以向工作表中添加和操作标签。ShapeCollection类提供了一个名为AddLabel的方法,用于向工作表添加标签控件。该方法返回一个Label对象。Label类表示工作表中的标签。它有一些重要成员:
- Text方法指定标签的标题字符串。
- Placement方法指定PlacementType,即标签附加到工作表中的单元格的方式。
以下示例显示如何向工作表添加标签。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Create a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet in the workbook. | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add a new label to the worksheet. | |
Aspose.Cells.Drawing.Label label = sheet.Shapes.AddLabel(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.FreeFloating; | |
// Saves the file. | |
workbook.Save(dataDir + "book1.out.xls"); |
将列表框控件添加到工作表
列表框控件创建一个列表控件,允许选择单个或多个项目。
使用Microsoft Excel
要在工作表中放置列表框控件:
- 确保表单工具栏已显示。
- 点击列表框工具。
- 在工作表区域,单击并拖动以定义将容纳列表框的矩形。
- 将列表框放置在工作表中后,右键单击控件,然后点击格式控件,并指定输入范围。
- 在单元格链接字段中,指定应将该列表框链接到的单元格地址,并设置选择类型(单选,多选,扩展)属性
- 点击确定。
使用Aspose.Cells
ShapeCollection类提供了一个名为AddListBox的方法,用于向工作表添加列表框控件。该方法返回一个Aspose.Cells.Drawing.ListBox对象。ListBox类表示列表框。它有一些重要成员:
- LinkedCell方法指定与列表框链接的单元格。
- InputRange方法指定用于填充列表框的工作表单元格范围。
- SelectionType方法指定列表框的选择模式。
- Shadow方法指示列表框是否具有3D阴影。
以下示例显示了如何向工作表中添加列表框。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Create a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet. | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Get the worksheet cells collection. | |
Cells cells = sheet.Cells; | |
// Input a value. | |
cells["B3"].PutValue("Choose Dept:"); | |
// Set it bold. | |
cells["B3"].GetStyle().Font.IsBold = true; | |
// Input some values that denote the input range | |
// For the list box. | |
cells["A2"].PutValue("Sales"); | |
cells["A3"].PutValue("Finance"); | |
cells["A4"].PutValue("MIS"); | |
cells["A5"].PutValue("R&D"); | |
cells["A6"].PutValue("Marketing"); | |
cells["A7"].PutValue("HRA"); | |
// Add a new list box. | |
Aspose.Cells.Drawing.ListBox listBox = sheet.Shapes.AddListBox(2, 0, 3, 0, 122, 100); | |
// Set the placement type. | |
listBox.Placement = PlacementType.FreeFloating; | |
// Set the linked cell. | |
listBox.LinkedCell = "A1"; | |
// Set the input range. | |
listBox.InputRange = "A2:A7"; | |
// Set the selection tyle. | |
listBox.SelectionType = 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
ShapeCollection类提供了一个名为AddButton的方法,用于向工作表中添加按钮控件。该方法返回一个Aspose.Cells.Drawing.Button对象。类Aspose.Cells.Drawing.Button表示按钮,具有一些重要成员:
- Text属性指定按钮的标题。
- Font属性指定按钮控件标签的字体属性。
- Placement属性指定PlacementType,按钮与工作表中的单元格的连接方式。
- AddHyperlink属性为按钮控件添加超链接。单击按钮将导航到相关的URL。
以下示例显示了如何向工作表中添加按钮。
向工作表添加线控件
使用Microsoft Excel
- 在 绘图 工具栏上,单击 自选图形,指向 线条,然后选择所需的线条样式。
- 拖动以绘制线条。
- 执行以下操作中的一个或两个:
- 要限制线条以与起点呈15度角的方式绘制,请在拖动时按住 SHIFT 键。
- 要使线条从第一个端点向相反方向延伸,请在拖动时按住 CTRL 键。
使用Aspose.Cells
ShapeCollection 类提供了一个名为 AddLine 的方法,用于向工作表添加线形状。该方法返回一个 LineShape 对象。类 LineShape 表示一条线。它有一些重要成员:
- LineFormat 方法指定线的格式。
- Placement 方法指定 PlacementType,线如何连接到工作表中的单元格。
以下示例展示了如何向工作表添加线条。它创建了三条具有不同样式的线。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet in the book. | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Add a new line to the worksheet. | |
Aspose.Cells.Drawing.LineShape line1 = worksheet.Shapes.AddLine(5, 0, 1, 0, 0, 250); | |
// Set the line dash style | |
line1.Line.DashStyle = MsoLineDashStyle.Solid; | |
// Set the placement. | |
line1.Placement = PlacementType.FreeFloating; | |
// Add another line to the worksheet. | |
Aspose.Cells.Drawing.LineShape line2 = worksheet.Shapes.AddLine(7, 0, 1, 0, 85, 250); | |
// Set the line dash style. | |
line2.Line.DashStyle = MsoLineDashStyle.DashLongDash; | |
// Set the weight of the line. | |
line2.Line.Weight = 4; | |
// Set the placement. | |
line2.Placement = PlacementType.FreeFloating; | |
// Add the third line to the worksheet. | |
Aspose.Cells.Drawing.LineShape line3 = worksheet.Shapes.AddLine(13, 0, 1, 0, 0, 250); | |
// Set the line dash style | |
line3.Line.DashStyle = MsoLineDashStyle.Solid; | |
// Set the placement. | |
line3.Placement = PlacementType.FreeFloating; | |
// Make the gridlines invisible in the first worksheet. | |
workbook.Worksheets[0].IsGridlinesVisible = false; | |
// Save the excel file. | |
workbook.Save(dataDir + "book1.out.xls"); |
向线条添加箭头
Aspose.Cells 还允许您绘制箭头线条。您可以向一条线条添加箭头,并格式化该线。例如,您可以更改线条的颜色,或指定线条的粗细和样式。
以下示例展示了如何向线添加箭头。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet in the book. | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Add a line to the worksheet | |
Aspose.Cells.Drawing.LineShape line2 = worksheet.Shapes.AddLine(7, 0, 1, 0, 85, 250); | |
// Set the line color | |
line2.Line.FillType = FillType.Solid; | |
line2.Line.SolidFill.Color = Color.Blue; | |
// Set the weight of the line. | |
line2.Line.Weight = 3; | |
// Set the placement. | |
line2.Placement = PlacementType.FreeFloating; | |
// Set the line arrows. | |
line2.Line.EndArrowheadWidth = MsoArrowheadWidth.Medium; | |
line2.Line.EndArrowheadStyle = MsoArrowheadStyle.Arrow; | |
line2.Line.EndArrowheadLength = MsoArrowheadLength.Medium; | |
line2.Line.BeginArrowheadStyle = MsoArrowheadStyle.ArrowDiamond; | |
line2.Line.BeginArrowheadLength = MsoArrowheadLength.Medium; | |
// Make the gridlines invisible in the first worksheet. | |
workbook.Worksheets[0].IsGridlinesVisible = false; | |
// Save the excel file. | |
workbook.Save(dataDir + "book1.out.xlsx"); |
在工作表中添加矩形控件
Aspose.Cells允许您在工作表中绘制矩形形状。您可以创建矩形、正方形等形状,还可以格式化控件的填充颜色和边框线颜色。例如,您可以更改矩形的颜色,设置阴影颜色,指定矩形的重量和样式以满足您的需求。
使用Microsoft Excel
- 在绘图工具栏上,单击矩形。
- 拖动绘制矩形。
- 执行以下操作中的一个或两个:
- 若要从起点绘制正方形并约束矩形,请按住SHIFT键并拖动。
- 若要从中心点绘制矩形,请按住CTRL键并拖动。
使用Aspose.Cells
ShapeCollection 类提供了一个名为 AddRectangle 的方法,用于向工作表添加矩形形状。该方法返回一个 Aspose.Cells.Drawing.RectangleShape 对象。类 Aspose.Cells.Drawing.RectangleShape 表示一个矩形。它有一些重要成员:
- LineFormat 属性指定矩形的线条格式属性。
- Placement 属性指定 PlacementType,矩形如何连接到工作表中的单元格。
- FillFormat 属性指定矩形的填充格式样式。
以下示例展示了如何向工作表添加矩形。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook excelbook = new Workbook(); | |
// Add a rectangle control. | |
Aspose.Cells.Drawing.RectangleShape rectangle = excelbook.Worksheets[0].Shapes.AddRectangle(3, 0, 2, 0, 70, 130); | |
// Set the placement of the rectangle. | |
rectangle.Placement = PlacementType.FreeFloating; | |
// Set the line weight. | |
rectangle.Line.Weight = 4; | |
// Set the dash style of the rectangle. | |
rectangle.Line.DashStyle = MsoLineDashStyle.Solid; | |
// Save the excel file. | |
excelbook.Save(dataDir + "book1.out.xls"); |
向工作表添加弧形控件
Aspose.Cells允许您在工作表中绘制弧形。您可以创建简单的填充弧形。您可以格式化控件的填充颜色和边框线颜色。例如,您可以指定/更改弧形的颜色,设置阴影颜色,指定形状的重量和样式以满足您的需求。
使用Microsoft Excel
- 在绘图工具栏上,单击AutoShapes中的弧形。
- 拖动绘制弧形。
使用Aspose.Cells
ShapeCollection 类提供了一个名为 AddArc 的方法,用于向工作表添加弧形状。该方法返回一个 Aspose.Cells.Drawing.ArcShape 对象。类 Aspose.Cells.Drawing.ArcShape 表示一条弧。它有一些重要成员:
- LineFormat 属性指定弧形状的线条格式属性。
- Placement 属性指定 PlacementType,弧形状如何连接到工作表中的单元格。
- FillFormat 属性指定了形状的填充格式样式。
- LowerRightRow 属性指定右下角行索引。
- LowerRightColumn 属性指定右下角列索引。
以下示例显示了如何将弧形形状添加到工作表。该示例创建了两个弧形形状:一个填充,另一个是简单的。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook excelbook = new Workbook(); | |
// Add an arc shape. | |
Aspose.Cells.Drawing.ArcShape arc1 = excelbook.Worksheets[0].Shapes.AddArc(2, 0, 2, 0, 130, 130); | |
// Set the fill shape color | |
arc1.Fill.FillType = FillType.Solid; | |
arc1.Fill.SolidFill.Color = Color.Blue; | |
// Set the placement of the arc. | |
arc1.Placement = PlacementType.FreeFloating; | |
// Set the line weight. | |
arc1.Line.Weight = 1; | |
// Set the dash style of the arc. | |
arc1.Line.DashStyle = MsoLineDashStyle.Solid; | |
// Add another arc shape. | |
Aspose.Cells.Drawing.ArcShape arc2 = excelbook.Worksheets[0].Shapes.AddArc(9, 0, 2, 0, 130, 130); | |
// Set the line color | |
arc2.Line.FillType = FillType.Solid; | |
arc2.Line.SolidFill.Color = Color.Blue; | |
// Set the placement of the arc. | |
arc2.Placement = PlacementType.FreeFloating; | |
// Set the line weight. | |
arc2.Line.Weight = 1; | |
// Set the dash style of the arc. | |
arc2.Line.DashStyle = MsoLineDashStyle.Solid; | |
// Save the excel file. | |
excelbook.Save(dataDir + "book1.out.xls"); |
向工作表添加椭圆控件
Aspose.Cells允许您在工作表中绘制椭圆形状。创建简单和填充的椭圆形状,并格式化控件的填充颜色和边框线颜色。例如,您可以指定/更改椭圆的颜色,设置阴影颜色,指定形状的宽度和样式。
使用Microsoft Excel
- 在绘图工具栏上,单击椭圆。
- 拖动以绘制椭圆。
- 可以执行以下操作中的一个或者两个:
- 要使椭圆从起点处绘制成圆形,请按住SHIFT键并拖动。
- 要从中心点绘制椭圆,请按住CTRL键并拖动。
使用Aspose.Cells
ShapeCollection 类提供了一个名为 AddOval 的方法,该方法用于向工作表添加椭圆形状。该方法返回一个 Aspose.Cells.Drawing.Oval 对象。类 Aspose.Cells.Drawing.Oval 表示椭圆形状。它具有一些重要成员:
- LineFormat 属性指定椭圆形状的线条格式属性。
- Placement 属性指定了 PlacementType,即椭圆形状附加到工作表中的单元格的方式。
- FillFormat 属性指定了形状的填充格式样式。
- LowerRightRow 属性指定右下角行索引。
- LowerRightColumn 属性指定右下角列索引。
以下示例显示了如何将椭圆形状添加到工作表。该示例创建了两个椭圆形状:一个填充的椭圆和一个简单的圆形。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook excelbook = new Workbook(); | |
// Add an oval shape. | |
Aspose.Cells.Drawing.Oval oval1 = excelbook.Worksheets[0].Shapes.AddOval(2, 0, 2, 0, 130, 160); | |
// Set the placement of the oval. | |
oval1.Placement = PlacementType.FreeFloating; | |
// Set the line weight. | |
oval1.Line.Weight = 1; | |
// Set the dash style of the oval. | |
oval1.Line.DashStyle = MsoLineDashStyle.Solid; | |
// Add another oval (circle) shape. | |
Aspose.Cells.Drawing.Oval oval2 = excelbook.Worksheets[0].Shapes.AddOval(9, 0, 2, 15, 130, 130); | |
// Set the placement of the oval. | |
oval2.Placement = PlacementType.FreeFloating; | |
// Set the line weight. | |
oval2.Line.Weight = 1; | |
// Set the dash style of the oval. | |
oval2.Line.DashStyle = MsoLineDashStyle.Solid; | |
// Save the excel file. | |
excelbook.Save(dataDir + "book1.out.xls"); |
向工作表添加微调控件
微调框是附加到按钮(称为微调按钮)的文本框,由上箭头和下箭头组成,点击它们可以逐渐改变文本框中的值。通过使用微调框,您可以查看输入对财务模型的变化将如何改变模型输出。您可以将微调按钮附加到特定输入单元格。当您点击微调按钮上的上箭头或下箭头时,目标输入单元格中的整数值增加或减少。Aspose.Cells允许您在工作表中创建微调器。
使用Microsoft Excel
在工作表中放置微调框控件:
- 确保表单工具栏已显示。
- 单击微调工具。
- 在工作表区域,单击并拖动以定义将容纳微调器的矩形。 将微调按钮放置在工作表中后,右键单击控件,然后单击格式控件,指定最大、最小和递增值。 在单元格链接字段中,指定与此微调按钮应链接的单元格的地址。 单击确定。
使用Aspose.Cells
ShapeCollection类提供一个名为AddSpinner的方法,用于向工作表添加微调按钮控件。该方法返回一个Aspose.Cells.Drawing.Spinner对象。Aspose.Cells.Drawing.Spinner类表示微调按钮,具有一些重要成员:
LinkedCell属性指定与微调按钮关联的单元格。 Max属性指定微调按钮范围的最大值。 Min属性指定微调按钮范围的最小值。 IncrementalChange属性指定微调按钮递增一行滚动的值。 Shadow属性指示微调按钮是否具有3D阴影。 CurrentValue属性指定微调按钮的当前值。
以下示例显示了如何向工作表添加微调按钮。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook excelbook = new Workbook(); | |
// Get the first worksheet. | |
Worksheet worksheet = excelbook.Worksheets[0]; | |
// Get the worksheet cells. | |
Cells cells = worksheet.Cells; | |
// Input a string value into A1 cell. | |
cells["A1"].PutValue("Select Value:"); | |
// Set the font color of the cell. | |
cells["A1"].GetStyle().Font.Color = Color.Red; | |
// Set the font text bold. | |
cells["A1"].GetStyle().Font.IsBold = true; | |
// Input value into A2 cell. | |
cells["A2"].PutValue(0); | |
// Set the shading color to black with solid background. | |
cells["A2"].GetStyle().ForegroundColor = Color.Black; | |
cells["A2"].GetStyle().Pattern = BackgroundType.Solid; | |
// Set the font color of the cell. | |
cells["A2"].GetStyle().Font.Color = Color.White; | |
// Set the font text bold. | |
cells["A2"].GetStyle().Font.IsBold = true; | |
// Add a spinner control. | |
Aspose.Cells.Drawing.Spinner spinner = excelbook.Worksheets[0].Shapes.AddSpinner(1, 0, 1, 0, 20, 18); | |
// Set the placement type of the spinner. | |
spinner.Placement = PlacementType.FreeFloating; | |
// Set the linked cell for the control. | |
spinner.LinkedCell = "A2"; | |
// Set the maximum value. | |
spinner.Max = 10; | |
// Set the minimum value. | |
spinner.Min = 0; | |
// Set the incr. change for the control. | |
spinner.IncrementalChange = 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
ShapeCollection类提供了一个名为AddScrollBar的方法,用于在工作表中添加滚动条控件。该方法返回一个Aspose.Cells.Drawing.ScrollBar对象。类Aspose.Cells.Drawing.ScrollBar表示一个滚动条。它有一些重要成员:
- LinkedCell属性指定与滚动条链接的单元格。
- Max属性指定滚动条范围的最大值。
- Min属性指定滚动条范围的最小值。
- IncrementalChange属性指定滚动条增量的值。
- Shadow属性指示滚动条是否具有3D阴影。
- CurrentValue属性指定滚动条的当前值。
- PageChange属性指定如果在滚动条滚动框两侧点击,将要增加多少当前值。
下面的示例展示了如何将滚动条添加到工作表。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook excelbook = new Workbook(); | |
// Get the first worksheet. | |
Worksheet worksheet = excelbook.Worksheets[0]; | |
// Invisible the gridlines of the worksheet. | |
worksheet.IsGridlinesVisible = false; | |
// Get the worksheet cells. | |
Cells cells = worksheet.Cells; | |
// Input a value into A1 cell. | |
cells["A1"].PutValue(1); | |
// Set the font color of the cell. | |
cells["A1"].GetStyle().Font.Color = Color.Maroon; | |
// Set the font text bold. | |
cells["A1"].GetStyle().Font.IsBold = true; | |
// Set the number format. | |
cells["A1"].GetStyle().Number = 1; | |
// Add a scrollbar control. | |
Aspose.Cells.Drawing.ScrollBar scrollbar = worksheet.Shapes.AddScrollBar(0, 0, 1, 0, 125, 20); | |
// Set the placement type of the scrollbar. | |
scrollbar.Placement = PlacementType.FreeFloating; | |
// Set the linked cell for the control. | |
scrollbar.LinkedCell = "A1"; | |
// Set the maximum value. | |
scrollbar.Max = 20; | |
// Set the minimum value. | |
scrollbar.Min = 1; | |
// Set the incr. change for the control. | |
scrollbar.IncrementalChange = 1; | |
// Set the page change attribute. | |
scrollbar.PageChange = 5; | |
// Set it 3-D shading. | |
scrollbar.Shadow = true; | |
// Save the excel file. | |
excelbook.Save(dataDir + "book1.out.xls"); |
在工作表中对组控件添加GroupBox控件
有时候你确实需要实现单选按钮或其他属于某个组的控件,可以通过包括一个组框或矩形控件来实现。这两个对象中的任何一个都将作为组的分隔符。添加其中一个形状后,然后你可以添加两个或两个以上的单选按钮或其他组对象。
使用Microsoft Excel
要在工作表中放置一个组框控件并在其中放置控件:
- 要开始一个窗体,在主菜单上,点击查看,然后依次点击工具栏和窗体。
- 在窗体工具栏上,点击组框并在工作表上绘制一个矩形。
- 为框输入说明字符串。
- 在表单工具栏上,点击选项按钮,并点击位于标题字符串下方的分组框内。
- 再次从表单工具栏上,点击选项按钮,并点击位于第一个单选按钮下方的分组框内。
- 再次从表单工具栏上,点击选项按钮,并点击位于上一个单选按钮下方的分组框内。
使用Aspose.Cells
ShapeCollection类提供了一个名为AddGroupBox的方法,用于向工作表中添加一个分组框控件。该方法返回一个Aspose.Cells.Drawing.GroupBox对象。此外,ShapeCollection类的Group方法将形状分组,它以Shape数组作为参数并返回一个GroupShape对象。Aspose.Cells.Drawing.GroupBox类代表一个分组框。它有一些重要成员:
下面的示例演示了如何在工作表中添加一个分组框和对控件进行分组。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook excelbook = new Workbook(); | |
// Add a group box to the first worksheet. | |
Aspose.Cells.Drawing.GroupBox box = excelbook.Worksheets[0].Shapes.AddGroupBox(1, 0, 1, 0, 300, 250); | |
// Set the caption of the group box. | |
box.Text = "Age Groups"; | |
box.Placement = PlacementType.FreeFloating; | |
// Make it 2-D box. | |
box.Shadow = false; | |
// Add a radio button. | |
Aspose.Cells.Drawing.RadioButton radio1 = excelbook.Worksheets[0].Shapes.AddRadioButton(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.LinkedCell = "A1"; | |
// Make the radio button 3-D. | |
radio1.Shadow = true; | |
// Set the weight of the radio button. | |
radio1.Line.Weight = 4; | |
// Set the dash style of the radio button. | |
radio1.Line.DashStyle = MsoLineDashStyle.Solid; | |
// Add another radio button. | |
Aspose.Cells.Drawing.RadioButton radio2 = excelbook.Worksheets[0].Shapes.AddRadioButton(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.LinkedCell = "A1"; | |
// Make the radio button 3-D. | |
radio2.Shadow = true; | |
// Set the weight of the radio button. | |
radio2.Line.Weight = 4; | |
// Set the dash style of the radio button. | |
radio2.Line.DashStyle = MsoLineDashStyle.Solid; | |
// Add another radio button. | |
Aspose.Cells.Drawing.RadioButton radio3 = excelbook.Worksheets[0].Shapes.AddRadioButton(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.LinkedCell = "A1"; | |
// Make the radio button 3-D. | |
radio3.Shadow = true; | |
// Set the weight of the radio button. | |
radio3.Line.Weight = 4; | |
// Set the dash style of the radio button. | |
radio3.Line.DashStyle = MsoLineDashStyle.Solid; | |
// Get the shapes. | |
Aspose.Cells.Drawing.Shape[] shapeobjects = new Aspose.Cells.Drawing.Shape[] { box, radio1, radio2, radio3 }; | |
// Group the shapes. | |
Aspose.Cells.Drawing.GroupShape group = excelbook.Worksheets[0].Shapes.Group(shapeobjects); | |
// Save the excel file. | |
excelbook.Save(dataDir + "book1.out.xls"); |