管理控制

介绍

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

将 TextBox 控件添加到工作表

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

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

通过执行代码生成以下输出:

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

待办事项:图片_替代_文本

// 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");

在 Designer 电子表格中操作文本框控件

Aspose.Cells 还允许您访问设计器工作表中的文本框并对其进行操作。使用工作表.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");

将 CheckBox 控件添加到工作表

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

使用 Microsoft Excel

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

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

使用 Aspose.Cells

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

  • 设置链接单元格方法指定链接到复选框的单元格。
  • 设置文本方法指定与复选框关联的文本字符串。它是复选框的标签。
  • 设定值方法指定复选框是否被选中。

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

工作表中添加了一个 CheckBox

待办事项:图片_替代_文本

// 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");

将 RadioButton 控件添加到工作表

单选按钮或选项按钮是由圆形框组成的控件。用户通过选择圆形框来做出他或她的决定。单选按钮通常(如果不是总是)伴随其他按钮。这样的单选按钮作为一个组出现和运行。用户通过仅选择其中一个按钮来决定哪个按钮有效。当用户单击一个按钮时,它就会被填充。当组中的一个按钮被选中时,同一组按钮为空。

使用 Microsoft Excel

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

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

使用 Aspose.Cells

形状集合类提供了一个名为 addShape 的方法,可用于将单选按钮控件添加到工作表。该方法可能会返回一个 RadioButton 对象。 RadioButton 类表示一个选项按钮。它有一些重要的成员:

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

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

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

待办事项:图片_替代_文本

// 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. 在里面Cell 链接字段,指定此组合框应链接到的单元格的地址。
  6. 点击好的.

使用 Aspose.Cells

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

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

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

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

待办事项:图片_替代_文本

// 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 可以在工作表中添加和操作标签。这形状集合类提供了一个名为addShape 的方法,用于向工作表添加标签控件。该方法返回一个标签对象。 Label 类表示工作表中的标签。它有一些重要的成员:

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

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

工作表中添加了标签

待办事项:图片_替代_文本

// 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. 在里面Cell 链接字段,指定此列表框应链接到的单元格的地址并设置选择类型(单、多、扩展)属性
  6. 点击好的.

使用 Aspose.Cells

形状集合类提供了一个名为 addShape 的方法,用于将列表框控件添加到工作表中。该方法返回一个 ListBox 对象。类 ListBox 代表一个列表框。它有一些重要的成员:

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

下面的示例演示如何将列表框添加到工作表。执行代码时会生成以下输出。

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

待办事项:图片_替代_文本

// 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

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

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

以下示例显示如何向工作表添加按钮。执行代码时产生如下输出

工作表中添加了一个按钮

待办事项:图片_替代_文本

// 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. 执行以下一项或两项操作:
  4. 要限制线条以与其起点成 15 度角绘制,请在拖动时按住 SHIFT。
  5. 要从第一个端点沿相反方向延长线,请在拖动时按住 CTRL。

使用 Aspose.Cells

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

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

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

在工作表中添加几行

待办事项:图片_替代_文本

// 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 还允许您绘制箭头线。可以将箭头添加到一行,并格式化该行。例如,您可以更改线条的颜色,或指定线条的粗细和样式。

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

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

待办事项:图片_替代_文本

// 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. 执行以下一项或两项操作:
  4. 要限制矩形从其起点绘制正方形,请在拖动时按住 SHIFT。
  5. 要从中心点绘制矩形,请在拖动时按住 CTRL。

使用 Aspose.Cells

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

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

下面的示例演示如何将矩形添加到工作表。执行代码时会生成以下输出。

工作表中添加了一个矩形

待办事项:图片_替代_文本

// 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. 画画工具栏,单击在里面自选图形.
  2. 拖动以绘制圆弧。

使用 Aspose.Cells

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

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

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

工作表中添加了两个弧形

待办事项:图片_替代_文本

// 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. 执行以下一项或两项操作:
  4. 要限制椭圆从其起点绘制圆,请在拖动时按住 SHIFT。
  5. 要从中心点绘制椭圆,请在拖动时按住 CTRL。

使用 Aspose.Cells

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

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

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

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

待办事项:图片_替代_文本

// 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");

推进主题