Excel ファイルの画像と図形を挿入します。
必要な図形をワークシートに挿入する必要がある場合があります。ワークシートの異なる位置に同じ図形を挿入する必要がある場合があります。または、ワークシートに図形を一括挿入する必要がある場合もあります。
心配しないでください!Aspose.Cellsこれらすべての操作をサポートします。
Excel の図形は、主に次の種類に分類されます。
- ピクチャー
- OleObjects
- ライン
- 長方形
- 基本形状
- ブロック矢印
- 方程式の形
- フローチャート
- 星と旗
- 吹き出し
このガイド ドキュメントでは、各タイプから 1 つまたは 2 つの形状を選択してサンプルを作成します。これらの例を通じて、使用方法を学習します。Aspose.Cells指定した形状をワークシートに挿入します。
C# の Excel ワークシートに画像を追加する
スプレッドシートに画像を追加するのはとても簡単です。数行のコードしか必要ありません。 単純に追加の方法ピクチャーコレクション (ワークシート物体)。の追加メソッドは次のパラメータを取ります。
- 左上行インデックス、左上の行のインデックス。
- 左上の列インデックス、左上の列のインデックス。
- 画像ファイル名、パスを含む画像ファイルの名前。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Workbook object | |
int sheetIndex = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[sheetIndex]; | |
// Adding a picture at the location of a cell whose row and column indices | |
// Are 5 in the worksheet. It is "F6" cell | |
worksheet.Pictures.Add(5, 5, dataDir + "logo.jpg"); | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xls"); |
C# の Excel ワークシートに OLE オブジェクトを挿入する
Aspose.Cells は、ワークシートでの OLE オブジェクトの追加、抽出、および操作をサポートしています。このため、Aspose.Cells にはOleObjectCollectionコレクション リストに新しい OLE オブジェクトを追加するために使用されるクラス。別のクラス、OleObject、OLE オブジェクトを表します。いくつかの重要なメンバーがあります。
- の画像データプロパティは、バイト配列型のイメージ (アイコン) データを指定します。画像が表示され、ワークシートに OLE オブジェクトが表示されます。
- のオブジェクトデータプロパティは、バイト配列の形式でオブジェクト データを指定します。このデータは、OLE オブジェクト アイコンをダブルクリックすると、関連するプログラムに表示されます。
次の例は、OLE オブジェクトをワークシートに追加する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet. | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Define a string variable to store the image path. | |
string ImageUrl = dataDir + "logo.jpg"; | |
// Get the picture into the streams. | |
FileStream fs = File.OpenRead(ImageUrl); | |
// Define a byte array. | |
byte[] imageData = new Byte[fs.Length]; | |
// Obtain the picture into the array of bytes from streams. | |
fs.Read(imageData, 0, imageData.Length); | |
// Close the stream. | |
fs.Close(); | |
// Get an excel file path in a variable. | |
string path = dataDir + "book1.xls"; | |
// Get the file into the streams. | |
fs = File.OpenRead(path); | |
// Define an array of bytes. | |
byte[] objectData = new Byte[fs.Length]; | |
// Store the file from streams. | |
fs.Read(objectData, 0, objectData.Length); | |
// Close the stream. | |
fs.Close(); | |
// Add an Ole object into the worksheet with the image | |
// Shown in MS Excel. | |
sheet.OleObjects.Add(14, 3, 200, 220, imageData); | |
// Set embedded ole object data. | |
sheet.OleObjects[0].ObjectData = objectData; | |
// Save the excel file | |
workbook.Save(dataDir + "output.out.xls"); |
C# の Excel ワークシートに行を挿入する
線の形はに属します行カテゴリー。
Microsoft Excel (例: 2007):
- 行を挿入するセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- 次に、「最近使用した図形」または「線」から線を選択します
Aspose.Cells を使用
次のメソッドを使用して、ワークシートに行を挿入できます。
public LineShape AddLine( int upperLeftRow、 整数トップ、 int upperLeftColumn、 整数左、 整数の高さ、 整数幅 )
メソッドは線の形状物体。
次の例は、ワークシートに行を挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the line to the worksheet | |
sheet.Shapes.AddLine(2, 0, 2, 0, 100, 300);//method 1 | |
//sheet.Shapes.AddAutoShape(AutoShapeType.Line, 2, 0, 2, 0, 100, 300);//method 2 | |
//sheet.Shapes.AddShape(MsoDrawingType.Line, 2, 0, 2, 0, 100, 300);//method 3 | |
//Save.You can check your line in this way. | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
上記のコードを実行すると、次の結果が得られます。
C# の Excel ワークシートに線矢印を挿入する
線矢印の形状は、ラインカテゴリ。ラインの特殊なケースです。
Microsoft Excel (例: 2007):
- 線の矢印を挿入するセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- 次に、「最近使用した図形」または「線」から線の矢印を選択します
Aspose.Cells を使用
次のメソッドを使用して、ワークシートに線の矢印を挿入できます。
public LineShape AddLine( int upperLeftRow、 整数トップ、 int upperLeftColumn、 整数左、 整数の高さ、 整数幅 )
メソッドは線の形状物体。
次の例は、ワークシートに線矢印を挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the line arrow to the worksheet | |
Shape s = sheet.Shapes.AddLine(2, 0, 2, 0, 100, 300);//method 1 | |
//Shape s = sheet.Shapes.AddAutoShape(AutoShapeType.Line, 2, 0, 2, 0, 100, 300);//method 2 | |
//Shape s = sheet.Shapes.AddShape(MsoDrawingType.Line, 2, 0, 2, 0, 100, 300);//method 3 | |
//add a arrow at the line begin | |
s.Line.BeginArrowheadStyle = MsoArrowheadStyle.Arrow;//arrow type | |
s.Line.BeginArrowheadWidth = MsoArrowheadWidth.Wide;//arrow width | |
s.Line.BeginArrowheadLength = MsoArrowheadLength.Short;//arrow length | |
//add a arrow at the line end | |
s.Line.EndArrowheadStyle = MsoArrowheadStyle.ArrowOpen;//arrow type | |
s.Line.EndArrowheadWidth = MsoArrowheadWidth.Narrow;//arrow width | |
s.Line.EndArrowheadLength = MsoArrowheadLength.Long;//arrow length | |
//Save.You can check your arrow in this way. | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
上記のコードを実行すると、次の結果が得られます。
C# の Excel ワークシートに四角形を挿入する
長方形の形状は、長方形カテゴリー。
Microsoft Excel (例: 2007):
- 四角形を挿入するセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- 次に、「最近使用した形状」または「長方形」から長方形を選択します
Aspose.Cells を使用
次のメソッドを使用して、ワークシートに四角形を挿入できます。
public RectangleShape AddRectangle( int upperLeftRow、 整数トップ、 int upperLeftColumn、 整数左、 整数の高さ、 整数幅 )
メソッドは長方形の形物体。
次の例は、四角形をワークシートに挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the rectangle to the worksheet | |
sheet.Shapes.AddRectangle(2, 0, 2, 0, 100, 300); | |
//Save | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
上記のコードを実行すると、次の結果が得られます。
C# の Excel ワークシートにキューブを挿入する
立方体の形状は、基本形状カテゴリー。
Microsoft Excel (例: 2007):
- 立方体を挿入するセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- 次に、からキューブを選択します基本形状
Aspose.Cells を使用
次のメソッドを使用して、ワークシートにキューブを挿入できます。
メソッドは形物体。
次の例は、キューブをワークシートに挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the cube to the worksheet | |
sheet.Shapes.AddAutoShape(AutoShapeType.Cube, 2, 0, 2, 0, 100, 300); | |
//Save.You can check your cube in this way. | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
上記のコードを実行すると、次の結果が得られます。
C# の Excel ワークシートに吹き出し四角矢印を挿入する
吹き出しの四角形矢印の形状は、ブロック矢印カテゴリー。
Microsoft Excel (例: 2007):
- 吹き出しの四角形矢印を挿入するセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- 次に、吹き出しのクワッド矢印を選択します。ブロック矢印
Aspose.Cells を使用
次の方法を使用して、ワークシートに四角形吹き出し矢印を挿入できます。
メソッドは形物体。
次の例は、吹き出し四角形矢印をワークシートに挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the callout quad arrow to the worksheet | |
sheet.Shapes.AddAutoShape(AutoShapeType.QuadArrowCallout, 2, 0, 2, 0, 100, 100); | |
//Save | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
上記のコードを実行すると、次の結果が得られます。
C# の Excel ワークシートに乗算記号を挿入する
乗算記号の形状は、方程式の形カテゴリー。
Microsoft Excel (例: 2007):
- 乗算記号を挿入するセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- 次に、から乗算記号を選択します方程式の形
Aspose.Cells を使用
次のメソッドを使用して、ワークシートに乗算記号を挿入できます。
メソッドは形物体。
次の例は、乗算記号をワークシートに挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the multiplication sign to the worksheet | |
sheet.Shapes.AddAutoShape(AutoShapeType.MathMultiply, 2, 0, 2, 0, 100, 100); | |
//Save.You can check your multiplication in this way. | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
上記のコードを実行すると、次の結果が得られます。
マルチドキュメントを C# の Excel ワークシートに挿入する
multidocument の形状は、フローチャートカテゴリー。
Microsoft Excel (例: 2007):
- マルチドキュメントを挿入するセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- 次に、マルチドキュメントを選択しますフローチャート
Aspose.Cells を使用
次のメソッドを使用して、マルチドキュメントをワークシートに挿入できます。
メソッドは形物体。
次の例は、マルチドキュメントをワークシートに挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the multidocument to the worksheet | |
sheet.Shapes.AddAutoShape(AutoShapeType.FlowChartMultidocument, 2, 0, 2, 0, 100, 100); | |
//Save | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
上記のコードを実行すると、次の結果が得られます。
C# の Excel ワークシートに五芒星を挿入する
五芒星の形状は、星と旗カテゴリー。
Microsoft Excel (例: 2007):
- 五芒星を挿入したいセルを選択
- [挿入] メニューをクリックし、[図形] をクリックします。
- 次に五芒星を選択します。星と旗
Aspose.Cells を使用
次のメソッドを使用して、ワークシートに五芒星を挿入できます。
メソッドは形物体。
次の例は、ワークシートに五芒星を挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the Five-pointed star to the worksheet | |
sheet.Shapes.AddAutoShape(AutoShapeType.Star5, 2, 0, 2, 0, 100, 100); | |
//Save.You can check your icon in this way. | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
上記のコードを実行すると、次の結果が得られます。
C# の Excel ワークシートに吹き出し雲を挿入する
思考の泡の雲の形は、吹き出しカテゴリー。
Microsoft Excel (例: 2007):
- ふきだし雲を挿入するセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- 次に、から思考の泡の雲を選択します吹き出し
Aspose.Cells を使用
次の方法を使用して、ワークシートに吹き出しの雲を挿入できます。
メソッドは形物体。
次の例は、吹き出しの雲をワークシートに挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the thought bubble cloud to the worksheet | |
sheet.Shapes.AddAutoShape(AutoShapeType.CloudCallout, 2, 0, 2, 0, 100, 100); | |
//Save | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
上記のコードを実行すると、次の結果が得られます。