控件管理

介绍

开发人员可以添加不同的绘图对象,如文本框、复选框、单选按钮、组合框、标签、按钮、直线、矩形、弧线、椭圆、微调控件、滚动条、分组框等。Aspose.Cells 提供了 Aspose.Cells.Drawing 命名空间,其中包含所有的绘图对象。但是,目前还不支持一些绘图对象或形状。可以在 Microsoft Excel 中创建这些绘图对象,并将设计好的电子表格导入到 Aspose.Cells 中。Aspose.Cells 允许从设计好的电子表格加载这些绘图对象,然后将它们写入生成的文件中。

向工作表添加文本框控件

在报告中强调重要信息的一种方式是使用文本框。例如,添加文本以突出公司名称或指示销售额最高的地理区域等。Aspose.Cells 提供了 TextBoxes 类,用于向集合中添加新的文本框。还有另一个类,TextBox,表示用于定义各种设置的文本框。它具有一些重要成员:

以下示例在工作簿的第一个工作表中创建了两个文本框。第一个文本框配有不同的格式设置。第二个是一个简单的文本框。

执行代码后生成以下输出:

在工作表中创建了两个文本框

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AddingTextBoxControl.class) + "DrawingObjects/";
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the book.
Worksheet worksheet = workbook.getWorksheets().get(0);
// Get the textbox object.
int textboxIndex = worksheet.getTextBoxes().add(2, 1, 160, 200);
TextBox textbox0 = worksheet.getTextBoxes().get(textboxIndex);
// Fill the text.
textbox0.setText("ASPOSE______The .NET & JAVA Component Publisher!");
// Set the placement.
textbox0.setPlacement(PlacementType.FREE_FLOATING);
// Set the font color.
textbox0.getFont().setColor(Color.getBlue());
// Set the font to bold.
textbox0.getFont().setBold(true);
// Set the font size.
textbox0.getFont().setSize(14);
// Set font attribute to italic.
textbox0.getFont().setItalic(true);
// Add a hyperlink to the textbox.
textbox0.addHyperlink("http://www.aspose.com/");
// Get the filformat of the textbox.
FillFormat fillformat = textbox0.getFill();
// Set the fillcolor.
fillformat.setOneColorGradient(Color.getSilver(), 1, GradientStyleType.HORIZONTAL, 1);
// Get the lineformat type of the textbox.
LineFormat lineformat = textbox0.getLine();
// Set the line style.
lineformat.setDashStyle(MsoLineStyle.THIN_THICK);
// Set the line weight.
lineformat.setWeight(6);
// Set the dash style to squaredot.
lineformat.setDashStyle(MsoLineDashStyle.SQUARE_DOT);
// Get the second textbox.
TextBox textbox1 = (com.aspose.cells.TextBox) worksheet.getShapes().addShape(MsoDrawingType.TEXT_BOX, 15, 0, 4,
0, 85, 120);
// Input some text to it.
textbox1.setText("This is another simple text box");
// Set the placement type as the textbox will move and resize with cells.
textbox1.setPlacement(PlacementType.MOVE_AND_SIZE);
// Save the excel file.
workbook.save(dataDir + "AddingTextBoxControl_out.xls");

在设计器电子表格中操作文本框控件

Aspose.Cells还允许您访问设计器工作表中的文本框并对其进行操作。使用Worksheet.getTextBoxes属性获取工作表中的文本框集合。

下面的例子使用我们在上面的例子中创建的Microsoft Excel文件 - tsttextboxes.xls。它获取两个文本框的文本字符串并更改第二个文本框的文本以保存文件。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ManipulatingTextBoxControls.class);
// Instantiate a new Workbook.
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Get the first worksheet in the book.
Worksheet worksheet = workbook.getWorksheets().get(0);
// Get the first textbox object.
com.aspose.cells.TextBox textbox0 = worksheet.getTextBoxes().get(0);
// Obtain the text in the first textbox.
String text0 = textbox0.getText();
System.out.println(text0);
// Get the second textbox object.
com.aspose.cells.TextBox textbox1 = worksheet.getTextBoxes().get(1);
// Obtain the text in the second textbox.
String text1 = textbox1.getText();
// Change the text of the second textbox.
textbox1.setText("This is an alternative text");
// Save the excel file.
workbook.save(dataDir + "tsttextboxes1.xls");

