Developers can add different other drawing objects such as text box, check box, radio button, combo box, label, button, line, rectangle, arc, oval, spinner, scroll bar, group box etc. Aspose.Cells supports to create and access these drawing objects at runtime. However, there are a few drawing objects which are not supported yet, you can create these drawing objects in a designer spreadsheet using Microsoft Excel and then import your designer spreadsheet to Aspose.Cells. Aspose.Cells allows you to load these drawing objects from a designer spreadsheet and write them to a generated file.
Adding TextBox Control to the Worksheet
One way to stress important information on your report is to use a text box. For example, you can enter text to highlight your company's name or to indicate the geographic region with the highest sales etc. Aspose.Cells provides TextBoxes class, which is used to add a new text box to the collection. There is another class TextBox, which represents a text box used to define all types of settings. It has some important members:
1. TextFrame...(Property) returns a MsoTextFrame object used to adjust the contents of the text box.
2. Placement...(Property) specifies the placement type.
3. Font...(Property) specifies the font attributes.
4. AddHyperlink...(Method) adds a hyperlink for the text box.
5. FillFormat...(Property) returns MsoFillFormat object used to set the fill format for the text box.
6. LineFormat...(Property) returns the MsoLineFormat object usually used to style and weight of the text box line.
7. Text...(Property) specifies the input text for the text box.
Example:
The following example creates two textboxes in the first worksheet of the workbook. The first text box is well-furnished with differnt format settings. The second is a simple one.
[C#]
//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.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.
MsoFillFormat fillformat = textbox0.FillFormat;
//Set the fillcolor.
fillformat.ForeColor = Color.Silver;
//Get the lineformat type of the textbox.
MsoLineFormat lineformat = textbox0.LineFormat;
//Set the line style.
lineformat.Style = MsoLineStyle.ThinThick;
//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.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("d:\\test\\tsttextboxes.xls");
[VB]
'Instantiate a new Workbook.
Dim workbook As Workbook = New Workbook()
'Get the first worksheet in the book.
Dim worksheet As Worksheet = workbook.Worksheets(0)
'Add a new textbox to the collection.
Dim textboxIndex As Integer = worksheet.TextBoxes.Add(2, 1, 160, 200)
'Get the textbox object.
Dim textbox0 As Aspose.Cells.TextBox = worksheet.TextBoxes(textboxIndex)
'Fill the text.
textbox0.Text = "ASPOSE______The .NET & JAVA Component Publisher!"
'Get the textbox text frame.
Dim textframe0 As MsoTextFrame = textbox0.TextFame
'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.
Dim fillformat As MsoFillFormat = textbox0.FillFormat
'Set the fillcolor.
fillformat.ForeColor = Color.Silver
'Get the lineformat type of the textbox.
Dim lineformat As MsoLineFormat = textbox0.LineFormat
'Set the line style.
lineformat.Style = MsoLineStyle.ThinThick
'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.
Dim textbox1 As Aspose.Cells.TextBox = 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("d:\test\tsttextboxes.xls")
[JAVA]
//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//Get the first worksheet in the book.
Worksheet worksheet = workbook.getWorksheets().getSheet(0);
//Add a new textbox to the collection.
int textboxIndex = worksheet.getTextBoxes().add(2, 1,200, 160);
//Get the textbox object.
com.aspose.cells.TextBox textbox0 = worksheet.getTextBoxes().get(textboxIndex);
//Fill the text.
textbox0.setContent("ASPOSE______The .NET & JAVA Component Publisher!");
//Set the placement.
textbox0.setPlacement(PlacementType.FREE_FLOATING);
//Set the font color.
textbox0.getFont().setColor(Color.BLUE);
//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.
ShapeFill fillformat = textbox0.getFill();
//Set the fillcolor.
fillformat.setColor(Color.SILVER);
//Get the lineformat type of the textbox.
ShapeLine lineformat = textbox0.getLine();
//Set the line style.
lineformat.setStyle(MsoLineStyle.THIN_THICK);
//Set the line weight.
lineformat.setWeight(6);
//Set the dash style to squaredot.
lineformat.setDashStyle(MsoLineDashStyle.SQUARE_DOT);
//Add another textbox.
textboxIndex = worksheet.getTextBoxes().add(15, 4, 120, 85);
//Get the second textbox.
com.aspose.cells.TextBox textbox1 = worksheet.getTextBoxes().get(textboxIndex);
//Input some text to it.
textbox1.setContent("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("d:\\test\\tsttextboxes.xls");
The following output would be generated after executing the above code:
|
Figure: Two TextBoxes are created in the worksheet.
|
Manipulating TextBox Controls in the Designer Spreadsheets
Aspose.Cells also facilitates you to access the textboxes in the desinger worksheets and manipulate them. You may utilize Worksheet.TextBoxes property to get the textboxes collection in the sheet.
Example:
Following example uses the existing excel file "tsttextboxes.xls" which we created earlier in the above example. It gets the text strings of the two textboxes and changes the text of the second textbox to save the file.
[C#]
//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//Open the existing excel file.
workbook.Open("d:\\test\\tsttextboxes.xls");
//Get the first worksheet in the book.
Worksheet worksheet = workbook.Worksheets[0];
//Get the first textbox object.
Aspose.Cells.TextBox textbox0 = worksheet.TextBoxes[0];
//Obtain the text in the first textbox.
string text0 = textbox0.Text;
//Get the second textbox object.
Aspose.Cells.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("d:\\test\\tsttextboxes1.xls");
[VB]
'Instantiate a new Workbook.
Dim workbook As Workbook = New Workbook()
'Open the existing excel file.
workbook.Open("d:\test\tsttextboxes.xls")
'Get the first worksheet in the book.
Dim worksheet As Worksheet = workbook.Worksheets(0)
'Get the first textbox object.
Dim textbox0 As Aspose.Cells.TextBox = worksheet.TextBoxes(0)
'Obtain the text in the first textbox.
Dim text0 As String = textbox0.Text
'Get the second textbox object.
Dim textbox1 As Aspose.Cells.TextBox = worksheet.TextBoxes(1)
'Obtain the text in the second textbox.
Dim text1 As String = textbox1.Text
'Change the text of the second textbox.
textbox1.Text = "This is an alternative text"
'Save the excel file.
workbook.Save("d:\test\tsttextboxes1.xls")
[JAVA]
//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//Open the existing excel file.
workbook.open("d:\\test\\tsttextboxes.xls");
//Get the first worksheet in the book.
Worksheet worksheet = workbook.getWorksheets().getSheet(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.getContent();
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.getContent();
//Change the text of the second textbox.
textbox1.setContent("This is an alternative text");
//Save the excel file.
workbook.save("d:\\test\\tsttextboxes1.xls");
Adding CheckBox Control to the Worksheet
CheckBoxes are handy if you want to provide a way for a user to choose between two options, such as true or false; yes or no. Aspose.Cells allows you to use check boxes in your worksheets, if desired.For instance, you may have developed a financial projection worksheet in which you can either account for a particular acquisition or not. In this case, you might want to place a check box at the top of the worksheet. You can then link the status of this check box to another cell, so that if the check box is selected, the value of the cell is True; if it is not selected, the value of the cell is False.
Using MS Excel
To place a check box control in your worksheet, follow these steps:
1. Make sure the Forms toolbar is displayed.
2. Click on the Check Box tool on the Forms toolbar.
3. In your worksheet area, click and drag to define the rectangle that will hold the check box and the label beside the check box.
4. Once the check box is placed in the worksheet, you can move the mouse cursor into the label area and change the label to anything desired.
5. In the Cell Link field, specify the address of the cell to which this check box should be linked.
6. Click on OK.
Using Aspose.Cells
Aspose.Cells provides CheckBoxes class, which is used to add a new check box to the collection. There is another class CheckBox, which represents a check box. It has some important members:
1. LinkedCell...(Property) specifies a cell which is linked to the check box.
2. Text...(Property) specifies the text string associated with the check box. It is the label of the check box.
3. Value...(Property) specifies if the check box is checked or not.
Example:
The following example shows how to add a checkbox to the worksheet.
[C#]
//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.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("d:\\test\\tstcheckboxes.xls");
[VB]
'Instantiate a new Workbook.
Dim excelbook As Workbook = New Workbook()
'Add a checkbox to the first worksheet in the workbook.
Dim index As Integer = excelbook.Worksheets(0).CheckBoxes.Add(5, 5, 100, 120)
'Get the checkbox object.
Dim checkbox As Aspose.Cells.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("d:\test\tstcheckboxes.xls")
The following output would be generated after executing the above code: