コントロールの管理
紹介
開発者はテキストボックス、チェックボックス、ラジオボタン、コンボボックス、ラベル、ボタン、ライン、四角形、円弧、楕円、スピンボタン、スクロールバー、グループボックスなどの異なる図形オブジェクトを追加できます。Aspose.Cellsには、すべての図形オブジェクトを含むAspose.Cells.Drawing名前空間が提供されています。ただし、まだサポートされていないいくつかの図形オブジェクトや形状があります。これらの図形オブジェクトはMicrosoft Excelでデザインされたスプレッドシートに作成し、その後Aspose.Cellsに読み込むことができます。Aspose.Cellsは、デザイナースプレッドシートからこれらの図形オブジェクトを読み込み、生成されたファイルに書き込むことができます。
ワークシートにテキストボックスコントロールを追加する
報告書で重要な情報を強調する1つの方法は、テキストボックスを使用することです。たとえば、企業名を強調するためのテキストを追加したり、売上高が最も高い地域を示すためにテキストを追加することができます。Aspose.Cells にはテキストボックスコントロールを追加するための TextBoxes クラスが用意されています。また、その他の設定を定義するために使用するテキストボックスを表すクラス TextBox もあります。いくつか重要なメンバーを持っています:
- getTextFrame メソッドは MsoTextFrame オブジェクトを返し、テキストボックスの内容を調整するのに使用されます。
- setPlacement メソッドは配置のタイプを指定します。
- setFont メソッドはフォント属性を指定します。
- addHyperlinkメソッドは、テキストボックスにハイパーリンクを追加します。
- FillFormat プロパティはテキストボックスの塗りつぶし形式を設定するために使用する MsoFillFormat オブジェクトを返します。
- LineFormatプロパティは、テキストボックスのラインのスタイルと太さを通常設定するために使用されるMsoLineFormatオブジェクトを返します。
- setText メソッドはテキストボックスの入力テキストを指定します。
次の例では、ブックの最初のワークシートに2つのテキストボックスを作成します。最初のテキストボックスはさまざまなフォーマット設定で整備されています。2番目はシンプルなものです。
上記のコードを実行した結果は次のとおりです:
ワークシートに2つのテキストボックスが作成されます
// 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 - を使用します。2つのテキストボックスのテキスト文字列を取得し、2番目のテキストボックスのテキストを変更してファイルを保存します。
// 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"); |
ワークシートにチェックボックスコントロールを追加する
チェックボックスは、ユーザーが真偽またははい・いいえなどの2つのオプションから選択する方法を提供したい場合に便利です。Aspose.Cellsを使用すると、ワークシートでチェックボックスを使用できます。たとえば、特定の取得を考慮するかどうかを伴う財務予測ワークシートを作成した場合、そのワークシートの上部にチェックボックスを配置したいと思うかもしれません。その後、このチェックボックスの状態を他のセルにリンクすることができます。つまり、チェックボックスが選択されている場合、セルの値はTrueになり、選択されていない場合、セルの値はFalseになります。
Microsoft Excel の使用
ワークシートにチェックボックスコントロールを配置するには、次の手順に従います。
- フォームツールバーが表示されていることを確認します。
- フォームツールバーのチェックボックスツールをクリックします。
- ワークシートエリアで、チェックボックスとチェックボックスの横に表示されるラベルを定義するためにクリックしてドラッグします。
- チェックボックスが配置されたら、マウスカーソルをラベル領域に移動してラベルを変更します。
- セルリンクフィールドで、このチェックボックスをリンクするセルのアドレスを指定します。
- OK をクリックします。
Aspose.Cellsの使用
Aspose.Cellsには新しいチェックボックスをコレクションに追加するために使用されるCheckBoxCollectionクラスが提供されています。重要なメンバーを持つ他のクラスAspose.Cells.Drawing.CheckBoxもあります。
- setLinkedCellメソッドは、チェックボックスにリンクされたセルを指定します。
- setTextメソッドは、チェックボックスに関連付けられるテキスト文字列を指定します。これはチェックボックスのラベルです。
- setValueメソッドは、チェックボックスがチェックされているかどうかを指定します。
以下の例は、ワークシートにチェックボックスを追加する方法を示しています。以下の出力は、コードの実行後に生成されます。
ワークシートに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コントロールを追加する
ラジオボタン、またはオプションボタンは、円形のボックスからなるコントロールです。ユーザーはラウンドボックスを選択することで決定を行います。ラジオボタンは通常、他のラジオボタンに伴って表示され、グループとして振る舞います。ユーザーは1つのボタンのみを選択することで、どのボタンが有効か決定します。ユーザーが1つのボタンをクリックすると、それは選択されます。グループ内の1つのボタンが選択されると、同じグループのボタンは空になります。
Microsoft Excel の使用
ワークシートにラジオボタンコントロールを配置するには、次の手順に従います。
- フォーム ツールバーが表示されていることを確認します。
- オプションボタンツールをクリックします。
- ワークシートで、オプションボタンとオプションボタンの横に表示されるラベルを定義するためにクリックしてドラッグします。
- ワークシートにラジオボタンを配置したら、マウスカーソルをラベル領域に移動してラベルを変更します。
- セルリンクフィールドで、このラジオボタンがリンクされるセルのアドレスを指定します。
- OK をクリックします。
Aspose.Cellsの使用
ShapeCollectionクラスは、ワークシートにラジオボタンコントロールを追加するために使用できるaddShapeというメソッドを提供します。このメソッドはRadioButtonオブジェクトを返すことがあります。RadioButtonクラスは、オプションボタンを表します。いくつかの重要なメンバーがあります:
- setLinkedCellメソッドは、ラジオボタンにリンクされたセルを指定します。
- setTextメソッドは、ラジオボタンに関連するテキスト文字列を指定します。これはラジオボタンのラベルです。
- Checkedプロパティは、ラジオボタンがチェックされているかどうかを指定します。
- setFillFormatメソッドは、ラジオボタンの塗りつぶし形式を指定します。
- setLineFormatメソッドは、オプションボタンのライン形式スタイルを指定します。
以下の例は、ワークシートにラジオボタンを追加する方法を示しています。この例では、年齢層を表す3つのラジオボタンが追加されます。下記の出力は、コードの実行後に生成されます。
ワークシートにいくつかのラジオボタンが追加されました
ワークシートにコンボボックスコントロールを追加する
データの入力を容易にするか、定義した項目にエントリを制限するためには、ワークシートの他のセルからコンパイルされる有効なエントリのコンボボックス、またはドロップダウンリストを作成できます。セルのドロップダウンリストを作成すると、そのセルの隣に矢印が表示されます。そのセルに情報を入力するには、矢印をクリックし、その後、欲しいエントリをクリックします。
Microsoft Excel の使用
ワークシートにコンボボックスコントロールを配置するには、次の手順に従います:
- フォーム ツールバーが表示されていることを確認します。
- コンボボックス ツールをクリックします。
- ワークシートエリアで、コンボボックスを保持する四角形を定義するためにクリックしてドラッグします。
- コンボボックスがワークシートに配置されたら、コントロールを右クリックして コントロールの書式設定 をクリックし、入力範囲を指定します。
- セルリンク フィールドで、このコンボボックスをリンクするセルのアドレスを指定します。
- OK をクリックします。
Aspose.Cellsの使用
ShapeCollection クラスは、ワークシートにコンボボックスコントロールを追加するための 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 では、ワークシートにラベルを追加および操作することができます。ShapeCollection クラスは、ワークシートにラベルコントロールを追加するための addShape というメソッドを提供します。メソッドは Label オブジェクトを返します。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 の使用
ワークシートにリストボックスコントロールを配置するには:
- フォーム ツールバーが表示されていることを確認します。
- リストボックス ツールをクリックします。
- ワークシートエリアで、リストボックスを保持する四角形を定義するためにクリックしてドラッグします。
- リストボックスがワークシートに配置されたら、コントロールを右クリックして コントロールの書式設定 をクリックし、入力範囲を指定します。
- セルリンク フィールドで、このリストボックスをリンクするセルのアドレスを指定し、選択タイプ(単一、複数、拡張)属性を設定します。
- OK をクリックします。
Aspose.Cellsの使用
ShapeCollection クラスは、ワークシートにリストボックスコントロールを追加するための 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マクロを割り当てたり、Webページを開くためのハイパーリンクを割り当てることも有用です。
Microsoft Excel の使用
ボタンコントロールをワークシートに配置するには:
- フォーム ツールバーが表示されていることを確認します。
- ボタン ツールをクリックします。
- ワークシート領域でクリックしてドラッグして、ボタンを配置する矩形を定義します。
- リストボックスがワークシートに配置されたら、コントロールを右クリックして フォーマットコントロール を選択し、VBAマクロを指定し、フォント、配置、サイズ、余白などに関連する属性を設定します。
- OK をクリックします。
Aspose.Cellsの使用
ShapeCollection クラスには、ボタンコントロールをワークシートに追加するためのaddShapeというメソッドが用意されています。このメソッドは、ボタンオブジェクトを返すことがあります。Buttonクラスはボタンを表します。いくつか重要なメンバがあります:
- setTextメソッドはボタンのキャプションを指定します。
- setPlacementメソッドは、ボタンがワークシートのセルに接続される方法を指定します。
- addHyperlinkメソッドは、ボタンコントロールにハイパーリンクを追加します。ボタンをクリックすると、関連する URL に移動します。
次の例では、ワークシートにボタンを追加する方法を示しています。コードを実行すると、次の出力が生成されます
ワークシートにボタンが追加されます
ワークシートにラインコントロールを追加する
Aspose.Cellsを使用すると、ワークシートに自動形状を描画することができます。線を簡単に作成できます。また、線の書式を指定することもできます。例えば、線の色を変更したり、線の太さやスタイルを指定することができます。
Microsoft Excel の使用
- 描画 ツールバーで オートシェイプ をクリックし、ライン を指して、希望のラインスタイルを選択します。
- ドラッグしてラインを描きます。
- 次のいずれかを行います。
- ラインを開始点から15度の角度で制限するには、ドラッグしながら SHIFT キーを押します。
- 最初の端点から異なる方向にラインを長くするには、ドラッグしながら CTRL キーを押します。
Aspose.Cellsの使用
ShapeCollection クラスには、ワークシートにライン形状を追加するために使用されるaddShapeというメソッドが用意されています。このメソッドは、LineShapeオブジェクトを返すことがあります。LineShapeクラスはラインを表します。いくつか重要なメンバがあります:
- setDashStyleメソッドは、ラインの形式を指定します。
- setPlacementメソッドは、ラインがワークシートのセルに接続される方法を指定します。
次の例では、ワークシートにラインを追加する方法を示しています。異なるスタイルの3本のラインを作成します。コードを実行すると、次の出力が生成されます。
ワークシートに数行が追加されます
// 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 の使用
- 描画ツールバーで、長方形をクリックします。
- 長方形を描画するには、ドラッグします。
- 次のいずれかを行います。
- 長方形を開始点から正方形に描画するには、ドラッグしながらSHIFTキーを押し続けます。
- 長方形を中心点から描画するには、ドラッグしながらCTRLキーを押し続けます。
Aspose.Cellsの使用
ShapeCollectionクラスは、長方形形状をワークシートに追加するために使用される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 の使用
- 図形の自動スタイルで、円弧をクリックします。
- 円弧を描画するには、ドラッグします。
Aspose.Cellsの使用
ShapeCollectionクラスは、ワークシートに円弧形状を追加するために使用されるaddShapeというメソッドを提供します。このメソッドはArcShapeオブジェクトを返すことができます。ArcShapeクラスは円弧を表します。重要なメンバーを持っており、次のようなものがあります。
- setLineFormatメソッドは、円弧形状の線のフォーマット属性を指定します。
- setPlacementメソッドは、円弧がワークシートのセルにどのようにアタッチされるかを指定するPlacementTypeを指定します。
- FillFormat プロパティは、形状の塗りつぶし形式スタイルを指定します。
次の例では、ワークシートに円弧形状を追加する方法が示されています。例では、塗りつぶされた円弧と単純な円弧の2つを作成します。コードを実行すると、次の出力が生成されます。
ワークシートに2つの円弧形状が追加されます
// 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 の使用
- 描画 ツールバーで 楕円 をクリックします。
- 楕円を描画するにはドラッグします。
- 次のいずれかを行います。
- 楕円を開始点から円に制約するには、ドラッグしながら SHIFT キーを押します。
- 中心点から楕円を描くには、ドラッグしながら CTRL キーを押します。
Aspose.Cellsの使用
ShapeCollection クラスは、ワークシートに楕円形を追加するために使用される addShape というメソッドを提供しています。このメソッドは、Oval オブジェクトを返す可能性があります。Oval クラスは楕円形を表します。いくつか重要なメンバーがあります。
- setLineFormat メソッドは、楕円形のライン形式の属性を指定します。
- setPlacement メソッドは、楕円形がワークシートの中のセルにアタッチされる方法である PlacementType を指定します。
- FillFormat プロパティは、形状の塗りつぶし形式スタイルを指定します。
次の例は、ワークシートに楕円形を追加する方法を示しています。この例では、塗りつぶしのある楕円形と単純な円形の2つの楕円形が作成されます。 コードを実行すると、次の出力が生成されます。
ワークシートに2つの楕円形が追加されました
// 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"); |