向工作表添加复选框控件

如果您希望为用户提供在两个选项之间进行选择的方式,例如true或false;yes或no,复选框非常有用。Aspose.Cells允许您在工作表中使用复选框。例如,您可能已经开发了一个财务预测工作表,在其中您可以选择是否考虑某个收购。在这种情况下,您可能想要在工作表顶部放置一个复选框。然后,您可以将该复选框的状态链接到另一个单元格,以便如果选择了复选框,则单元格的值为True;如果未选择,则单元格的值为False。

使用Microsoft Excel

要在工作表中放置复选框控件,请按照以下步骤进行:

  1. 确保显示“表单”工具栏。
  2. 单击“表单”工具栏上的复选框工具。
  3. 在工作表区域,单击并拖动以定义容纳复选框和复选框旁边标签的矩形。
  4. 放置复选框后,将鼠标光标移至标签区域并更改标签。
  5. 单元格链接字段中,指定应链接到该复选框的单元格地址。
  6. 单击确定

使用Aspose.Cells

Aspose.Cells提供了CheckBoxCollection类,用于添加新的复选框到集合中。还有另一个类Aspose.Cells.Drawing.CheckBox,代表一个复选框。它具有一些重要成员:

  • 方法setLinkedCell指定与复选框关联的单元格。
  • 方法setText指定与复选框关联的文本字符串。这是复选框的标签。
  • 方法setValue指定复选框是否被选择。

下面的例子显示了如何向工作表中添加复选框。执行代码后生成下面的输出。

在工作表中添加了一个复选框

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(AddingCheckBoxControl.class);
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the book.
Worksheet worksheet = workbook.getWorksheets().get(0);
// Add a checkbox to the first worksheet in the workbook.
int checkBoxIndex = worksheet.getCheckBoxes().add(5, 5, 100, 120);
CheckBox checkBox = worksheet.getCheckBoxes().get(checkBoxIndex);
// Set its text string.
checkBox.setText("Check it!");
// Put a value into B1 cell.
worksheet.getCells().get("B1").setValue("LnkCell");
// Set B1 cell as a linked cell for the checkbox.
checkBox.setLinkedCell("=B1");
// Save the excel file.
workbook.save(dataDir + "tstcheckboxes.xls");

向工作表添加单选按钮控件

单选按钮,或选项按钮,是一个由圆形框制成的控件。用户通过选择圆形框来做出选择。单选按钮通常,如果不是总是,会以其他形式出现和表现。这样的单选按钮会以组的形式出现和表现。用户可以通过只选择其中一个来决定哪一个按钮是有效的。当用户点击其中一个按钮时,它会被填充。当组中的一个按钮被选中时,同一组的按钮是空的。

使用Microsoft Excel

要在工作表中放置单选按钮控件,请按照以下步骤执行:

  1. 确保表单工具栏已显示。
  2. 单击 选项按钮 工具。
  3. 在工作表中,单击并拖动以定义将容纳选项按钮和选项按钮旁边标签的矩形。
  4. 一旦单选按钮放置在工作表中,将鼠标光标移入标签区域并更改标签。
  5. 单元格链接 字段中,指定应与此单选按钮链接的单元格的地址。
  6. 点击确定

使用Aspose.Cells

ShapeCollection 类提供了一个名为 addShape 的方法,可以用来向工作表添加单选按钮控件。该方法可能会返回一个 RadioButton 对象。RadioButton 类表示一个选项按钮。它具有一些重要成员:

  • setLinkedCell 方法指定与单选按钮关联的单元格。
  • setText 方法指定与单选按钮关联的文本字符串。它是单选按钮的标签。
  • Checked 属性指定单选按钮是否被选中。
  • setFillFormat 方法指定单选按钮的填充格式。
  • setLineFormat 方法指定选项按钮的线条格式样式。

下面的示例显示了如何向工作表添加单选按钮。该示例添加了代表年龄组的三个单选按钮。在执行代码后,将生成以下输出。

工作表中添加了一些单选按钮

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AddingRadioButtonControl.class) + "DrawingObjects/";
// Create a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet.
Worksheet sheet = workbook.getWorksheets().get(0);
// Get the worksheet cells collection.
Cells cells = sheet.getCells();
// Insert a value.
cells.get("C2").setValue("Age Groups");
Style style = cells.get("B3").getStyle();
style.getFont().setBold(true);
// Set it bold.
cells.get("C2").setStyle(style);
// Add a radio button to the first sheet.
com.aspose.cells.RadioButton radio1 = (com.aspose.cells.RadioButton) sheet.getShapes()
.addShape(MsoDrawingType.RADIO_BUTTON, 3, 0, 1, 0, 20, 100);
// Set its text string.
radio1.setText("20-29");
// Set A1 cell as a linked cell for the radio button.
radio1.setLinkedCell("A1");
// Make the radio button 3-D.
radio1.setShadow(true);
// Set the foreground color of the radio button.
radio1.getFill().setOneColorGradient(Color.getGreen(), 1, GradientStyleType.HORIZONTAL, 1);
// set the line style of the radio button.
radio1.getLine().setDashStyle(MsoLineStyle.THICK_THIN);
// Set the weight of the radio button.
radio1.getLine().setWeight(4);
// Set the line color of the radio button.
radio1.getLine().setOneColorGradient(Color.getBlue(), 1, GradientStyleType.HORIZONTAL, 1);
// Set the dash style of the radio button.
radio1.getLine().setDashStyle(MsoLineDashStyle.SOLID);
// Add another radio button to the first sheet.
com.aspose.cells.RadioButton radio2 = (com.aspose.cells.RadioButton) sheet.getShapes()
.addShape(MsoDrawingType.RADIO_BUTTON, 6, 0, 1, 0, 20, 100);
// Set its text string.
radio2.setText("30-39");
// Set A1 cell as a linked cell for the radio button.
radio2.setLinkedCell("A1");
// Make the radio button 3-D.
radio2.setShadow(true);
// Set the foreground color of the radio button.
radio2.getFill().setOneColorGradient(Color.getGreen(), 1, GradientStyleType.HORIZONTAL, 1);
// set the line style of the radio button.
radio2.getLine().setDashStyle(MsoLineStyle.THICK_THIN);
// Set the weight of the radio button.
radio2.getLine().setWeight(4);
// Set the line color of the radio button.
radio2.getLine().setOneColorGradient(Color.getBlue(), 1, GradientStyleType.HORIZONTAL, 1);
// Set the dash style of the radio button.
radio2.getLine().setDashStyle(MsoLineDashStyle.SOLID);
// Add another radio button to the first sheet.
com.aspose.cells.RadioButton radio3 = (com.aspose.cells.RadioButton) sheet.getShapes()
.addShape(MsoDrawingType.RADIO_BUTTON, 9, 0, 1, 0, 20, 100);
// Set its text string.
radio3.setText("40-49");
// Set A1 cell as a linked cell for the radio button.
radio3.setLinkedCell("A1");
// Make the radio button 3-D.
radio3.setShadow(true);
// Set the foreground color of the radio button.
radio3.getFill().setOneColorGradient(Color.getGreen(), 1, GradientStyleType.HORIZONTAL, 1);
// set the line style of the radio button.
radio3.getLine().setDashStyle(MsoLineStyle.THICK_THIN);
// Set the weight of the radio button.
radio3.getLine().setWeight(4);
// Set the line color of the radio button.
radio3.getLine().setOneColorGradient(Color.getBlue(), 1, GradientStyleType.HORIZONTAL, 1);
// Set the dash style of the radio button.
radio3.getLine().setDashStyle(MsoLineDashStyle.SOLID);
// Save the excel file.
workbook.save(dataDir + "ARBControl_out.xls");

向工作表添加组合框控件

为了更容易进行数据输入,或者将输入限制为您定义的某些项,您可以创建一个组合框或下拉列表,这些有效条目是从工作表上的其他单元格编制而成的。当您为单元格创建下拉列表时,它会在该单元格旁边显示一个箭头。要在该单元格中输入信息,单击箭头,然后单击所需的条目。

使用Microsoft Excel

要在工作表中放置组合框控件,请按照以下步骤执行:

  1. 确保表单工具栏已显示。
  2. 单击 组合框 工具。
  3. 在您的工作表区域,单击并拖动以定义将容纳组合框的矩形。
  4. 一旦组合框放置在工作表中,请右键单击控件,选择设置控件格式并指定输入范围。
  5. 单元格链接字段中,指定应链接到该组合框的单元格地址。
  6. 单击确定

使用Aspose.Cells

ShapeCollection类提供了一个名为addShape的方法,该方法可用于向工作表添加组合框控件。该方法可以返回ComboBox对象。类ComboBox表示组合框,它有一些重要成员:

  • setLinkedCell方法指定与组合框链接的单元格。
  • setInputRange方法指定用于填充组合框的工作表单元范围。
  • setDropDownLines方法指定下拉式组合框部分显示的列表行数。
  • setShadow方法指示组合框是否具有3D阴影。

以下示例显示了如何向工作表添加组合框。执行代码时生成以下输出。

在工作表中添加了一个组合框

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(AddingComboBoxControl.class);
// Create a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet.
Worksheet sheet = workbook.getWorksheets().get(0);
// Get the worksheet cells collection.
Cells cells = sheet.getCells();
// Input a value.
cells.get("B3").setValue("Employee:");
Style style = cells.get("B3").getStyle();
style.getFont().setBold(true);
// Set it bold.
cells.get("B3").setStyle(style);
// Input some values that denote the input range for the combo box.
cells.get("A2").setValue("Emp001");
cells.get("A3").setValue("Emp002");
cells.get("A4").setValue("Emp003");
cells.get("A5").setValue("Emp004");
cells.get("A6").setValue("Emp005");
cells.get("A7").setValue("Emp006");
// Add a new combo box.
com.aspose.cells.ComboBox comboBox = (com.aspose.cells.ComboBox) sheet.getShapes()
.addShape(MsoDrawingType.COMBO_BOX, 3, 0, 1, 0, 20, 100);
// Set the linked cell;
comboBox.setLinkedCell("A1");
// Set the input range.
comboBox.setInputRange("=A2:A7");
// Set no. of list lines displayed in the combo box's list portion.
comboBox.setDropDownLines(5);
// Set the combo box with 3-D shading.
comboBox.setShadow(true);
// AutoFit Columns
sheet.autoFitColumns();
// Saves the file.
workbook.save(dataDir + "tstcombobox.xls");

将标签控件添加到工作表

标签是向用户提供有关电子表格内容的信息的一种方式。Aspose.Cells使得可以在工作表中添加和操作标签。ShapeCollection类提供了一个名为addShape的方法,用于在工作表中添加标签控件。该方法返回一个Label对象。类Label表示工作表中的标签,它有一些重要成员:

  • setText方法指定标签的标题字符串。
  • setPlacement方法指定PlacementType,即标签连接到工作表中的单元格的方式。

以下示例显示了如何向工作表添加一个标签。执行代码时生成以下输出。

在工作表中添加了一个标签

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AddingLabelControl.class) + "DrawingObjects/";
// Create a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet.
Worksheet sheet = workbook.getWorksheets().get(0);
// Add a new label to the worksheet.
com.aspose.cells.Label label = (com.aspose.cells.Label) sheet.getShapes().addShape(MsoDrawingType.LABEL, 2, 2,
2, 0, 60, 120);
// Set the caption of the label.
label.setText("This is a Label");
// Set the Placement Type, the way the label is attached to the cells.
label.setPlacement(PlacementType.FREE_FLOATING);
// Set the fill color of the label.
label.getFill().setOneColorGradient(Color.getYellow(), 1, GradientStyleType.HORIZONTAL, 1);
// Saves the file.
workbook.save(dataDir + "AddingLabelControl_out.xls");

将列表框控件添加到工作表

列表框控件创建一个列表控件,允许选择单个或多个项目。

使用Microsoft Excel

要在工作表中放置列表框控件:

  1. 确保表单工具栏已显示。
  2. 点击列表框工具。
  3. 在工作表区域,单击并拖动以定义将容纳列表框的矩形。
  4. 将列表框放置在工作表中后,右键单击控件,然后点击格式控件,并指定输入范围。
  5. 单元格链接字段中,指定应将该列表框链接到的单元格地址,并设置选择类型(单选,多选,扩展)属性
  6. 点击确定

使用Aspose.Cells

ShapeCollection类提供一个名为addShape的方法,用于向工作表中添加列表框控件。该方法返回一个ListBox对象。ListBox类表示一个列表框。它有一些重要的成员:

  • setLinkedCell方法指定与列表框链接的单元格。
  • setInputRange方法指定用于填充列表框的工作表单元格范围。
  • setSelectionType方法指定列表框的选择模式。
  • setShadow方法指示列表框是否具有3D阴影。

以下示例显示了如何向工作表中添加列表框。执行代码时生成以下输出。

在工作表中添加了一个列表框

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(AddingListBoxControl.class);
// Create a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet.
Worksheet sheet = workbook.getWorksheets().get(0);
// Get the worksheet cells collection.
Cells cells = sheet.getCells();
// Input a value.
cells.get("B3").setValue("Choose Dept:");
Style style = cells.get("B3").getStyle();
style.getFont().setBold(true);
// Set it bold.
cells.get("B3").setStyle(style);
// Input some values that denote the input range for the combo box.
cells.get("A2").setValue("Sales");
cells.get("A3").setValue("Finance");
cells.get("A4").setValue("MIS");
cells.get("A5").setValue("R&D");
cells.get("A6").setValue("Marketing");
cells.get("A7").setValue("HRA");
// Add a new list box.
com.aspose.cells.ListBox listBox = (com.aspose.cells.ListBox) sheet.getShapes()
.addShape(MsoDrawingType.LIST_BOX, 3, 3, 1, 0, 100, 122);
// Set the linked cell;
listBox.setLinkedCell("A1");
// Set the input range.
listBox.setInputRange("=A2:A7");
// Set the Placement Type, the way the list box is attached to the cells.
listBox.setPlacement(PlacementType.FREE_FLOATING);
// Set the list box with 3-D shading.
listBox.setShadow(true);
// Set the selection type.
listBox.setSelectionType(SelectionType.SINGLE);
// AutoFit Columns
sheet.autoFitColumns();
// Saves the file.
workbook.save(dataDir + "tstlistbox.xls");

向工作表添加按钮控件

按钮对于执行某些操作非常有用。有时,将VBA宏分配给按钮或分配超链接以打开网页非常有用。

使用Microsoft Excel

要在工作表中放置按钮控件:

  1. 确保表单工具栏已显示。
  2. 单击按钮工具。
  3. 在工作表区域,单击并拖动以定义将容纳按钮的矩形。
  4. 将按钮放置在工作表中后,右键单击控件并选择格式控件,然后指定VBA宏和相关字体、对齐、大小、边距等属性。
  5. 单击确定

使用Aspose.Cells

ShapeCollection 提供了一个名为 addShape 的方法,用于将按钮控件添加到工作表中。该方法可能会返回一个 Button 对象。Button 类代表一个按钮。它有一些重要的成员:

  • setText 方法指定按钮的标题。
  • setPlacement 方法指定了 PlacementType,即按钮附加到工作表单元格的方式。
  • addHyperlink 方法为按钮控件添加超链接。单击按钮将导航到相关的 URL。

下面的示例显示了如何向工作表中添加一个按钮。在执行代码时生成以下输出

在工作表中添加了一个按钮

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(AddingButtonControl.class);
// Create a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet.
Worksheet sheet = workbook.getWorksheets().get(0);
// Add a new button to the worksheet.
com.aspose.cells.Button button = (com.aspose.cells.Button) sheet.getShapes().addShape(MsoDrawingType.BUTTON, 2,
2, 2, 0, 20, 80);
// Set the caption of the button.
button.setText("Aspose");
// Set the Placement Type, the way the button is attached to the cells.
button.setPlacement(PlacementType.FREE_FLOATING);
// Set the font name.
button.getFont().setName("Tahoma");
// Set the caption string bold.
button.getFont().setBold(true);
// Set the color to blue.
button.getFont().setColor(Color.getBlue());
// Set the hyperlink for the button.
button.addHyperlink("http://www.aspose.com/");
// Saves the file.
workbook.save(dataDir + "tstbutton.xls");

向工作表添加线控件

Aspose.Cells 允许您在工作表中绘制自选图形。您可以轻松地创建一条线,并允许您格式化该线。例如,您可以更改线的颜色、指定线的粗细和样式。

使用Microsoft Excel

  1. 绘图 工具栏上,单击 自选图形,指向 线条,然后选择所需的线条样式。
  2. 拖动以绘制线条。
  3. 执行以下操作中的一个或两个:
    1. 要限制线条以与起点呈15度角的方式绘制,请在拖动时按住 SHIFT 键。
    2. 要使线条从第一个端点向相反方向延伸,请在拖动时按住 CTRL 键。

使用Aspose.Cells

ShapeCollection 提供了一个名为 addShape 的方法,用于向工作表中添加一条线形状。该方法可能会返回一个 LineShape 对象。LineShape 类代表一条线。它有一些重要的成员:

  • setDashStyle 方法指定了线条的格式。
  • setPlacement 方法指定了 PlacementType,即线条附加到工作表单元格的方式。

下面的示例显示了如何向工作表中添加线条。它创建了三条不同样式的线。在执行代码后生成以下输出

工作表中添加了几条线

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AddingLineControl.class) + "DrawingObjects/";
//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//Get the first worksheet in the book.
Worksheet worksheet = workbook.getWorksheets().get(0);
//Add a new line to the worksheet.
LineShape line1 = (LineShape)worksheet.getShapes().addShape(MsoDrawingType.LINE,5, 1,0,0, 0, 250);
line1.setHasLine(true);
//Set the line dash style
LineFormat shapeline = line1.getLine();
shapeline.setDashStyle(MsoLineDashStyle.SOLID);
//Set the placement.
line1.setPlacement(PlacementType.FREE_FLOATING);
//Add another line to the worksheet.
LineShape line2 = (LineShape) worksheet.getShapes().addShape(MsoDrawingType.LINE, 7, 1,0,0, 85, 250);
line2.setHasLine(true);
//Set the line dash style.
shapeline = line2.getLine();
shapeline.setDashStyle(MsoLineDashStyle.DASH_LONG_DASH);
shapeline.setWeight(4);
//Set the placement.
line2.setPlacement(PlacementType.FREE_FLOATING);
//Add the third line to the worksheet.
LineShape line3 = (LineShape)worksheet.getShapes().addShape(MsoDrawingType.LINE, 13, 1,0,0, 0, 250);
line3.setHasLine(true);
//Set the line dash style
shapeline = line1.getLine();
shapeline.setDashStyle(MsoLineDashStyle.SOLID);
//Set the placement.
line3.setPlacement(PlacementType.FREE_FLOATING);
//Save the excel file.
workbook.save(dataDir + "tstlines.xls");

向一条线条中添加箭头

Aspose.Cells 还允许您绘制箭头线条。您可以向一条线条添加箭头,并格式化该线。例如,您可以更改线条的颜色,或指定线条的粗细和样式。

下面的示例显示了如何向一条线添加箭头。在执行代码时生成以下输出。

在工作表中添加了带箭头的直线

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AddinganArrowHead.class) + "DrawingObjects/";
//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//Get the first worksheet in the book.
Worksheet worksheet = workbook.getWorksheets().get(0);
//Add a line to the worksheet
LineShape line2 = (LineShape) worksheet.getShapes().addShape(MsoDrawingType.LINE, 7, 0, 1, 0, 85, 250);
//Set the line color
line2.getLine().setFillType(FillType.SOLID);
line2.getLine().getSolidFill().setColor(Color.getRed());
//Set the weight of the line.
line2.getLine().setWeight(3);
//Set the placement.
line2.setPlacement(PlacementType.FREE_FLOATING);
//Set the line arrows.
line2.getLine().setEndArrowheadWidth(MsoArrowheadWidth.MEDIUM);
line2.getLine().setEndArrowheadStyle(MsoArrowheadStyle.ARROW);
line2.getLine().setEndArrowheadLength(MsoArrowheadLength.MEDIUM);
line2.getLine().setBeginArrowheadStyle(MsoArrowheadStyle.ARROW_DIAMOND);
line2.getLine().setBeginArrowheadLength(MsoArrowheadLength.MEDIUM);
//Make the gridlines invisible in the first worksheet.
workbook.getWorksheets().get(0).setGridlinesVisible(false);
//Save the excel file.
workbook.save(dataDir + "AddinganArrowHead_out.xlsx");

在工作表中添加矩形控件

Aspose.Cells允许您在工作表中绘制矩形形状。您可以创建矩形、正方形等形状,还可以格式化控件的填充颜色和边框线颜色。例如,您可以更改矩形的颜色,设置阴影颜色,指定矩形的重量和样式以满足您的需求。

使用Microsoft Excel

  1. 绘图工具栏上,单击矩形
  2. 拖动绘制矩形。
  3. 执行以下操作中的一个或两个:
    1. 若要从起点绘制正方形并约束矩形,请按住SHIFT键并拖动。
    2. 若要从中心点绘制矩形,请按住CTRL键并拖动。

使用Aspose.Cells

ShapeCollection类提供了一个名为addShape的方法,用于向工作表添加矩形形状。该方法可以返回一个RectangleShape对象。RectangleShape类表示矩形形状。它具有一些重要成员:

  • setLineFormat方法指定矩形的线条格式属性。
  • setPlacement方法指定PlacementType,即矩形附加到工作表单元格的方式。
  • FillFormat属性指定矩形的填充格式样式。

以下示例展示了如何向工作表添加矩形。执行代码时会生成以下输出。

工作表中添加了一个矩形

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AddingRectangleControl.class) + "DrawingObjects/";
// Instantiate a new Workbook.
Workbook excelbook = new Workbook();
// Add a rectangle control.
com.aspose.cells.RectangleShape rectangle = (com.aspose.cells.RectangleShape) excelbook.getWorksheets().get(0)
.getShapes().addShape(MsoDrawingType.RECTANGLE, 3, 2, 0, 0, 70, 130);
// Set the placement of the rectangle.
rectangle.setPlacement(PlacementType.FREE_FLOATING);
// Set the fill format.
FillFormat fillformat = rectangle.getFill();
fillformat.setOneColorGradient(Color.getOlive(), 1, GradientStyleType.HORIZONTAL, 1);
// Set the line style.
LineFormat linestyle = rectangle.getLine();
linestyle.setDashStyle(MsoLineStyle.THICK_THIN);
// Set the line weight.
linestyle.setWeight(4);
// Set the color of the line.
linestyle.setOneColorGradient(Color.getBlue(), 1, GradientStyleType.HORIZONTAL, 1);
// Set the dash style of the rectangle.
linestyle.setDashStyle(MsoLineDashStyle.SOLID);
// Save the excel file.
excelbook.save(dataDir + "AddingRectangleControl_out.xls");

向工作表添加弧形控件

Aspose.Cells允许您在工作表中绘制弧形。您可以创建简单的填充弧形。您可以格式化控件的填充颜色和边框线颜色。例如,您可以指定/更改弧形的颜色,设置阴影颜色,指定形状的重量和样式以满足您的需求。

使用Microsoft Excel

  1. 绘图工具栏上,单击AutoShapes中的弧形
  2. 拖动绘制弧形。

使用Aspose.Cells

ShapeCollection类提供了一个名为addShape的方法,用于向工作表添加弧形。该方法可以返回一个ArcShape对象。ArcShape类表示弧形。它具有一些重要成员:

  • setLineFormat方法指定弧形形状的线条格式属性。
  • setPlacement方法指定PlacementType,弧形附加到工作表单元格的方式。
  • FillFormat属性指定了形状的填充格式样式。

以下示例展示了如何向工作表添加弧形。示例创建了两个弧形:一个是填充的,另一个是简单的。执行代码时会生成以下输出。

工作表中添加了两个弧形

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AddingArcControl.class) + "DrawingObjects/";
// Instantiate a new Workbook.
Workbook excelbook = new Workbook();
// Add an arc shape.
com.aspose.cells.ArcShape arc1 = (com.aspose.cells.ArcShape) excelbook.getWorksheets().get(0).getShapes()
.addShape(MsoDrawingType.ARC, 2, 2, 0, 0, 130, 130);
// Set the placement of the arc.
arc1.setPlacement(PlacementType.FREE_FLOATING);
// Set the fill format.
FillFormat fillformat = arc1.getFill();// getFillFormat();
fillformat.setOneColorGradient(Color.getLime(), 1, GradientStyleType.HORIZONTAL, 1);
//setForeColor(Color.getBlue());
// Set the line style.
LineFormat lineformat = arc1.getLine();// getLineFormat();
lineformat.setDashStyle(MsoLineStyle.SINGLE); //setStyle(MsoLineStyle.SINGLE);
// Set the line weight.
lineformat.setWeight(1);
// Set the color of the arc line.
lineformat.setOneColorGradient(Color.getLime(), 1, GradientStyleType.HORIZONTAL, 1); //setForeColor(Color.getBlue());
// Set the dash style of the arc.
lineformat.setDashStyle(MsoLineDashStyle.SOLID);
// Add another arc shape.
com.aspose.cells.ArcShape arc2 = (com.aspose.cells.ArcShape) excelbook.getWorksheets().get(0).getShapes()
.addShape(MsoDrawingType.ARC, 9, 2, 0, 0, 130, 130);
// Set the placement of the arc.
arc2.setPlacement(PlacementType.FREE_FLOATING);
// Set the line style.
LineFormat lineformat1 = arc2.getLine(); //getLineFormat();
lineformat1.setDashStyle(MsoLineStyle.SINGLE);
// Set the line weight.
lineformat1.setWeight(1);
// Set the color of the arc line.
lineformat1.setOneColorGradient(Color.getLime(), 1, GradientStyleType.HORIZONTAL, 1);//setForeColor(Color.getBlue());
// Set the dash style of the arc.
lineformat1.setDashStyle(MsoLineDashStyle.SOLID);
// Save the excel file.
excelbook.save(dataDir + "AddingArcControl_out.xls");

向工作表添加椭圆控件

Aspose.Cells允许您在工作表中绘制椭圆形状。创建简单和填充的椭圆形状,并格式化控件的填充颜色和边框线颜色。例如,您可以指定/更改椭圆的颜色,设置阴影颜色,指定形状的宽度和样式。

使用Microsoft Excel

  1. 绘图工具栏上,单击椭圆
  2. 拖动以绘制椭圆。
  3. 执行以下操作中的一个或两个:
    1. 要将椭圆约束为从其起始点绘制圆,请在拖动时按住SHIFT。
    2. 要从中心点绘制椭圆,请在拖动时按住CTRL。

使用Aspose.Cells

ShapeCollection类提供了一个名为addShape的方法,用于向工作表添加椭圆形状。该方法可能返回一个椭圆对象。椭圆类表示椭圆形状。它具有一些重要成员:

  • setLineFormat方法指定椭圆形状的线格式属性。
  • setPlacement方法指定了PlacementType,即椭圆附加到工作表中的单元格的方式。
  • FillFormat属性指定了形状的填充格式样式。

以下示例显示如何向工作表添加椭圆形状。该示例创建了两个椭圆形状:一个是填充的椭圆,另一个是简单的圆。执行代码时生成以下输出。

工作表中添加了两个椭圆形状

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AddinganOvalControl.class) + "DrawingObjects/";
// Instantiate a new Workbook.
Workbook excelbook = new Workbook();
// Add an oval shape.
Oval oval1 = (com.aspose.cells.Oval) excelbook.getWorksheets().get(0).getShapes().addShape(MsoDrawingType.OVAL,
2, 2, 0, 0, 130, 130);
// Set the placement of the oval.
oval1.setPlacement(PlacementType.FREE_FLOATING);
// Set the fill format.
FillFormat fillformat = oval1.getFill();// getFillFormat();
fillformat.setOneColorGradient(Color.getNavy(), 1, GradientStyleType.HORIZONTAL, 1);
// Set the line style.
LineFormat lineformat = oval1.getLine();// getLineFormat();
lineformat.setDashStyle(MsoLineStyle.SINGLE); //setStyle(MsoLineStyle.SINGLE);
// Set the line weight.
lineformat.setWeight(1);
// Set the color of the oval line.
lineformat.setOneColorGradient(Color.getGreen(), 1, GradientStyleType.HORIZONTAL, 1);
// Set the dash style of the oval.
lineformat.setDashStyle(MsoLineDashStyle.SOLID);
// Add another arc shape.
Oval oval2 = (com.aspose.cells.Oval) excelbook.getWorksheets().get(0).getShapes().addShape(MsoDrawingType.OVAL,
9, 2, 0, 0, 130, 130);
// Set the placement of the oval.
oval2.setPlacement(PlacementType.FREE_FLOATING);
// Set the line style.
LineFormat lineformat1 = oval2.getLine();
lineformat.setDashStyle(MsoLineStyle.SINGLE); //setStyle(MsoLineStyle.SINGLE);
// Set the line weight.
lineformat1.setWeight(1);
// Set the color of the oval line.
lineformat1.setOneColorGradient(Color.getBlue(),1, GradientStyleType.HORIZONTAL, 1);
// Set the dash style of the oval.
lineformat1.setDashStyle(MsoLineDashStyle.SOLID);
// Save the excel file.
excelbook.save(dataDir + "AddinganOvalControl_out.xls");

高级主题